Skip to content
Snippets Groups Projects

RPM lookup

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Josha Bartsch

    Lookup package metadata by partial name or local path

    Edited
    rpm-search.py 1.79 KiB
    #!/usr/bin/env python3
    from subprocess import run, CalledProcessError
    from pathlib import Path
    from argparse import ArgumentParser
    import sys
    
    
    def parse(target: str, format: str, verbose: bool):
        # Construct rpm query
        query = ["rpm", "-q"]
    
        path = Path(target)
        if path.exists():
            query += ["--whatprovides", path.absolute()]
        else:
            # Partial might match multiple packages found by yum, may include packages not installed
            tpkgs = run(
                ["yum", "--disableexcludes=all", "info", f"*{target}*"],
                text=True,
                capture_output=True,
                check=True,
            )
            pkgs = {
                name.split(":")[1].strip()
                for name in filter(lambda line: "Name" in line, tpkgs.stdout.split("\n"))
                if ":" in name
            }
            query += pkgs
    
        print(f"Searching for packages matching {target}...")
        ret = run(
            [*query, "--qf", format + "\\n"], text=True, capture_output=True, check=False
        )
        for url in ret.stdout.split("\n"):
            if url:
                if "not installed" in url and not verbose:
                    continue
                print(url)
    
    
    if __name__ == "__main__":
        parser = ArgumentParser(
            description="Query locally installed rpm packages for metadata. Available fields are: %{url}, %{name}, ... For reference see https://rpm-packaging-guide.github.io/#what-is-a-spec-file"
        )
        parser.add_argument(
            "target",
            help="Package reference to lookup: Either a partial package name or path to the installed files.",
        )
        parser.add_argument(
            "--format", "-f", help="Metadata format string (default: url)", default="%{url}"
        )
        parser.add_argument("-v", "--verbose", action="store_true")
    
        args = parser.parse_args()
        parse(args.target, args.format, args.verbose)
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment