From 4d6ab78b41d57d1aad31544906814c75649ca5a1 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 19 Nov 2022 13:55:18 +0000 Subject: [PATCH] pakfire-builder: Make search functions re-use some code Signed-off-by: Michael Tremer --- src/scripts/pakfire-builder.in | 96 +++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 26 deletions(-) diff --git a/src/scripts/pakfire-builder.in b/src/scripts/pakfire-builder.in index 25e3de7c7..6dee24fb2 100644 --- a/src/scripts/pakfire-builder.in +++ b/src/scripts/pakfire-builder.in @@ -83,7 +83,8 @@ class Cli(object): help=_("Print some information about the given package(s)")) info.add_argument("--filelist", action="store_true", help=_("Show filelist")) - info.add_argument("package", help=_("Give at least the name of one package.")) + info.add_argument("package", nargs="+", + help=_("Give at least the name of one package.")) info.set_defaults(func=self._info) # provides @@ -96,7 +97,8 @@ class Cli(object): # requires requires = subparsers.add_parser("requires", help=_("Get a list of packages that require a given file or feature")) - requires.add_argument("pattern", help=_("File or feature to search for")) + requires.add_argument("pattern", nargs="+", + help=_("File or feature to search for")) requires.set_defaults(func=self._requires) # repolist @@ -232,33 +234,80 @@ class Cli(object): for pkg in pkgs: p.dist(pkg, resultdir) + # Some common functions + + def _print_packages(self, packages, unique=True, long=True, **kwargs): + """ + This is a helper function that prints the result of a search + """ + # Make all packages only appear once in the result + if unique: + packages = set(packages) + + # Walk through all packages in order + for package in sorted(packages): + # Dump all metadata + s = package.dump(long=long, **kwargs) + + # Print the result + print(s) + + def _search_packages(self, func, *args, **kwargs): + """ + This is a helper function that performs a search for packages + """ + packages = [] + + # Search and store the results + for arg in args: + packages += func(arg, **kwargs) + + return packages + def _info(self, ns): """ Shows information about a certain package """ p = self.pakfire(ns) - pkgs = [] + archives = [] + patterns = [] - # Try to open a file at this name - try: - archive = p.open(ns.package) + # Find all archives + for package in ns.package: + print(package) + try: + archive = p.open(package) + archives.append(archive) - pkgs.append(archive.get_package()) + # If we could not open the file, we will search for the pattern later + except FileNotFoundError: + patterns.append(package) - # If there is no file, or it could not be opened, - # search for a package with a matching name... - except FileNotFoundError: - pkgs += p.search(ns.package, name_only=True) + packages = [] - for pkg in sorted(pkgs): - s = pkg.dump(long=True, filelist=ns.filelist) - print(s) + # Perform the pattern search + if patterns: + packages += self._search_packages(p.search, *patterns, name_only=True) + + # Append all packages found in the archives + if archives: + packages += (a.get_package() for a in archives) + + # Print the result + self._print_packages(packages) def _requires(self, ns): - for pkg in self.pakfire(ns).whatrequires(ns.pattern): - s = pkg.dump(long=True) - print(s) + """ + Searches for all packages that have a certain requirement + """ + p = self.pakfire(ns) + + # Search for packages + packages = self._search_packages(p.whatrequires, *ns.pattern) + + # Print packages + self._print_packages(packages) def _provides(self, ns): """ @@ -266,16 +315,11 @@ class Cli(object): """ p = self.pakfire(ns) - packages = set() + # Search for packages + packages = self._search_packages(p.whatprovides, *ns.pattern) - # Walk through all patterns and find all packages - for pattern in ns.pattern: - packages.update(p.whatprovides(pattern)) - - # Print all packages - for package in sorted(packages): - s = package.dump(long=True) - print(s) + # Print packages + self._print_packages(packages) def _repolist(self, ns): """ -- 2.39.5