#!/usr/bin/python import gzip, urllib, sys, tempfile, os.path, apt_pkg, shutil import subprocess blacklist = ['language-pack', 'language-support'] def any(iterable): for element in iterable: if element: return True return False def package_list_map(url, merge_to = None): '''Read a package list and parse it into a map package->key->value. E. g. package_list_map('Sources.gz')['dpkg']['Version'] would deliver the Version of the dpkg source package. ''' # write the uncompressed downloaded list into a temporary file downloadfile = urllib.urlretrieve(url, tempfile.mkstemp()[1])[0] list = tempfile.TemporaryFile() try: print >> list, gzip.open(downloadfile).read() except IOError: print >> list, open(downloadfile).read() os.unlink(downloadfile) list.seek(0) if merge_to is None: map = {} else: map = merge_to parser = apt_pkg.ParseTagFile(list) while parser.Step() == True: pkg = {} for key in parser.Section.keys(): if key != 'Package': pkg[key] = parser.Section.get(key) map[parser.Section.get('Package')] = pkg return map def process_dsc(dscpath, command): '''Unpack a .dsc and run command in it.''' print '==== %s ====' % os.path.basename(dscpath) d = tempfile.mkdtemp() os.chdir(d) try: subprocess.call(['dpkg-source', '-x', dscpath], stdout=subprocess.PIPE, stderr=subprocess.PIPE) subprocess.call(command, shell=True) finally: shutil.rmtree(d) # # main # if len(sys.argv) != 4: print >> sys.stderr, 'Usage: %s ' % sys.argv[0] sys.exit(1) (archive_root, sources_path, command) = sys.argv[1:] map = package_list_map(archive_root + '/' + sources_path) for pkg, i in map.iteritems(): if any([b in pkg for b in blacklist]): continue for fline in i['Files'].splitlines(): if fline.endswith('.dsc'): process_dsc(os.path.join(archive_root, i['Directory'], fline.split()[-1]), command) break