From d1fadba5b4ac949ccf3271bcb511e9a1aa381bc6 Mon Sep 17 00:00:00 2001 From: Shubham Naik Date: Sun, 31 Jan 2021 23:46:56 +0530 Subject: [PATCH] Enforce E501: Limit the line length to 79 for files in pmb/parse - part 1 (MR 2019) Made changes to limit the line length in following files, - pmb/parse/_apkbuild.py - pmb/parse/apkindex.py - pmb/parse/binfmt_info.py - pmb/parse/deviceinfo.py - test/test_parse_apkbuild.py Added the above files in E501 flake8 command list. Substitute f-string for string concatenation. --- pmb/parse/_apkbuild.py | 71 +++++++++++++++++++----------------- pmb/parse/apkindex.py | 14 ++++--- pmb/parse/binfmt_info.py | 4 +- pmb/parse/deviceinfo.py | 11 +++--- test/static_code_analysis.sh | 5 +++ test/test_parse_apkbuild.py | 9 +++-- 6 files changed, 64 insertions(+), 50 deletions(-) diff --git a/pmb/parse/_apkbuild.py b/pmb/parse/_apkbuild.py index 9a8306d8..88b15056 100644 --- a/pmb/parse/_apkbuild.py +++ b/pmb/parse/_apkbuild.py @@ -25,6 +25,10 @@ revar4 = re.compile(r"\${([a-zA-Z_]+[a-zA-Z0-9_]*)#(.*)}") def replace_variable(apkbuild, value: str) -> str: + def log_key_not_found(match): + logging.verbose(f"{apkbuild['pkgname']}: key '{match.group(1)}' for" + f" replacing '{match.group(0)}' not found, ignoring") + # ${foo} for match in revar.finditer(value): try: @@ -33,9 +37,7 @@ def replace_variable(apkbuild, value: str) -> str: apkbuild[match.group(1)])) value = value.replace(match.group(0), apkbuild[match.group(1)], 1) except KeyError: - logging.verbose("{}: key '{}' for replacing '{}' not found, ignoring" - "".format(apkbuild["pkgname"], match.group(1), - match.group(0))) + log_key_not_found(match) # $foo for match in revar2.finditer(value): @@ -46,9 +48,7 @@ def replace_variable(apkbuild, value: str) -> str: newvalue)) value = value.replace(match.group(0), newvalue, 1) except KeyError: - logging.verbose("{}: key '{}' for replacing '{}' not found, ignoring" - "".format(apkbuild["pkgname"], match.group(1), - match.group(0))) + log_key_not_found(match) # ${var/foo/bar}, ${var/foo/}, ${var/foo} for match in revar3.finditer(value): @@ -63,9 +63,7 @@ def replace_variable(apkbuild, value: str) -> str: apkbuild["pkgname"], match.group(0), newvalue)) value = value.replace(match.group(0), newvalue, 1) except KeyError: - logging.verbose("{}: key '{}' for replacing '{}' not found, ignoring" - "".format(apkbuild["pkgname"], match.group(1), - match.group(0))) + log_key_not_found(match) # ${foo#bar} rematch4 = revar4.finditer(value) @@ -79,9 +77,7 @@ def replace_variable(apkbuild, value: str) -> str: apkbuild["pkgname"], match.group(0), newvalue)) value = value.replace(match.group(0), newvalue, 1) except KeyError: - logging.verbose("{}: key '{}' for replacing '{}' not found, ignoring" - "".format(apkbuild["pkgname"], match.group(1), - match.group(0))) + log_key_not_found(match) return value @@ -121,7 +117,7 @@ def read_file(path): with open(path, encoding="utf-8") as handle: lines = handle.readlines() if handle.newlines != '\n': - raise RuntimeError("Wrong line endings in APKBUILD: " + path) + raise RuntimeError(f"Wrong line endings in APKBUILD: {path}") return lines @@ -179,14 +175,15 @@ def parse_attribute(attribute, lines, i, path): value += line.strip() i += 1 - raise RuntimeError("Can't find closing quote sign (" + end_char + ") for" - " attribute '" + attribute + "' in: " + path) + raise RuntimeError(f"Can't find closing quote sign ({end_char}) for" + f" attribute '{attribute}' in: {path}") def _parse_attributes(path, lines, apkbuild_attributes, ret): """ Parse attributes from a list of lines. Variables are replaced with values - from ret (if found) and split into the format configured in apkbuild_attributes. + from ret (if found) and split into the format configured in + apkbuild_attributes. :param lines: the lines to parse :param apkbuild_attributes: the attributes to parse @@ -245,19 +242,21 @@ def _parse_subpackage(path, lines, apkbuild, subpackages, subpkg): if not start: # Unable to find subpackage function in the APKBUILD. - # The subpackage function could be actually missing, or this is a problem - # in the parser. For now we also don't handle subpackages with default - # functions (e.g. -dev or -doc). + # The subpackage function could be actually missing, or this is a + # problem in the parser. For now we also don't handle subpackages with + # default functions (e.g. -dev or -doc). # In the future we may want to specifically handle these, and throw # an exception here for all other missing subpackage functions. subpackages[subpkgname] = None - logging.verbose("{}: subpackage function '{}' for subpackage '{}' not found, ignoring" - "".format(apkbuild["pkgname"], subpkgsplit, subpkgname)) + logging.verbose( + f"{apkbuild['pkgname']}: subpackage function '{subpkgsplit}' for " + f"subpackage '{subpkgname}' not found, ignoring") return if not end: - raise RuntimeError("Could not find end of subpackage function, no line starts" - " with '}' after '" + prefix + "' in " + path) + raise RuntimeError( + f"Could not find end of subpackage function, no line starts with " + f"'}}' after '{prefix}' in {path}") lines = lines[start:end] # Strip tabs before lines in function @@ -268,7 +267,8 @@ def _parse_subpackage(path, lines, apkbuild, subpackages, subpkg): apkbuild["subpkgname"] = subpkgname # Parse relevant attributes for the subpackage - _parse_attributes(path, lines, pmb.config.apkbuild_package_attributes, apkbuild) + _parse_attributes( + path, lines, pmb.config.apkbuild_package_attributes, apkbuild) # Return only properties interesting for subpackages ret = {} @@ -304,21 +304,23 @@ def apkbuild(args, path, check_pkgver=True, check_pkgname=True): _parse_attributes(path, lines, pmb.config.apkbuild_attributes, ret) # Sanity check: pkgname - suffix = "/" + ret["pkgname"] + "/APKBUILD" + suffix = f"/{ret['pkgname']}/APKBUILD" if check_pkgname: if not os.path.realpath(path).endswith(suffix): - logging.info("Folder: '" + os.path.dirname(path) + "'") - logging.info("Pkgname: '" + ret["pkgname"] + "'") + logging.info(f"Folder: '{os.path.dirname(path)}'") + logging.info(f"Pkgname: '{ret['pkgname']}'") raise RuntimeError("The pkgname must be equal to the name of" " the folder, that contains the APKBUILD!") # Sanity check: pkgver if check_pkgver: - if "-r" in ret["pkgver"] or not pmb.parse.version.validate(ret["pkgver"]): - logging.info("NOTE: Valid pkgvers are described here:") - logging.info("") - raise RuntimeError("Invalid pkgver '" + ret["pkgver"] + - "' in APKBUILD: " + path) + if ("-r" in ret["pkgver"] or not + pmb.parse.version.validate(ret["pkgver"])): + logging.info( + "NOTE: Valid pkgvers are described here: " + "https://wiki.alpinelinux.org/wiki/APKBUILD_Reference#pkgver") + raise RuntimeError(f"Invalid pkgver '{ret['pkgver']}' in" + f" APKBUILD: {path}") # Fill cache args.cache["apkbuild"][path] = ret @@ -344,12 +346,13 @@ def kernels(args, device): # Read kernels from subpackages ret = {} - subpackage_prefix = "device-" + device + "-kernel-" + subpackage_prefix = f"device-{device}-kernel-" for subpkgname, subpkg in subpackages.items(): if not subpkgname.startswith(subpackage_prefix): continue if subpkg is None: - raise RuntimeError("Cannot find subpackage function for: " + subpkgname) + raise RuntimeError( + f"Cannot find subpackage function for: {subpkgname}") name = subpkgname[len(subpackage_prefix):] ret[name] = subpkg["pkgdesc"] diff --git a/pmb/parse/apkindex.py b/pmb/parse/apkindex.py index 8d8bc9c7..f15d462e 100644 --- a/pmb/parse/apkindex.py +++ b/pmb/parse/apkindex.py @@ -71,8 +71,8 @@ def parse_next_block(args, path, lines, start): # Check for required keys for key in ["arch", "pkgname", "version"]: if key not in ret: - raise RuntimeError("Missing required key '" + key + - "' in block " + str(ret) + ", file: " + path) + raise RuntimeError(f"Missing required key '{key}' in block " + f"{ret}, file: {path}") # Format optional lists for key in ["provides", "depends"]: @@ -340,8 +340,9 @@ def provider_highest_priority(providers, pkgname): priority_providers[provider_name] = provider if priority_providers: - logging.debug(f"{pkgname}: picked provider(s) with higest priority {max_priority}: " + - ", ".join(priority_providers.keys())) + logging.debug( + f"{pkgname}: picked provider(s) with higest priority " + f"{max_priority}: {', '.join(priority_providers.keys())}") return priority_providers # None of the providers seems to have a provider_priority defined @@ -359,8 +360,9 @@ def provider_shortest(providers, pkgname): """ ret = min(list(providers.keys()), key=len) if len(providers) != 1: - logging.debug(pkgname + ": has multiple providers (" + - ", ".join(providers.keys()) + "), picked shortest: " + ret) + logging.debug( + f"{pkgname}: has multiple providers (" + f"{', '.join(providers.keys())}), picked shortest: {ret}") return providers[ret] diff --git a/pmb/parse/binfmt_info.py b/pmb/parse/binfmt_info.py index b8579746..5af0f3b9 100644 --- a/pmb/parse/binfmt_info.py +++ b/pmb/parse/binfmt_info.py @@ -26,8 +26,8 @@ def binfmt_info(args, arch_qemu): for type in ["mask", "magic"]: key = arch_qemu + "_" + type if key not in full: - raise RuntimeError("Could not find key " + key + " in binfmt info file: " + - info) + raise RuntimeError( + f"Could not find key {key} in binfmt info file: {info}") ret[type] = full[key] logging.verbose("=> " + str(ret)) return ret diff --git a/pmb/parse/deviceinfo.py b/pmb/parse/deviceinfo.py index 58a742a6..b23b32fa 100644 --- a/pmb/parse/deviceinfo.py +++ b/pmb/parse/deviceinfo.py @@ -48,8 +48,8 @@ def sanity_check(info, path): if codename.startswith("device-"): codename = codename[7:] if "codename" not in info or info["codename"] != codename: - raise RuntimeError("Please add 'deviceinfo_codename=\"" + codename + - "\"' to: " + path) + raise RuntimeError(f"Please add 'deviceinfo_codename=\"{codename}\"' " + f"to: {path}") # "chassis" is required chassis_types = pmb.config.deviceinfo_chassis_types @@ -118,8 +118,9 @@ def deviceinfo(args, device=None, kernel=None): kernel = args.kernel if not os.path.exists(args.aports): - logging.fatal("Aports directory is missing, expected: " + args.aports) - logging.fatal("Please provide a path to the aports directory using the -p flag") + logging.fatal(f"Aports directory is missing, expected: {args.aports}") + logging.fatal("Please provide a path to the aports directory using the" + " -p flag") raise RuntimeError("Aports directory missing") path = pmb.helpers.devices.find_path(args, device, 'deviceinfo') @@ -135,7 +136,7 @@ def deviceinfo(args, device=None, kernel=None): if not line.startswith("deviceinfo_"): continue if "=" not in line: - raise SyntaxError(path + ": No '=' found:\n\t" + line) + raise SyntaxError(f"{path}: No '=' found:\n\t{line}") split = line.split("=", 1) key = split[0][len("deviceinfo_"):] value = split[1].replace("\"", "").replace("\n", "") diff --git a/test/static_code_analysis.sh b/test/static_code_analysis.sh index 47a7a014..aac57967 100755 --- a/test/static_code_analysis.sh +++ b/test/static_code_analysis.sh @@ -92,9 +92,13 @@ py_files=" pmb/install/recovery.py pmb/install/ui.py pmb/parse/__init__.py + pmb/parse/_apkbuild.py + pmb/parse/apkindex.py pmb/parse/arch.py pmb/parse/arguments.py + pmb/parse/binfmt_info.py pmb/parse/cpuinfo.py + pmb/parse/deviceinfo.py pmb/parse/version.py pmb/qemu/__init__.py pmb/sideload/__init__.py @@ -126,6 +130,7 @@ py_files=" test/test_keys.py test/test_mount.py test/test_newapkbuild.py + test/test_parse_apkbuild.py test/test_parse_apkindex.py test/test_parse_deviceinfo.py test/test_questions.py diff --git a/test/test_parse_apkbuild.py b/test/test_parse_apkbuild.py index 8b6485b1..a7e7a582 100644 --- a/test/test_parse_apkbuild.py +++ b/test/test_parse_apkbuild.py @@ -37,13 +37,15 @@ def test_subpackages(args): path = (testdata + "/init_questions_device/aports/device/testing/" "device-nonfree-firmware/APKBUILD") apkbuild = pmb.parse.apkbuild(args, path) - subpkg = apkbuild["subpackages"]["device-nonfree-firmware-nonfree-firmware"] + subpkg = (apkbuild["subpackages"] + ["device-nonfree-firmware-nonfree-firmware"]) assert subpkg["pkgdesc"] == "firmware description" # Can't find the pkgdesc in the function path = testdata + "/apkbuild/APKBUILD.missing-pkgdesc-in-subpackage" apkbuild = pmb.parse.apkbuild(args, path, check_pkgname=False) - subpkg = apkbuild["subpackages"]["missing-pkgdesc-in-subpackage-subpackage"] + subpkg = (apkbuild["subpackages"] + ["missing-pkgdesc-in-subpackage-subpackage"]) assert subpkg["pkgdesc"] == "" # Can't find the function @@ -131,4 +133,5 @@ def test_variable_replacements(args): assert apkbuild["subpackages"]["replacements"] is None test_subpkg = apkbuild["subpackages"]["test"] - assert test_subpkg["pkgdesc"] == "this should not affect variable replacement" + assert test_subpkg["pkgdesc"] == ("this should not affect variable " + "replacement")