]> glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/__init__.py
tools: Upver to 1.2.1
[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 + "/waydroid.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" and args.action != "log"):
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 # This branch is taken if:
43 # - waydroid is not yet initialized
44 # - waydroid is invoked with no command or with log
45 if not os.path.exists(args.log):
46 # The log could have been already created if init was used and failed, if its not the case we use a temporary one
47 args.log = "/tmp/tools.log"
48
49 tools_logging.init(args)
50
51 # Initialize or require config
52 if args.action == "init":
53 actionNeedRoot(args.action)
54 actions.init(args)
55 elif args.action == "upgrade":
56 actionNeedRoot(args.action)
57 actions.upgrade(args)
58 elif args.action == "session":
59 if args.subaction == "start":
60 actions.session_manager.start(args)
61 elif args.subaction == "stop":
62 actions.session_manager.stop(args)
63 else:
64 logging.info(
65 "Run waydroid {} -h for usage information.".format(args.action))
66 elif args.action == "container":
67 actionNeedRoot(args.action)
68 if args.subaction == "start":
69 actions.container_manager.start(args)
70 elif args.subaction == "stop":
71 actions.container_manager.stop(args)
72 elif args.subaction == "restart":
73 actions.container_manager.restart(args)
74 elif args.subaction == "freeze":
75 actions.container_manager.freeze(args)
76 elif args.subaction == "unfreeze":
77 actions.container_manager.unfreeze(args)
78 else:
79 logging.info(
80 "Run waydroid {} -h for usage information.".format(args.action))
81 elif args.action == "app":
82 if args.subaction == "install":
83 actions.app_manager.install(args)
84 elif args.subaction == "remove":
85 actions.app_manager.remove(args)
86 elif args.subaction == "launch":
87 actions.app_manager.launch(args)
88 elif args.subaction == "list":
89 actions.app_manager.list(args)
90 else:
91 logging.info(
92 "Run waydroid {} -h for usage information.".format(args.action))
93 elif args.action == "prop":
94 if args.subaction == "get":
95 ret = helpers.props.get(args, args.key)
96 if ret:
97 print(ret)
98 elif args.subaction == "set":
99 helpers.props.set(args, args.key, args.value)
100 else:
101 logging.info(
102 "Run waydroid {} -h for usage information.".format(args.action))
103 elif args.action == "shell":
104 actionNeedRoot(args.action)
105 helpers.lxc.shell(args)
106 elif args.action == "logcat":
107 actionNeedRoot(args.action)
108 helpers.lxc.logcat(args)
109 elif args.action == "show-full-ui":
110 actions.app_manager.showFullUI(args)
111 elif args.action == "status":
112 actions.status.print_status(args)
113 elif args.action == "log":
114 if args.clear_log:
115 helpers.run.user(args, ["truncate", "-s", "0", args.log])
116 try:
117 helpers.run.user(
118 args, ["tail", "-n", args.lines, "-F", args.log], output="tui")
119 except KeyboardInterrupt:
120 pass
121 else:
122 logging.info("Run waydroid -h for usage information.")
123
124 #logging.info("Done")
125
126 except Exception as e:
127 # Dump log to stdout when args (and therefore logging) init failed
128 if not args:
129 logging.getLogger().setLevel(logging.DEBUG)
130
131 logging.info("ERROR: " + str(e))
132 logging.info("See also: <https://github.com/waydroid>")
133 logging.debug(traceback.format_exc())
134
135 # Hints about the log file (print to stdout only)
136 log_hint = "Run 'waydroid log' for details."
137 if not args or not os.path.exists(args.log):
138 log_hint += (" Alternatively you can use '--details-to-stdout' to"
139 " get more output, e.g. 'waydroid"
140 " --details-to-stdout init'.")
141 print(log_hint)
142 return 1
143
144
145 if __name__ == "__main__":
146 sys.exit(main())