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