]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire-builder: Make search functions re-use some code
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 19 Nov 2022 13:55:18 +0000 (13:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 19 Nov 2022 13:55:18 +0000 (13:55 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/scripts/pakfire-builder.in

index 25e3de7c77700c8768483788f3f5eeb9e2f2e02c..6dee24fb255425bd735ff7021f3934ffdf79a912 100644 (file)
@@ -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):
                """