From d7f61f6475c0cdf82dc0ec957050f651f0d57a78 Mon Sep 17 00:00:00 2001 From: InsanePrawn Date: Tue, 30 Aug 2022 00:20:04 +0200 Subject: [PATCH] packages: move filter_packages() to pkgbuild, rename to filter_pkgbuilds() --- packages/__init__.py | 44 +++----------------------------------------- packages/pkgbuild.py | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/packages/__init__.py b/packages/__init__.py index 1203493..ad46088 100644 --- a/packages/__init__.py +++ b/packages/__init__.py @@ -22,7 +22,7 @@ from ssh import run_ssh_command, scp_put_files from wrapper import enforce_wrap, check_programs_wrap, wrap_if_foreign_arch from utils import git -from .pkgbuild import discover_pkgbuilds, init_pkgbuilds, Pkgbuild +from .pkgbuild import discover_pkgbuilds, filter_pkgbuilds, init_pkgbuilds, Pkgbuild from .device import get_profile_device pacman_cmd = [ @@ -77,44 +77,6 @@ def init_prebuilts(arch: Arch, dir: str = None): raise Exception(f'Failed to create local repo {repo}') -def filter_packages( - paths: Iterable[str], - repo: Optional[dict[str, Pkgbuild]] = None, - arch: Optional[Arch] = None, - allow_empty_results=True, - use_paths=True, - use_names=True, -) -> Iterable[Pkgbuild]: - if not (use_names or use_paths): - raise Exception('Error: filter_packages instructed to match neither by names nor paths; impossible!') - if not allow_empty_results and not paths: - raise Exception("Can't search for packages: no query given") - repo = repo or discover_pkgbuilds() - if 'all' in paths: - all_pkgs = list(repo.values()) - if arch: - all_pkgs = [pkg for pkg in all_pkgs if set([arch, 'any']).intersection(pkg.arches)] - return all_pkgs - result = [] - for pkg in repo.values(): - comparison = set() - if use_paths: - comparison.add(pkg.path) - if use_names: - comparison.add(pkg.name) - matches = list(comparison.intersection(paths)) - if matches: - assert pkg.arches - if arch and not set([arch, 'any']).intersection(pkg.arches): - logging.warn(f"Pkg {pkg.name} matches query {matches[0]} but isn't available for architecture {arch}: {pkg.arches}") - continue - result += [pkg] - - if not allow_empty_results and not result: - raise Exception('No packages matched by paths: ' + ', '.join([f'"{p}"' for p in paths])) - return result - - def generate_dependency_chain(package_repo: dict[str, Pkgbuild], to_build: Iterable[Pkgbuild]) -> list[set[Pkgbuild]]: """ This figures out all dependencies and their sub-dependencies for the selection and adds those packages to the selection. @@ -649,7 +611,7 @@ def build_packages_by_paths( assert config.runtime.arch for _arch in set([arch, config.runtime.arch]): init_prebuilts(_arch) - packages = filter_packages(paths, arch=arch, repo=repo, allow_empty_results=False) + packages = filter_pkgbuilds(paths, arch=arch, repo=repo, allow_empty_results=False) return build_packages( packages, arch, @@ -858,7 +820,7 @@ def cmd_check(paths): return False paths = list(paths) - packages = filter_packages(paths, allow_empty_results=False) + packages = filter_pkgbuilds(paths, allow_empty_results=False) for package in packages: name = package.name diff --git a/packages/pkgbuild.py b/packages/pkgbuild.py index f0e7af5..4c02c54 100644 --- a/packages/pkgbuild.py +++ b/packages/pkgbuild.py @@ -7,7 +7,7 @@ import os import subprocess from joblib import Parallel, delayed -from typing import Optional +from typing import Iterable, Optional from config import config, ConfigStateHolder from constants import REPOSITORIES @@ -358,3 +358,41 @@ def discover_pkgbuilds(parallel: bool = True, lazy: bool = True) -> dict[str, Pk _pkgbuilds_cache.update(packages) _pkgbuilds_scanned = True return packages + + +def filter_pkgbuilds( + paths: Iterable[str], + repo: Optional[dict[str, Pkgbuild]] = None, + arch: Optional[Arch] = None, + allow_empty_results=True, + use_paths=True, + use_names=True, +) -> Iterable[Pkgbuild]: + if not (use_names or use_paths): + raise Exception('Error: filter_packages instructed to match neither by names nor paths; impossible!') + if not allow_empty_results and not paths: + raise Exception("Can't search for packages: no query given") + repo = repo or discover_pkgbuilds() + if 'all' in paths: + all_pkgs = list(repo.values()) + if arch: + all_pkgs = [pkg for pkg in all_pkgs if set([arch, 'any']).intersection(pkg.arches)] + return all_pkgs + result = [] + for pkg in repo.values(): + comparison = set() + if use_paths: + comparison.add(pkg.path) + if use_names: + comparison.add(pkg.name) + matches = list(comparison.intersection(paths)) + if matches: + assert pkg.arches + if arch and not set([arch, 'any']).intersection(pkg.arches): + logging.warn(f"Pkg {pkg.name} matches query {matches[0]} but isn't available for architecture {arch}: {pkg.arches}") + continue + result += [pkg] + + if not allow_empty_results and not result: + raise Exception('No packages matched by paths: ' + ', '.join([f'"{p}"' for p in paths])) + return result