pmb: Use unpacking operator to concatenate collections (MR 2525)

See https://docs.astral.sh/ruff/rules/collection-literal-concatenation

This is also slightly faster according to a microbenchmark that
shows it taking around 19% less time to run:
https://github.com/astral-sh/ruff/pull/1957#issue-1538092351
This commit is contained in:
Newbyte 2025-01-08 13:57:34 +01:00 committed by Oliver Smith
parent 4868cc9548
commit c797b30dfe
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
14 changed files with 29 additions and 33 deletions

View file

@ -87,7 +87,7 @@ def get_depends(context: Context, apkbuild: dict[str, Any]) -> list[str]:
ret = sorted(set(ret)) ret = sorted(set(ret))
# Don't recurse forever when a package depends on itself (#948) # Don't recurse forever when a package depends on itself (#948)
for pkgname in [apkbuild["pkgname"]] + list(apkbuild["subpackages"].keys()): for pkgname in [apkbuild["pkgname"], *apkbuild["subpackages"].keys()]:
if pkgname in ret: if pkgname in ret:
logging.verbose(apkbuild["pkgname"] + ": ignoring dependency on" " itself: " + pkgname) logging.verbose(apkbuild["pkgname"] + ": ignoring dependency on" " itself: " + pkgname)
ret.remove(pkgname) ret.remove(pkgname)
@ -224,7 +224,7 @@ class BuildQueueItem(TypedDict):
def has_cyclical_dependency( def has_cyclical_dependency(
unmet_deps: dict[str, list[str]], item: BuildQueueItem, dep: str unmet_deps: dict[str, list[str]], item: BuildQueueItem, dep: str
) -> bool: ) -> bool:
pkgnames = [item["name"]] + list(item["apkbuild"]["subpackages"].keys()) pkgnames = [item["name"], *item["apkbuild"]["subpackages"].keys()]
for pkgname in pkgnames: for pkgname in pkgnames:
if pkgname in unmet_deps.get(dep, []): if pkgname in unmet_deps.get(dep, []):

View file

@ -24,7 +24,7 @@ def newapkbuild(folder: PathString, args_passed: list[str], force: bool = False)
pmb.chroot.user(["mkdir", "-p", build]) pmb.chroot.user(["mkdir", "-p", build])
# Run newapkbuild # Run newapkbuild
pmb.chroot.user(["newapkbuild"] + args_passed, working_dir=build) pmb.chroot.user(["newapkbuild", *args_passed], working_dir=build)
glob_result = list(build_outside.glob("*/APKBUILD")) glob_result = list(build_outside.glob("*/APKBUILD"))
if not len(glob_result): if not len(glob_result):
return return

View file

@ -149,18 +149,18 @@ def install_run_apk(
if package.startswith("-"): if package.startswith("-"):
raise ValueError(f"Invalid package name: {package}") raise ValueError(f"Invalid package name: {package}")
commands: list[Sequence[PathString]] = [["add"] + to_add] commands: list[Sequence[PathString]] = [["add", *to_add]]
# Use a virtual package to mark only the explicitly requested packages as # Use a virtual package to mark only the explicitly requested packages as
# explicitly installed, not the ones in to_add_local # explicitly installed, not the ones in to_add_local
if to_add_local: if to_add_local:
commands += [ commands += [
["add", "-u", "--virtual", ".pmbootstrap"] + local_add, ["add", "-u", "--virtual", ".pmbootstrap", *local_add],
["del", ".pmbootstrap"], ["del", ".pmbootstrap"],
] ]
if to_del: if to_del:
commands += [["del"] + to_del] commands += [["del", *to_del]]
channel = pmb.config.pmaports.read_config()["channel"] channel = pmb.config.pmaports.read_config()["channel"]
# There are still some edgecases where we manage to get here while the chroot is not # There are still some edgecases where we manage to get here while the chroot is not
@ -182,7 +182,7 @@ def install_run_apk(
# Ignore missing repos before initial build (bpo#137) # Ignore missing repos before initial build (bpo#137)
if os.getenv("PMB_APK_FORCE_MISSING_REPOSITORIES") == "1": if os.getenv("PMB_APK_FORCE_MISSING_REPOSITORIES") == "1":
command = ["--force-missing-repositories"] + command command = ["--force-missing-repositories", *command]
# Virtual package related commands don't actually install or remove # Virtual package related commands don't actually install or remove
# packages, but only mark the right ones as explicitly installed. # packages, but only mark the right ones as explicitly installed.

View file

@ -158,7 +158,7 @@ def init(chroot: Chroot, usr_merge: UsrMerge = UsrMerge.AUTO) -> None:
pmb.helpers.repo.update(arch) pmb.helpers.repo.update(arch)
pkgs = ["alpine-base"] pkgs = ["alpine-base"]
cmd: list[PathString] = ["--initdb"] cmd: list[PathString] = ["--initdb"]
pmb.helpers.apk.run(cmd + ["add", *pkgs], chroot) pmb.helpers.apk.run([*cmd, "add", *pkgs], chroot)
# Merge /usr # Merge /usr
if usr_merge is UsrMerge.AUTO and pmb.config.is_systemd_selected(config): if usr_merge is UsrMerge.AUTO and pmb.config.is_systemd_selected(config):

View file

@ -178,5 +178,5 @@ def remove_mnt_pmbootstrap(chroot: Chroot) -> None:
if not mnt_dir.exists(): if not mnt_dir.exists():
return return
for path in list(mnt_dir.glob("*")) + [mnt_dir]: for path in [*mnt_dir.glob("*"), mnt_dir]:
pmb.helpers.run.root(["rmdir", path]) pmb.helpers.run.root(["rmdir", path])

View file

@ -24,7 +24,7 @@ def kernel_flavor_installed(chroot: Chroot, autoinstall: bool = True) -> str | N
if not chroot.is_mounted(): if not chroot.is_mounted():
pmb.chroot.init(chroot) pmb.chroot.init(chroot)
config = get_context().config config = get_context().config
packages = [f"device-{config.device}"] + pmb.install.get_kernel_package(config) packages = [f"device-{config.device}", *pmb.install.get_kernel_package(config)]
pmb.chroot.apk.install(packages, chroot) pmb.chroot.apk.install(packages, chroot)
glob_result = list((chroot / "usr/share/kernel").glob("*")) glob_result = list((chroot / "usr/share/kernel").glob("*"))

View file

@ -76,7 +76,7 @@ def rev_parse(
:returns: commit string like "90cd0ad84d390897efdcf881c0315747a4f3a966" :returns: commit string like "90cd0ad84d390897efdcf881c0315747a4f3a966"
or (with ``--abbrev-ref``): the branch name, e.g. "master" or (with ``--abbrev-ref``): the branch name, e.g. "master"
""" """
command = ["git", "rev-parse"] + extra_args + [revision] command = ["git", "rev-parse", *extra_args, revision]
rev = pmb.helpers.run.user_output(command, path, output="null" if silent else "log") rev = pmb.helpers.run.user_output(command, path, output="null" if silent else "log")
return rev.rstrip() return rev.rstrip()

View file

@ -96,7 +96,7 @@ def check(pkgnames: Sequence[str]) -> None:
has_failed = False has_failed = False
for pkgrepo, apkbuild_paths in apkbuilds.items(): for pkgrepo, apkbuild_paths in apkbuilds.items():
if pmb.chroot.user( if pmb.chroot.user(
["apkbuild-lint"] + apkbuild_paths, ["apkbuild-lint", *apkbuild_paths],
check=False, check=False,
output="stdout", output="stdout",
working_dir=dest_paths[pkgrepo_name(repo)], working_dir=dest_paths[pkgrepo_name(repo)],

View file

@ -88,7 +88,7 @@ def urls(
mirrors_exclude.append("systemd") mirrors_exclude.append("systemd")
# ["pmaports", "systemd", "alpine", "plasma-nightly"] # ["pmaports", "systemd", "alpine", "plasma-nightly"]
for repo in pkgrepo_names() + ["alpine"]: for repo in [*pkgrepo_names(), "alpine"]:
if repo in mirrors_exclude: if repo in mirrors_exclude:
continue continue

View file

@ -82,7 +82,7 @@ def generate(arch: Arch) -> list[dict[str, list[str] | str | None]]:
# Add abuild to depends if needed # Add abuild to depends if needed
if pkgname != "abuild" and is_abuild_forked(repo): if pkgname != "abuild" and is_abuild_forked(repo):
depends = ["abuild"] + depends depends = ["abuild", *depends]
depends = sorted(depends) depends = sorted(depends)

View file

@ -149,11 +149,11 @@ def copy_files_from_chroot(args: PmbArgs, chroot: Chroot) -> None:
if args.verbose: if args.verbose:
rsync_flags += "vP" rsync_flags += "vP"
pmb.chroot.root( pmb.chroot.root(
["rsync", rsync_flags, "--delete"] + folders + ["/mnt/install/"], working_dir=mountpoint ["rsync", rsync_flags, "--delete", *folders, "/mnt/install/"], working_dir=mountpoint
) )
pmb.chroot.root(["rm", "-rf", "/mnt/install/home"]) pmb.chroot.root(["rm", "-rf", "/mnt/install/home"])
else: else:
pmb.chroot.root(["cp", "-a"] + folders + ["/mnt/install/"], working_dir=mountpoint) pmb.chroot.root(["cp", "-a", *folders, "/mnt/install/"], working_dir=mountpoint)
def create_home_from_skel(filesystem: str, user: str) -> None: def create_home_from_skel(filesystem: str, user: str) -> None:
@ -1113,11 +1113,12 @@ def install_on_device_installer(args: PmbArgs, step: int, steps: int) -> None:
# Prepare the installer chroot # Prepare the installer chroot
logging.info(f"*** ({step}/{steps}) CREATE ON-DEVICE INSTALLER ROOTFS ***") logging.info(f"*** ({step}/{steps}) CREATE ON-DEVICE INSTALLER ROOTFS ***")
step += 1 step += 1
packages = ( packages = [
[f"device-{config.device}", "postmarketos-ondev"] f"device-{config.device}",
+ get_kernel_package(config) "postmarketos-ondev",
+ get_nonfree_packages(config.device) *get_kernel_package(config),
) *get_nonfree_packages(config.device),
]
chroot_installer = Chroot(ChrootType.INSTALLER, config.device) chroot_installer = Chroot(ChrootType.INSTALLER, config.device)
pmb.chroot.apk.install(packages, chroot_installer) pmb.chroot.apk.install(packages, chroot_installer)
@ -1303,7 +1304,7 @@ def create_device_rootfs(args: PmbArgs, step: int, steps: int) -> None:
set_user(context.config) set_user(context.config)
# Fill install_packages # Fill install_packages
install_packages = pmb.config.install_device_packages + ["device-" + device] install_packages = [*pmb.config.install_device_packages, "device-" + device]
if not args.install_base: if not args.install_base:
install_packages = [p for p in install_packages if p != "postmarketos-base"] install_packages = [p for p in install_packages if p != "postmarketos-base"]
if config.ui.lower() != "none": if config.ui.lower() != "none":

View file

@ -164,7 +164,7 @@ def format_and_mount_root(
# with non-block devices, we need to explicitly set a number of # with non-block devices, we need to explicitly set a number of
# inodes. See #1717 and #1845 for details # inodes. See #1717 and #1845 for details
if not disk: if not disk:
mkfs_root_args = mkfs_root_args + ["-N", "100000"] mkfs_root_args = [*mkfs_root_args, "-N", "100000"]
elif filesystem == "f2fs": elif filesystem == "f2fs":
mkfs_root_args = ["mkfs.f2fs", "-f", "-l", root_label] mkfs_root_args = ["mkfs.f2fs", "-f", "-l", root_label]
elif filesystem == "btrfs": elif filesystem == "btrfs":
@ -174,7 +174,7 @@ def format_and_mount_root(
install_fsprogs(filesystem) install_fsprogs(filesystem)
logging.info(f"(native) format {device} (root, {filesystem})") logging.info(f"(native) format {device} (root, {filesystem})")
pmb.chroot.root(mkfs_root_args + [device]) pmb.chroot.root([*mkfs_root_args, device])
# Mount # Mount
mountpoint = "/mnt/install" mountpoint = "/mnt/install"

View file

@ -120,7 +120,7 @@ def partition(layout: PartitionLayout, size_boot: int, size_reserve: int) -> Non
commands += [["set", str(layout["boot"]), "boot", "on"]] commands += [["set", str(layout["boot"]), "boot", "on"]]
for command in commands: for command in commands:
pmb.chroot.root(["parted", "-s", "/dev/install"] + command, check=False) pmb.chroot.root(["parted", "-s", "/dev/install", *command], check=False)
def partition_cgpt(layout: PartitionLayout, size_boot: int, size_reserve: int) -> None: def partition_cgpt(layout: PartitionLayout, size_boot: int, size_reserve: int) -> None:

View file

@ -77,18 +77,13 @@ def ssh_install_apks(args: PmbArgs, user: str, host: str, port: str, paths: list
remote_paths.append(os.path.join("/tmp", os.path.basename(path))) remote_paths.append(os.path.join("/tmp", os.path.basename(path)))
logging.info(f"Copying packages to {user}@{host}") logging.info(f"Copying packages to {user}@{host}")
command = ["scp", "-P", port] + paths + [f"{user}@{host}:/tmp"] command: list[PathString] = ["scp", "-P", port, *paths, f"{user}@{host}:/tmp"]
pmb.helpers.run.user(command, output="interactive") pmb.helpers.run.user(command, output="interactive")
logging.info(f"Installing packages at {user}@{host}") logging.info(f"Installing packages at {user}@{host}")
add_cmd_list = [ add_cmd_list = ["apk", "--wait", "30", "add", *remote_paths]
"apk",
"--wait",
"30",
"add",
] + remote_paths
add_cmd = pmb.helpers.run_core.flat_cmd([add_cmd_list]) add_cmd = pmb.helpers.run_core.flat_cmd([add_cmd_list])
clean_cmd = pmb.helpers.run_core.flat_cmd([["rm"] + remote_paths]) clean_cmd = pmb.helpers.run_core.flat_cmd([["rm", *remote_paths]])
add_cmd_complete = shlex.quote(f"{su_cmd} {add_cmd} rc=$?; {clean_cmd} exit $rc") add_cmd_complete = shlex.quote(f"{su_cmd} {add_cmd} rc=$?; {clean_cmd} exit $rc")
# Run apk command in a subshell in case the foreign device has a non-POSIX shell. # Run apk command in a subshell in case the foreign device has a non-POSIX shell.
command = ["ssh", "-t", "-p", port, f"{user}@{host}", f"sh -c {add_cmd_complete}"] command = ["ssh", "-t", "-p", port, f"{user}@{host}", f"sh -c {add_cmd_complete}"]