#!/usr/bin/python import apt import sys import os import urllib def progress(blocks, bs, total): print "\b\b\b\b\b\b", percent = blocks * bs / float(total) * 100 if percent > 100: percent = 100 print "%3d%%" % percent, def download(pkgnames): apt.pkgInit() cache = apt.pkgCacheFile() cache.Open(apt.OpTextProgress(), 0) apt.cvar._config.Set("Dir::Cache::Archives", os.getcwd()) for name in pkgnames: pkg = cache.FindPkg(name) if pkg: if not pkg.CurrentVer(): cache.pkgDepCache().MarkInstall(pkg, 1) else: cache.SetReInstall(pkg, 1) else: print "%s not found, skipping" % name list = apt.pkgSourceList() if not list.ReadMainList(): print "readlist failed" stat = apt.pkgAcquireStatusDumb() recs = apt.pkgRecords(cache.pkgCache()) fetcher = apt.pkgAcquire(stat) PM = apt.cvar._system.CreatePM(cache.pkgDepCache()) if not PM: print "Pm create failed" if not PM.GetArchives(fetcher, list, recs): print "getarchives failed" opener = urllib.FancyURLopener() for item in fetcher.ItemsIter(): print item.DescURI() for item in fetcher.ItemsIter(): if item.Local: print "Skipping local file", item.DestFile continue fname = item.DescURI().split('/')[-1:][0] print "Downloading %s... " % fname, opener.retrieve(item.DescURI(), fname, progress) print if __name__ == "__main__": if len(sys.argv) < 2: print "Usage: %s [...]" % sys.argv[0] sys.exit(1) download(sys.argv[1:]) # vim:ts=4