]> glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/__init__.py
Readme: Add documentation site
[waydroid.git] / tools / __init__.py
1 # Copyright 2021 Oliver Smith
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 # PYTHON_ARGCOMPLETE_OK
4 import sys
5 import logging
6 import os
7 import traceback
8
9 from . import actions
10 from . import config
11 from . import helpers
12 from .helpers import logging as tools_logging
13
14
15 def main():
16 def actionNeedRoot(action):
17 if os.geteuid() != 0:
18 raise RuntimeError(
19 "Action \"{}\" needs root access".format(action))
20
21 # Wrap everything to display nice error messages
22 args = None
23 try:
24 os.umask(0o000)
25 # Parse arguments, set up logging
26 args = helpers.arguments()
27 args.cache = {}
28 args.work = config.defaults["work"]
29 args.config = args.work + "/waydroid.cfg"
30 args.log = args.work + "/tools.log"
31 args.sudo_timer = True
32 args.timeout = 1800
33
34 if not os.path.isfile(args.config):
35 if args.action and args.action != "init":
36 print('ERROR: WayDroid is not initialized, run "waydroid init"')
37 return 0
38 elif os.geteuid() == 0 and args.action == "init":
39 if not os.path.exists(args.work):
40 os.mkdir(args.work)
41 else:
42 args.log = "/tmp/tools.log"
43
44 tools_logging.init(args)
45
46 # Initialize or require config
47 if args.action == "init":
48 actionNeedRoot(args.action)
49 actions.init(args)
50 elif args.action == "upgrade":
51 actionNeedRoot(args.action)
52 actions.upgrade(args)
53 elif args.action == "session":
54 if args.subaction == "start":
55 actions.session_manager.start(args)
56 elif args.subaction == "stop":
57 actions.session_manager.stop(args)
58 else:
59 logging.info(
60 "Run waydroid {} -h for usage information.".format(args.action))
61 elif args.action == "container":
62 actionNeedRoot(args.action)
63 if args.subaction == "start":
64 actions.container_manager.start(args)
65 elif args.subaction == "stop":
66 actions.container_manager.stop(args)
67 if args.subaction == "restart":
68 actions.container_manager.restart(args)
69 elif args.subaction == "freeze":
70 actions.container_manager.freeze(args)
71 elif args.subaction == "unfreeze":
72 actions.container_manager.unfreeze(args)
73 else:
74 logging.info(
75 "Run waydroid {} -h for usage information.".format(args.action))
76 elif args.action == "app":
77 if args.subaction == "install":
78 actions.app_manager.install(args)
79 elif args.subaction == "remove":
80 actions.app_manager.remove(args)
81 elif args.subaction == "launch":
82 actions.app_manager.launch(args)
83 elif args.subaction == "list":
84 actions.app_manager.list(args)
85 else:
86 logging.info(
87 "Run waydroid {} -h for usage information.".format(args.action))
88 elif args.action == "prop":
89 if args.subaction == "get":
90 ret = helpers.props.get(args, args.key)
91 if ret:
92 print(ret)
93 elif args.subaction == "set":
94 helpers.props.set(args, args.key, args.value)
95 else:
96 logging.info(
97 "Run waydroid {} -h for usage information.".format(args.action))
98 elif args.action == "shell":
99 actionNeedRoot(args.action)
100 helpers.lxc.shell(args)
101 elif args.action == "logcat":
102 actionNeedRoot(args.action)
103 helpers.lxc.logcat(args)
104 elif args.action == "show-full-ui":
105 actions.app_manager.showFullUI(args)
106 elif args.action == "status":
107 actions.status.print_status(args)
108 elif args.action == "log":
109 if args.clear_log:
110 helpers.run.user(args, ["truncate", "-s", "0", args.log])
111 helpers.run.user(
112 args, ["tail", "-n", args.lines, "-F", args.log], output="tui")
113 else:
114 logging.info("Run waydroid -h for usage information.")
115
116 #logging.info("Done")
117
118 except Exception as e:
119 # Dump log to stdout when args (and therefore logging) init failed
120 if not args:
121 logging.getLogger().setLevel(logging.DEBUG)
122
123 logging.info("ERROR: " + str(e))
124 logging.info("See also: <https://github.com/waydroid>")
125 logging.debug(traceback.format_exc())
126
127 # Hints about the log file (print to stdout only)
128 log_hint = "Run 'waydroid log' for details."
129 if not args or not os.path.exists(args.log):
130 log_hint += (" Alternatively you can use '--details-to-stdout' to"
131 " get more output, e.g. 'waydroid"
132 " --details-to-stdout init'.")
133 print(log_hint)
134 return 1
135
136
137 if __name__ == "__main__":
138 sys.exit(main())