From 5f5050f9a30f5fcaf3ac869e0917a465b8c790eb Mon Sep 17 00:00:00 2001 From: Minecrell Date: Mon, 27 Jan 2020 22:24:45 +0100 Subject: [PATCH] pmb.parse._apkbuild: Replace variables immediately after parsing (!1866) At the moment we parse all attributes, split them, and eventually join them back together for variable replacement. Replacing variables immediately after parsing (before splitting) has several advantages: - No need to handle different value types (e.g. lists by joining them every time they are accessed) - Variables like depends="$depends ..." are handled directly by the variable parser - APKBUILDs are shell scripts, so we match abuild more closely if variables defined later do not affect previous attributes --- pmb/parse/_apkbuild.py | 52 +++---------------- .../apkbuild/APKBUILD.variable-replacements | 3 +- 2 files changed, 8 insertions(+), 47 deletions(-) diff --git a/pmb/parse/_apkbuild.py b/pmb/parse/_apkbuild.py index f9854205..e5006fb2 100644 --- a/pmb/parse/_apkbuild.py +++ b/pmb/parse/_apkbuild.py @@ -55,8 +55,6 @@ def replace_variable(apkbuild, value: str) -> str: for match in revar2.finditer(value): try: newvalue = apkbuild[match.group(1)] - if type(newvalue) is list: - newvalue = " ".join(newvalue) logging.verbose("{}: replace '{}' with '{}'".format( apkbuild["pkgname"], match.group(0), newvalue)) @@ -102,28 +100,6 @@ def replace_variable(apkbuild, value: str) -> str: return value -def replace_variables(apkbuild): - """ - Replace a hardcoded list of variables inside the APKBUILD. - """ - ret = apkbuild - - # Iterate through all apkbuild attributes which get parsed - for key in pmb.config.apkbuild_attributes: - if type(ret[key]) is list: - replaced = [] - for value in ret[key]: - replaced_value = replace_variable(ret, value).split() - replaced.extend(replaced_value) - ret[key] = replaced - elif type(ret[key]) is str: - ret[key] = replace_variable(ret, ret[key]) - else: - raise RuntimeError("Value type " + type(ret[key]) + " not handled.") - - return ret - - def cut_off_function_names(apkbuild): """ For subpackages: only keep the subpackage name, without the internal @@ -255,38 +231,22 @@ def apkbuild(args, path, check_pkgver=True, check_pkgname=True): # Read the file and check line endings lines = read_file(path) - # Add default attributes - ret = {} - for attribute, options in pmb.config.apkbuild_attributes.items(): - if options["array"]: - ret[attribute] = [] - else: - ret[attribute] = "" - # Parse all attributes from the config + ret = {key: "" for key in pmb.config.apkbuild_attributes.keys()} for i in range(len(lines)): for attribute, options in pmb.config.apkbuild_attributes.items(): found, value, i = parse_attribute(attribute, lines, i, path) if not found: continue - # Support depends="$depends hello-world" (#1800) - if attribute == "depends" and ("${depends}" in value or - "$depends" in value): - previous = " ".join(ret["depends"]) if "depends" in ret else "" - value = value.replace("${depends}", previous) - value = value.replace("$depends", previous) + ret[attribute] = replace_variable(ret, value) + # Split attributes + for attribute, options in pmb.config.apkbuild_attributes.items(): + if options["array"]: # Split up arrays, delete empty strings inside the list - if options["array"]: - if value: - value = list(filter(None, value.split(" "))) - else: - value = [] - ret[attribute] = value + ret[attribute] = list(filter(None, ret[attribute].split(" "))) - # Properly format values - ret = replace_variables(ret) ret = cut_off_function_names(ret) # Sanity check: pkgname diff --git a/test/testdata/apkbuild/APKBUILD.variable-replacements b/test/testdata/apkbuild/APKBUILD.variable-replacements index ad696873..31ab73f2 100644 --- a/test/testdata/apkbuild/APKBUILD.variable-replacements +++ b/test/testdata/apkbuild/APKBUILD.variable-replacements @@ -2,6 +2,7 @@ pkgname="variable-replacements" pkgver="1.0.0" pkgrel=0 arch="armhf" -pkgdesc="variable-replacements test" +pkgdesc="$pkgdesc$pkgname test" url="${pkgname/variable-} ${pkgname/-replacements/} ${pkgname/variable/string}" subpackages="${pkgdesc#variable-}" +pkgdesc="this should not affect anything"