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