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
This commit is contained in:
parent
3aabaf451c
commit
5f5050f9a3
|
@ -55,8 +55,6 @@ def replace_variable(apkbuild, value: str) -> str:
|
||||||
for match in revar2.finditer(value):
|
for match in revar2.finditer(value):
|
||||||
try:
|
try:
|
||||||
newvalue = apkbuild[match.group(1)]
|
newvalue = apkbuild[match.group(1)]
|
||||||
if type(newvalue) is list:
|
|
||||||
newvalue = " ".join(newvalue)
|
|
||||||
logging.verbose("{}: replace '{}' with '{}'".format(
|
logging.verbose("{}: replace '{}' with '{}'".format(
|
||||||
apkbuild["pkgname"], match.group(0),
|
apkbuild["pkgname"], match.group(0),
|
||||||
newvalue))
|
newvalue))
|
||||||
|
@ -102,28 +100,6 @@ def replace_variable(apkbuild, value: str) -> str:
|
||||||
return value
|
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):
|
def cut_off_function_names(apkbuild):
|
||||||
"""
|
"""
|
||||||
For subpackages: only keep the subpackage name, without the internal
|
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
|
# Read the file and check line endings
|
||||||
lines = read_file(path)
|
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
|
# Parse all attributes from the config
|
||||||
|
ret = {key: "" for key in pmb.config.apkbuild_attributes.keys()}
|
||||||
for i in range(len(lines)):
|
for i in range(len(lines)):
|
||||||
for attribute, options in pmb.config.apkbuild_attributes.items():
|
for attribute, options in pmb.config.apkbuild_attributes.items():
|
||||||
found, value, i = parse_attribute(attribute, lines, i, path)
|
found, value, i = parse_attribute(attribute, lines, i, path)
|
||||||
if not found:
|
if not found:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Support depends="$depends hello-world" (#1800)
|
ret[attribute] = replace_variable(ret, value)
|
||||||
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)
|
|
||||||
|
|
||||||
|
# Split attributes
|
||||||
|
for attribute, options in pmb.config.apkbuild_attributes.items():
|
||||||
|
if options["array"]:
|
||||||
# Split up arrays, delete empty strings inside the list
|
# Split up arrays, delete empty strings inside the list
|
||||||
if options["array"]:
|
ret[attribute] = list(filter(None, ret[attribute].split(" ")))
|
||||||
if value:
|
|
||||||
value = list(filter(None, value.split(" ")))
|
|
||||||
else:
|
|
||||||
value = []
|
|
||||||
ret[attribute] = value
|
|
||||||
|
|
||||||
# Properly format values
|
|
||||||
ret = replace_variables(ret)
|
|
||||||
ret = cut_off_function_names(ret)
|
ret = cut_off_function_names(ret)
|
||||||
|
|
||||||
# Sanity check: pkgname
|
# Sanity check: pkgname
|
||||||
|
|
|
@ -2,6 +2,7 @@ pkgname="variable-replacements"
|
||||||
pkgver="1.0.0"
|
pkgver="1.0.0"
|
||||||
pkgrel=0
|
pkgrel=0
|
||||||
arch="armhf"
|
arch="armhf"
|
||||||
pkgdesc="variable-replacements test"
|
pkgdesc="$pkgdesc$pkgname test"
|
||||||
url="${pkgname/variable-} ${pkgname/-replacements/} ${pkgname/variable/string}"
|
url="${pkgname/variable-} ${pkgname/-replacements/} ${pkgname/variable/string}"
|
||||||
subpackages="${pkgdesc#variable-}"
|
subpackages="${pkgdesc#variable-}"
|
||||||
|
pkgdesc="this should not affect anything"
|
||||||
|
|
Loading…
Reference in New Issue