Skip to content

Instantly share code, notes, and snippets.

@moyix
Created February 5, 2022 22:45
Show Gist options
  • Save moyix/f64f35a563f74d23293c4c17fb15c904 to your computer and use it in GitHub Desktop.
Save moyix/f64f35a563f74d23293c4c17fb15c904 to your computer and use it in GitHub Desktop.
Script that uses python-apt to get some info about source packages
#!/usr/bin/env python
import re
import sys, os
sys.path.append('/usr/lib/python3/dist-packages')
import apt
import apt_pkg
import argparse
def urljoin(*args):
return "/".join(map(lambda x: str(x).rstrip("/"), args))
ROOT_URL = 'http://ftp.us.debian.org/debian/'
parser = argparse.ArgumentParser(description='List source pkg name or URLs')
parser.add_argument('command', choices=['list', 'urls', 'clean', 'check'], help='list or urls')
parser.add_argument('pkg', nargs='+', help='package names')
args = parser.parse_args()
cache = apt.Cache()
if args.command == 'check':
rv = 0
for pkg in args.pkg:
srcrecords = apt_pkg.SourceRecords()
srcrec = srcrecords.lookup(pkg)
if not srcrec:
print(f"No source package record found for {pkg}")
rv = 1
else:
print(f"{pkg} {srcrecords.package}")
sys.exit(rv)
else:
args.pkg = args.pkg[0]
srcrecords = apt_pkg.SourceRecords()
srcrec = srcrecords.lookup(args.pkg)
if not srcrec:
print(f"No source package record found for {args.pkg}")
sys.exit(1)
pkg_ver = srcrecords.version.split(':', 1)[-1]
short_ver = re.sub(r'-[^-]*$', '', pkg_ver)
if args.command == 'list':
dsc = [os.path.basename(f.path) for f in srcrecords.files if f.type == 'dsc'][0]
print(f'PKG="{srcrecords.package}" VER="{short_ver}" LONGVER="{srcrecords.version}" DSC="{dsc}"')
elif args.command == 'urls':
for f in srcrecords.files:
print(urljoin(ROOT_URL, f.path))
elif args.command == 'clean':
arch = 'amd64' # FIXME: detect arch
debs = [
f'{b}_{pkg_ver}_{arch}.deb' for b in srcrecords.binaries
]
other_files = [f'{os.path.basename(f.path)}' for f in srcrecords.files]
print('rm -f ' + ' '.join(debs + other_files))
else:
print(f"Unknown command: {args.command}")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment