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