]> glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/__init__.py
.desktop files should not be executable (#815)
[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 import dbus.mainloop.glib
9 import dbus
10 import dbus.exceptions
11
12 from . import actions
13 from . import config
14 from . import helpers
15 from .helpers import logging as tools_logging
16
17
18 def main():
19 def actionNeedRoot(action):
20 if os.geteuid() != 0:
21 raise RuntimeError(
22 "Action \"{}\" needs root access".format(action))
23
24 # Wrap everything to display nice error messages
25 args = None
26 try:
27 # Parse arguments, set up logging
28 args = helpers.arguments()
29 args.cache = {}
30 args.work = config.defaults["work"]
31 args.config = args.work + "/waydroid.cfg"
32 args.log = args.work + "/waydroid.log"
33 args.sudo_timer = True
34 args.timeout = 1800
35
36 if os.geteuid() == 0:
37 if not os.path.exists(args.work):
38 os.mkdir(args.work)
39 elif not os.path.exists(args.log):
40 args.log = "/tmp/tools.log"
41
42 tools_logging.init(args)
43
44 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
45 dbus.mainloop.glib.threads_init()
46 dbus_name_scope = None
47
48 if not actions.initializer.is_initialized(args) and \
49 args.action and args.action not in ("init", "first-launch", "log"):
50 if args.wait_for_init:
51 try:
52 dbus_name_scope = dbus.service.BusName("id.waydro.Container", dbus.SystemBus(), do_not_queue=True)
53 actions.wait_for_init(args)
54 except dbus.exceptions.NameExistsException:
55 print('ERROR: WayDroid service is already awaiting initialization')
56 return 1
57 else:
58 print('ERROR: WayDroid is not initialized, run "waydroid init"')
59 return 0
60
61 # Initialize or require config
62 if args.action == "init":
63 actionNeedRoot(args.action)
64 actions.init(args)
65 elif args.action == "upgrade":
66 actionNeedRoot(args.action)
67 actions.upgrade(args)
68 elif args.action == "session":
69 if args.subaction == "start":
70 actions.session_manager.start(args)
71 elif args.subaction == "stop":
72 actions.session_manager.stop(args)
73 else:
74 logging.info(
75 "Run waydroid {} -h for usage information.".format(args.action))
76 elif args.action == "container":
77 actionNeedRoot(args.action)
78 if args.subaction == "start":
79 if dbus_name_scope is None:
80 try:
81 dbus_name_scope = dbus.service.BusName("id.waydro.Container", dbus.SystemBus(), do_not_queue=True)
82 except dbus.exceptions.NameExistsException:
83 print('ERROR: WayDroid container service is already running')
84 return 1
85 actions.container_manager.start(args)
86 elif args.subaction == "stop":
87 actions.container_manager.stop(args)
88 elif args.subaction == "restart":
89 actions.container_manager.restart(args)
90 elif args.subaction == "freeze":
91 actions.container_manager.freeze(args)
92 elif args.subaction == "unfreeze":
93 actions.container_manager.unfreeze(args)
94 else:
95 logging.info(
96 "Run waydroid {} -h for usage information.".format(args.action))
97 elif args.action == "app":
98 if args.subaction == "install":
99 actions.app_manager.install(args)
100 elif args.subaction == "remove":
101 actions.app_manager.remove(args)
102 elif args.subaction == "launch":
103 actions.app_manager.launch(args)
104 elif args.subaction == "intent":
105 actions.app_manager.intent(args)
106 elif args.subaction == "list":
107 actions.app_manager.list(args)
108 else:
109 logging.info(
110 "Run waydroid {} -h for usage information.".format(args.action))
111 elif args.action == "prop":
112 if args.subaction == "get":
113 actions.prop.get(args)
114 elif args.subaction == "set":
115 actions.prop.set(args)
116 else:
117 logging.info(
118 "Run waydroid {} -h for usage information.".format(args.action))
119 elif args.action == "shell":
120 actionNeedRoot(args.action)
121 helpers.lxc.shell(args)
122 elif args.action == "logcat":
123 actionNeedRoot(args.action)
124 helpers.lxc.logcat(args)
125 elif args.action == "show-full-ui":
126 actions.app_manager.showFullUI(args)
127 elif args.action == "first-launch":
128 actions.remote_init_client(args)
129 if actions.initializer.is_initialized(args):
130 actions.app_manager.showFullUI(args)
131 elif args.action == "status":
132 actions.status.print_status(args)
133 elif args.action == "log":
134 if args.clear_log:
135 helpers.run.user(args, ["truncate", "-s", "0", args.log])
136 try:
137 helpers.run.user(
138 args, ["tail", "-n", args.lines, "-F", args.log], output="tui")
139 except KeyboardInterrupt:
140 pass
141 else:
142 logging.info("Run waydroid -h for usage information.")
143
144 #logging.info("Done")
145
146 except Exception as e:
147 # Dump log to stdout when args (and therefore logging) init failed
148 if not args:
149 logging.getLogger().setLevel(logging.DEBUG)
150
151 logging.info("ERROR: " + str(e))
152 logging.info("See also: <https://github.com/waydroid>")
153 logging.debug(traceback.format_exc())
154
155 # Hints about the log file (print to stdout only)
156 log_hint = "Run 'waydroid log' for details."
157 if not args or not os.path.exists(args.log):
158 log_hint += (" Alternatively you can use '--details-to-stdout' to"
159 " get more output, e.g. 'waydroid"
160 " --details-to-stdout init'.")
161 print(log_hint)
162 return 1
163
164
165 if __name__ == "__main__":
166 sys.exit(main())