]>
glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/helpers/run.py
1 # Copyright 2021 Oliver Smith
2 # SPDX-License-Identifier: GPL-3.0-or-later
4 import tools
.helpers
.run_core
7 def flat_cmd(cmd
, working_dir
=None, env
={}):
9 Convert a shell command passed as list into a flat shell string with
12 :param cmd: command as list, e.g. ["echo", "string with spaces"]
13 :param working_dir: when set, prepend "cd ...;" to execute the command
14 in the given working directory
15 :param env: dict of environment variables to be passed to the command, e.g.
17 :returns: the flat string, e.g.
18 echo 'string with spaces'
19 cd /home/pmos;echo 'string with spaces'
21 # Merge env and cmd into escaped list
23 for key
, value
in env
.items():
24 escaped
.append(key
+ "=" + shlex
.quote(value
))
25 for i
in range(len(cmd
)):
26 escaped
.append(shlex
.quote(cmd
[i
]))
29 ret
= " ".join(escaped
)
31 ret
= "cd " + shlex
.quote(working_dir
) + ";" + ret
36 def user(args
, cmd
, working_dir
=None, output
="log", output_return
=False,
37 check
=None, env
={}, sudo
=False):
39 Run a command on the host system as user.
41 :param env: dict of environment variables to be passed to the command, e.g.
44 See tools.helpers.run_core.core() for a detailed description of all other
45 arguments and the return value.
47 # Readable log message (without all the escaping)
49 for key
, value
in env
.items():
50 msg
+= key
+ "=" + value
+ " "
52 msg
+= "cd " + working_dir
+ "; "
55 # Add environment variables and run
57 cmd
= ["sh", "-c", flat_cmd(cmd
, env
=env
)]
58 return tools
.helpers
.run_core
.core(args
, msg
, cmd
, working_dir
, output
,
59 output_return
, check
, sudo
)
62 def root(args
, cmd
, working_dir
=None, output
="log", output_return
=False,
65 Run a command on the host system as root, with sudo.
67 :param env: dict of environment variables to be passed to the command, e.g.
70 See tools.helpers.run_core.core() for a detailed description of all other
71 arguments and the return value.
74 cmd
= ["sh", "-c", flat_cmd(cmd
, env
=env
)]
77 return user(args
, cmd
, working_dir
, output
, output_return
, check
, env
,