From 684cb3e1fbe76c39f7cda3c0109907c51d68c450 Mon Sep 17 00:00:00 2001 From: Shubham Naik Date: Sun, 31 Jan 2021 23:51:56 +0530 Subject: [PATCH] Enforce E501: Limit the line length to 79 for files in pmb/parse - part 2 (MR 2020) Made changes to limit the line length in following files, - pmb/parse/bootimg.py - pmb/parse/depends.py - pmb/parse/kconfig.py - test/test_parse_depends.py Added the above files in E501 flake8 command list. Substitute f-string for string concatenation. --- pmb/parse/bootimg.py | 42 +++++++++++++++++++++--------------- pmb/parse/depends.py | 36 +++++++++++++++---------------- pmb/parse/kconfig.py | 25 +++++++++++---------- test/static_code_analysis.sh | 4 ++++ test/test_parse_depends.py | 3 ++- 5 files changed, 63 insertions(+), 47 deletions(-) diff --git a/pmb/parse/bootimg.py b/pmb/parse/bootimg.py index aa0a2743..f6924197 100644 --- a/pmb/parse/bootimg.py +++ b/pmb/parse/bootimg.py @@ -25,9 +25,9 @@ def has_mtk_header(path, supported_label): # this, deviceinfo would need to store the label and # postmarketos-mkinitfs would need to use that label. if label != supported_label: - raise RuntimeError(f"Only '{supported_label}' is supported as label," - f" but your device has '{label}'. Please create" - f" an issue and attach your boot.img:" + raise RuntimeError(f"Only '{supported_label}' is supported as" + f" label, but your device has '{label}'. Please" + f" create an issue and attach your boot.img:" f" https://postmarketos.org/issues") return True @@ -36,12 +36,12 @@ def bootimg(args, path): if not os.path.exists(path): raise RuntimeError("Could not find file '" + path + "'") - logging.info("NOTE: You will be prompted for your sudo password, so we can set" - " up a chroot to extract and analyze your boot.img file") + logging.info("NOTE: You will be prompted for your sudo password, so we can" + " set up a chroot to extract and analyze your boot.img file") pmb.chroot.apk.install(args, ["file", "unpackbootimg"]) temp_path = pmb.chroot.other.tempfolder(args, "/tmp/bootimg_parser") - bootimg_path = args.work + "/chroot_native" + temp_path + "/boot.img" + bootimg_path = f"{args.work}/chroot_native{temp_path}/boot.img" # Copy the boot.img into the chroot temporary folder pmb.helpers.run.root(args, ["cp", path, bootimg_path]) @@ -69,32 +69,40 @@ def bootimg(args, path): file_output + ")") # Extract all the files - pmb.chroot.user(args, ["unpackbootimg", "-i", "boot.img"], working_dir=temp_path) + pmb.chroot.user(args, ["unpackbootimg", "-i", "boot.img"], + working_dir=temp_path) output = {} # Get base, offsets, pagesize, cmdline and qcdt info with open(bootimg_path + "-base", 'r') as f: output["base"] = ("0x%08x" % int(f.read().replace('\n', ''), 16)) with open(bootimg_path + "-kernel_offset", 'r') as f: - output["kernel_offset"] = ("0x%08x" % int(f.read().replace('\n', ''), 16)) + output["kernel_offset"] = ("0x%08x" + % int(f.read().replace('\n', ''), 16)) with open(bootimg_path + "-ramdisk_offset", 'r') as f: - output["ramdisk_offset"] = ("0x%08x" % int(f.read().replace('\n', ''), 16)) + output["ramdisk_offset"] = ("0x%08x" + % int(f.read().replace('\n', ''), 16)) with open(bootimg_path + "-second_offset", 'r') as f: - output["second_offset"] = ("0x%08x" % int(f.read().replace('\n', ''), 16)) + output["second_offset"] = ("0x%08x" + % int(f.read().replace('\n', ''), 16)) with open(bootimg_path + "-tags_offset", 'r') as f: - output["tags_offset"] = ("0x%08x" % int(f.read().replace('\n', ''), 16)) + output["tags_offset"] = ("0x%08x" + % int(f.read().replace('\n', ''), 16)) with open(bootimg_path + "-pagesize", 'r') as f: output["pagesize"] = f.read().replace('\n', '') with open(bootimg_path + "-cmdline", 'r') as f: output["cmdline"] = f.read().replace('\n', '') - output["qcdt"] = ("true" if os.path.isfile(bootimg_path + "-dt") and - os.path.getsize(bootimg_path + "-dt") > 0 else "false") - output["mtk_mkimage"] = ("true" if has_mtk_header(bootimg_path + "-zImage", "KERNEL") else "false") - output["dtb_second"] = ("true" if is_dtb(bootimg_path + "-second") else "false") + output["qcdt"] = ("true" if os.path.isfile(f"{bootimg_path}-dt") and + os.path.getsize(f"{bootimg_path}-dt") > 0 else "false") + output["mtk_mkimage"] = ("true" if has_mtk_header(f"{bootimg_path}-zImage", + "KERNEL") else "false") + output["dtb_second"] = ("true" if is_dtb(f"{bootimg_path}-second") + else "false") # Mediatek: Check that the ramdisk also has a known-good label - # We don't care about the return value, just whether it throws an exception or not. - has_mtk_header(bootimg_path + "-ramdisk.gz", "ROOTFS") + # We don't care about the return value, just whether it throws an exception + # or not. + has_mtk_header(f"{bootimg_path}-ramdisk.gz", "ROOTFS") # Cleanup pmb.chroot.root(args, ["rm", "-r", temp_path]) diff --git a/pmb/parse/depends.py b/pmb/parse/depends.py index e72a2bb1..00d6a1fa 100644 --- a/pmb/parse/depends.py +++ b/pmb/parse/depends.py @@ -24,8 +24,8 @@ def package_from_aports(args, pkgname_depend): version = apkbuild["pkgver"] + "-r" + apkbuild["pkgrel"] # Return the dict - logging.verbose(pkgname_depend + ": provided by: " + pkgname + "-" + - version + " in " + aport) + logging.verbose( + f"{pkgname_depend}: provided by: {pkgname}-{version} in {aport}") return {"pkgname": pkgname, "depends": apkbuild["depends"], "version": version} @@ -46,35 +46,35 @@ def package_provider(args, pkgname, pkgnames_install, suffix="native"): return None # 1. Only one provider - logging.verbose(pkgname + ": provided by: " + ", ".join(providers)) + logging.verbose(f"{pkgname}: provided by: {', '.join(providers)}") if len(providers) == 1: return list(providers.values())[0] # 2. Provider with the same package name if pkgname in providers: - logging.verbose(pkgname + ": choosing package of the same name as" - " provider") + logging.verbose(f"{pkgname}: choosing package of the same name as " + "provider") return providers[pkgname] # 3. Pick a package that will be installed anyway for provider_pkgname, provider in providers.items(): if provider_pkgname in pkgnames_install: - logging.verbose(pkgname + ": choosing provider '" + - provider_pkgname + "', because it will be" - " installed anyway") + logging.verbose(f"{pkgname}: choosing provider '{provider_pkgname}" + "', because it will be installed anyway") return provider # 4. Pick a package that is already installed installed = pmb.chroot.apk.installed(args, suffix) for provider_pkgname, provider in providers.items(): if provider_pkgname in installed: - logging.verbose(pkgname + ": choosing provider '" + - provider_pkgname + "', because it is installed in" - " the '" + suffix + "' chroot already") + logging.verbose(f"{pkgname}: choosing provider '{provider_pkgname}" + f"', because it is installed in the '{suffix}' " + "chroot already") return provider # 5. Pick the provider(s) with the highest priority - providers = pmb.parse.apkindex.provider_highest_priority(providers, pkgname) + providers = pmb.parse.apkindex.provider_highest_priority( + providers, pkgname) if len(providers) == 1: return list(providers.values())[0] @@ -118,8 +118,8 @@ def recurse(args, pkgnames, suffix="native"): :returns: list of pkgnames: consists of the initial pkgnames plus all depends """ - logging.debug("(" + suffix + ") calculate depends of " + - ", ".join(pkgnames) + " (pmbootstrap -v for details)") + logging.debug(f"({suffix}) calculate depends of {', '.join(pkgnames)} " + "(pmbootstrap -v for details)") # Iterate over todo-list until is is empty todo = list(pkgnames) @@ -138,17 +138,17 @@ def recurse(args, pkgnames, suffix="native"): # Nothing found if not package: - raise RuntimeError("Could not find dependency '" + pkgname_depend + - "' in any aports folder or APKINDEX. See:" + raise RuntimeError(f"Could not find dependency '{pkgname_depend}' " + "in any aports folder or APKINDEX. See:" " ") # Append to todo/ret (unless it is a duplicate) pkgname = package["pkgname"] if pkgname in ret: - logging.verbose(pkgname + ": already found") + logging.verbose(f"{pkgname}: already found") else: depends = package["depends"] - logging.verbose(pkgname + ": depends on: " + ",".join(depends)) + logging.verbose(f"{pkgname}: depends on: {','.join(depends)}") if depends: todo += depends ret.append(pkgname) diff --git a/pmb/parse/kconfig.py b/pmb/parse/kconfig.py index f4d4aa8e..9ae48908 100644 --- a/pmb/parse/kconfig.py +++ b/pmb/parse/kconfig.py @@ -31,7 +31,8 @@ def is_in_array(config, option, string): return False -def check_option(component, details, config, config_path_pretty, option, option_value): +def check_option(component, details, config, config_path_pretty, option, + option_value): link = (f"https://wiki.postmarketos.org/wiki/kconfig#CONFIG_{option}") warning_no_details = (f"WARNING: {config_path_pretty} isn't" f" configured properly for {component}, run" @@ -86,15 +87,16 @@ def check_config(config_path, config_path_pretty, config_arch, pkgver, for archs, options in archs_options.items(): if archs != "all": - # Split and check if the device's architecture architecture has special config - # options. If option does not contain the architecture of the device - # kernel, then just skip the option. + # Split and check if the device's architecture architecture has + # special config options. If option does not contain the + # architecture of the device kernel, then just skip the option. architectures = archs.split(" ") if config_arch not in architectures: continue for option, option_value in options.items(): - if not check_option(component, details, config, config_path_pretty, option, option_value): + if not check_option(component, details, config, + config_path_pretty, option, option_value): ret = False if not details: break # do not give too much error messages @@ -120,12 +122,13 @@ def check(args, pkgname, force_anbox_check=False, details=False): aport = pmb.helpers.pmaports.find(args, "linux-" + flavor) apkbuild = pmb.parse.apkbuild(args, aport + "/APKBUILD") pkgver = apkbuild["pkgver"] - check_anbox = force_anbox_check or ("pmb:kconfigcheck-anbox" in apkbuild["options"]) + check_anbox = force_anbox_check or ( + "pmb:kconfigcheck-anbox" in apkbuild["options"]) for config_path in glob.glob(aport + "/config-*"): # The architecture of the config is in the name, so it just needs to be # extracted config_arch = os.path.basename(config_path).split(".")[1] - config_path_pretty = "linux-" + flavor + "/" + os.path.basename(config_path) + config_path_pretty = f"linux-{flavor}/{os.path.basename(config_path)}" ret &= check_config(config_path, config_path_pretty, config_arch, pkgver, details=details) if check_anbox: @@ -174,11 +177,11 @@ def check_file(args, config_file, anbox=False, details=False): """ arch = extract_arch(config_file) version = extract_version(config_file) - logging.debug("Check kconfig: parsed arch=" + arch + ", version=" + - version + " from file: " + config_file) + logging.debug(f"Check kconfig: parsed arch={arch}, version={version} from " + "file: {config_file}") ret = check_config(config_file, config_file, arch, version, anbox=False, details=details) if anbox: - ret &= check_config(config_file, config_file, arch, version, anbox=True, - details=details) + ret &= check_config(config_file, config_file, arch, version, + anbox=True, details=details) return ret diff --git a/test/static_code_analysis.sh b/test/static_code_analysis.sh index aac57967..81cbedde 100755 --- a/test/static_code_analysis.sh +++ b/test/static_code_analysis.sh @@ -97,8 +97,11 @@ py_files=" pmb/parse/arch.py pmb/parse/arguments.py pmb/parse/binfmt_info.py + pmb/parse/bootimg.py pmb/parse/cpuinfo.py + pmb/parse/depends.py pmb/parse/deviceinfo.py + pmb/parse/kconfig.py pmb/parse/version.py pmb/qemu/__init__.py pmb/sideload/__init__.py @@ -132,6 +135,7 @@ py_files=" test/test_newapkbuild.py test/test_parse_apkbuild.py test/test_parse_apkindex.py + test/test_parse_depends.py test/test_parse_deviceinfo.py test/test_questions.py test/test_run_core.py diff --git a/test/test_parse_depends.py b/test/test_parse_depends.py index de0ba773..520b907f 100644 --- a/test/test_parse_depends.py +++ b/test/test_parse_depends.py @@ -75,7 +75,8 @@ def test_package_provider(args, monkeypatch): # 5. Pick package with highest priority package_with_priority = {"pkgname": "test-priority", "provides": ["test"], "provider_priority": 100} - providers = {"test-two": package_two, "test-priority": package_with_priority} + providers = {"test-two": package_two, + "test-priority": package_with_priority} assert func(args, pkgname, pkgnames_install) == package_with_priority # 6. Pick the first one