Skip to content

Instantly share code, notes, and snippets.

@Neboer
Created May 15, 2025 06:36
Show Gist options
  • Save Neboer/cf6ffb27296a9c8dcc31b6542f9f02f7 to your computer and use it in GitHub Desktop.
Save Neboer/cf6ffb27296a9c8dcc31b6542f9f02f7 to your computer and use it in GitHub Desktop.
pacman-pkgbyrepo archlinux show your installed packages and their origin repos
#!/usr/bin/python3
import configparser
import pyalpm
def get_repo_list():
c = configparser.ConfigParser(strict=False, allow_no_value=True)
c.read('/etc/pacman.conf')
return [
section
for section in c.sections()
if c.get(section, 'Include', fallback=None)
or c.get(section, 'Server', fallback=None)
]
def which_repo(dbs, pkg):
for repo, db in dbs.items():
if db.get_pkg(pkg):
return repo
return 'local'
def main():
repos = get_repo_list()
h = pyalpm.Handle('/', '/var/lib/pacman')
dbs = {repo: h.register_syncdb(repo, 0) for repo in repos}
by_db = {repo: [] for repo in repos}
by_db['local'] = []
ldb = h.get_localdb()
for pkg in ldb.pkgcache:
by_db[which_repo(dbs, pkg.name)].append(pkg.name)
for repo, pkgs in by_db.items():
pkgs.sort()
for pkg in pkgs:
print(f'{repo} {pkg}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment