Debian Codegraph @

src/amd_debug/s2idle-hook

raw
1 #!/usr/bin/python3
2 # SPDX-License-Identifier: MIT
3
4 import argparse
5 import logging
6 import os
7 import sys
8
9
10 def main(action, debug):
11 """Main function to run the systemd hook"""
12 try:
13 from amd_debug.validator import (
14 SleepValidator,
15 ) # pylint: disable=import-outside-toplevel
16 from amd_debug.prerequisites import (
17 PrerequisiteValidator,
18 ) # pylint: disable=import-outside-toplevel
19 except ModuleNotFoundError:
20 return
21
22 app = SleepValidator(tool_debug=debug, bios_debug=False)
23 if action == "pre":
24 prereq = PrerequisiteValidator(tool_debug=debug)
25 prereq.capture_once()
26 app.systemd_pre_hook()
27 elif action == "post":
28 app.systemd_post_hook()
29
30
31 def parse_args():
32 """Parse command line arguments"""
33 parser = argparse.ArgumentParser(
34 description="amd-s2idle systemd hook",
35 )
36
37 parser.add_argument(
38 "action",
39 choices=["pre", "post"],
40 help="Action to perform",
41 )
42 parser.add_argument(
43 "--debug",
44 action="store_true",
45 )
46 parser.add_argument(
47 "mode",
48 help="Mode to perform",
49 )
50
51 parser.add_argument("--log", default="/dev/null")
52 parser.add_argument("--path", default="")
53
54 if len(sys.argv) == 1:
55 parser.print_help(sys.stderr)
56 sys.exit(1)
57 return parser.parse_args()
58
59
60 if __name__ == "__main__":
61 args = parse_args()
62
63 if (
64 args.mode != "suspend"
65 and not os.getenv("SYSTEMD_SLEEP_ACTION", "").startswith("suspend")
66 ):
67 sys.exit(0)
68
69 if args.path:
70 sys.path.append(args.path)
71
72 logging.basicConfig(
73 filename=args.log, level=logging.DEBUG if args.debug else logging.WARNING
74 )
75
76 main(args.action, args.debug)