pmb.parse._apkbuild: Set default attribute values before starting to parse (!866)

Theoretically it is possible to reference variables before they are defined.
In the shell, these will simply be evaluated to an empty string.
In preparation of replacing variables immediately after parsing attributes,
these variables will be no longer replaced correctly.

We can simplify the code further, and avoid this problem by initializing
the dict with the default values, replacing them with the real values
from the APKBUILD.

This will also avoid a (somewhat unrelated) error in the bootimg test:

  File "pmb/parse/_apkbuild.py", line 46, in replace_variable
    apkbuild["pkgname"], match.group(0),
  KeyError: 'pkgname'
This commit is contained in:
Minecrell 2020-01-28 11:12:26 +01:00 committed by Alexey Min
parent 33e3553cdd
commit 3aabaf451c
No known key found for this signature in database
GPG Key ID: 463F84201DACD7B9
1 changed files with 8 additions and 9 deletions

View File

@ -255,8 +255,15 @@ def apkbuild(args, path, check_pkgver=True, check_pkgname=True):
# Read the file and check line endings
lines = read_file(path)
# Parse all attributes from the config
# 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
for i in range(len(lines)):
for attribute, options in pmb.config.apkbuild_attributes.items():
found, value, i = parse_attribute(attribute, lines, i, path)
@ -278,14 +285,6 @@ def apkbuild(args, path, check_pkgver=True, check_pkgname=True):
value = []
ret[attribute] = value
# Add missing keys
for attribute, options in pmb.config.apkbuild_attributes.items():
if attribute not in ret:
if options["array"]:
ret[attribute] = []
else:
ret[attribute] = ""
# Properly format values
ret = replace_variables(ret)
ret = cut_off_function_names(ret)