#!/usr/bin/python3 from __future__ import print_function import os import re import rpm dirs = ['/home/services/ftp/pld/dists/3.0/PLD/x32/RPMS', '/home/services/ftp/pld/dists/3.0/PLD/i686/RPMS', '/home/services/ftp/pld/dists/3.0/PLD/x86_64/RPMS', '/home/services/ftp/pld/dists/3.0/ready/x32/RPMS', '/home/services/ftp/pld/dists/3.0/ready/i686/RPMS', '/home/services/ftp/pld/dists/3.0/ready/x86_64/RPMS', '/home/services/ftp/pld/dists/3.0/test/x32/RPMS', '/home/services/ftp/pld/dists/3.0/test/i686/RPMS', '/home/services/ftp/pld/dists/3.0/test/x86_64/RPMS'] #dirs = ['/home/services/ftp/pld/dists/3.0/test/x86_64/RPMS'] outname = "/home/pld/admins/th/www/name-srcname.txt" re_rpm = re.compile(r'.*\.rpm$') re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.rpm$') ts = rpm.ts() ts.setVSFlags(-1) pkgs = {} for dir in dirs: for file in os.listdir(dir): if not re_rpm.match(file): continue rpm_file = os.path.join(dir, file) fdno = os.open(rpm_file, os.O_RDONLY) try: hdr = ts.hdrFromFdno(fdno) except Exception as e: print("hdrFromFdno: %s: %s" % (rpm_file, e)) os.close(fdno) continue os.close(fdno) name = hdr[rpm.RPMTAG_NAME] sourcerpm = hdr[rpm.RPMTAG_SOURCERPM] m = re_nvr.match(sourcerpm) if not m: print("%s: doesn't match src.rpm file name" % (sourcerpm)) continue srcname = m.group(1) pkgs[name] = srcname f = open(outname + ".tmp", "w") for (pkg, spkg) in iter(pkgs.items()): f.write("%s:%s\n" % (pkg, spkg)) f.close() os.chmod(outname + ".tmp", 0o644) os.rename(outname + ".tmp", outname)