Run shell scripts with -e (Closes: #1035)

Make sudo, init prebuild, build and Prepare fields lists and only
concatenate them with '; ' before execution. This allows arbitrary
commands inside the fileds (even && and ';') as we don't need to split
the commands again for rewritemeta.
This commit is contained in:
Jochen Sprickerhof 2022-09-09 12:36:54 +02:00 committed by Michael Pöhn
parent 49d8ba3b9b
commit 557fe87d44
10 changed files with 477 additions and 254 deletions

View file

@ -390,7 +390,7 @@ def build_local(app, build, vcs, build_dir, output_dir, log_dir, srclib_dir, ext
logging.info("Running 'sudo' commands in %s" % os.getcwd())
p = FDroidPopen(['sudo', 'DEBIAN_FRONTEND=noninteractive',
'bash', '-x', '-c', build.sudo])
'bash', '-e', '-u', '-o', 'pipefail', '-x', '-c', '; '.join(build.sudo)])
if p.returncode != 0:
raise BuildException("Error running sudo command for %s:%s" %
(app.id, build.versionName), p.output)
@ -535,13 +535,13 @@ def build_local(app, build, vcs, build_dir, output_dir, log_dir, srclib_dir, ext
# Run a build command if one is required...
if build.build:
logging.info("Running 'build' commands in %s" % root_dir)
cmd = common.replace_config_vars(build.build, build)
cmd = common.replace_config_vars("; ".join(build.build), build)
# Substitute source library paths into commands...
for name, number, libpath in srclibpaths:
cmd = cmd.replace('$$' + name + '$$', os.path.join(os.getcwd(), libpath))
p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
p = FDroidPopen(['bash', '-e', '-u', '-o', 'pipefail', '-x', '-c', cmd], cwd=root_dir)
if p.returncode != 0:
raise BuildException("Error running build command for %s:%s" %

View file

@ -2055,9 +2055,9 @@ def getsrclib(spec, srclib_dir, basepath=False,
if prepare:
if srclib["Prepare"]:
cmd = replace_config_vars(srclib["Prepare"], build)
cmd = replace_config_vars("; ".join(srclib["Prepare"]), build)
p = FDroidPopen(['bash', '-x', '-c', '--', cmd], cwd=libdir)
p = FDroidPopen(['bash', '-e', '-u', '-o', 'pipefail', '-x', '-c', '--', cmd], cwd=libdir)
if p.returncode != 0:
raise BuildException("Error running prepare command for srclib %s"
% name, p.output)
@ -2119,10 +2119,10 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=
# Run an init command if one is required
if build.init:
cmd = replace_config_vars(build.init, build)
cmd = replace_config_vars("; ".join(build.init), build)
logging.info("Running 'init' commands in %s" % root_dir)
p = FDroidPopen(['bash', '-x', '-c', '--', cmd], cwd=root_dir)
p = FDroidPopen(['bash', '-e', '-u', '-o', 'pipefail', '-x', '-c', '--', cmd], cwd=root_dir)
if p.returncode != 0:
raise BuildException("Error running init command for %s:%s" %
(app.id, build.versionName), p.output)
@ -2286,13 +2286,13 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=
if build.prebuild:
logging.info("Running 'prebuild' commands in %s" % root_dir)
cmd = replace_config_vars(build.prebuild, build)
cmd = replace_config_vars("; ".join(build.prebuild), build)
# Substitute source library paths into prebuild commands
for name, number, libpath in srclibpaths:
cmd = cmd.replace('$$' + name + '$$', os.path.join(os.getcwd(), libpath))
p = FDroidPopen(['bash', '-x', '-c', '--', cmd], cwd=root_dir)
p = FDroidPopen(['bash', '-e', '-u', '-o', 'pipefail', '-x', '-c', '--', cmd], cwd=root_dir)
if p.returncode != 0:
raise BuildException("Error running prebuild command for %s:%s" %
(app.id, build.versionName), p.output)

View file

@ -516,8 +516,11 @@ def parse_yaml_srclib(metadatapath):
thisinfo[key] = data[key]
elif data[key] is None:
thisinfo[key] = ['']
elif key == 'Prepare' and isinstance(data[key], list):
thisinfo[key] = ' && '.join(data[key])
elif key == 'Prepare' or flagtype(key) == TYPE_SCRIPT:
if isinstance(data[key], list):
thisinfo[key] = data[key]
else:
thisinfo[key] = [data[key]] if data[key] else []
else:
thisinfo[key] = str(data[key] or '')
@ -847,9 +850,8 @@ def post_parse_yaml_metadata(yamldata):
_flagtype = flagtype(flag)
if _flagtype is TYPE_SCRIPT:
# concatenate script flags into a single string if they are stored as list
if isinstance(build[flag], list):
build[flag] = ' && '.join(build[flag])
if isinstance(build[flag], str):
build[flag] = [build[flag]]
elif _flagtype is TYPE_STRING:
# things like versionNames are strings, but without quotes can be numbers
if isinstance(build[flag], float) or isinstance(build[flag], int):
@ -916,12 +918,6 @@ def write_yaml(mf, app):
return value[0]
else:
return value
else:
script_lines = value.split(' && ')
if len(script_lines) > 1:
return script_lines
else:
return value
else:
return value