#!/usr/bin/python # # Trivial / minimal prototype implementation of apt-get using apt-python # bindings based on original apt-get code # # Redistributable under the GPL # # by pmatilai@laiskiainen.org / 2003 # import apt import sys import os def do_upgrade(cache, cmdline): cache.Open(apt.OpTextProgress(), 1) apt.pkgAllUpgrade(cache.pkgDepCache()) return install_packages(cache) def do_distupgrade(cache, cmdline): cache.Open(apt.OpTextProgress(), 1) apt.pkgDistUpgrade(cache.pkgDepCache()) return install_packages(cache) def do_install(cache, cmdline): cache.Open(apt.OpTextProgress(), 1) for name in cmdline: pkg = cache.pkgDepCache().FindPkg(name) if pkg: cache.pkgDepCache().MarkInstall(pkg, 1) return install_packages(cache) def do_remove(cache, cmdline): cache.Open(apt.OpTextProgress(), 1) for name in cmdline: pkg = cache.pkgDepCache().FindPkg(name) if pkg: cache.pkgDepCache().MarkDelete(pkg, 1) install_packages(cache) def install_packages(cache): show_upgraded(cache) recs = apt.pkgRecords(cache.pkgCache()) stat = apt.pkgAcquireStatusDumb() fetcher = apt.pkgAcquire(stat) list = apt.pkgSourceList() if not list.ReadMainList(): print "Error reading source list" pm = apt.cvar._system.CreatePM(cache.pkgDepCache()) if not pm.GetArchives(fetcher, list, recs): print "GetArchives failed" print "Downloading packages..." if fetcher.Run() == apt.pkgAcquire.Failed: print "..failed" return 0 transient = 0 for I in fetcher.ItemsIter(): if I.Local: continue I.Finished() if not I.Complete: #print "transient" transient = 1 for I in fetcher.ItemsIter(): I.Finished() if I.Status == apt.pkgAcquireItem.StatDone and I.Complete: continue if I.Status == apt.pkgAcquireItem.StatIdle: transient = 1 continue print "Failed to fetch %s, %s" % (I.DescURI(), I.ErrorText) apt.cvar._system.UnLock() print "Installing packages..." res = pm.DoInstall() if res == apt.pkgPackageManager.Completed: print "Done" else: print "Installation failed" fetcher.Shutdown() def show_upgraded(cache): print "%s packages to upgrade:" % cache.pkgDepCache().InstCount() for I in cache.PkgIter(): if cache[I].Upgrade(): print I.Name() def get_upgraded(cache): count = cache.pkgDepCache().InstCount() print count upgraded = [] for I in cache.PkgIter(): if cache[I].Upgrade(): count -= 1 print I.Name() if has_modified_configs(I.Name()): print "Keeping", I.Name() cache.pkgDepCache().MarkKeep(I) else: print "Upgrading", I.Name() if not count: break return upgraded def do_check(cache, cmdline): cache.Open(apt.OpTextProgress(), 1) if cache.pkgDepCache().BrokenCount(): print "Broken dependencies" return 0 return 1 def do_update(cache, cmdline): print "Updating indexes.." failed = 0 list = apt.pkgSourceList() if not list.ReadMainList(): print "Failed to read main list" return 0 stat = apt.pkgAcquireStatusDumb() fetcher = apt.pkgAcquire(stat) if not list.GetReleases(fetcher): print "Failed to fetch releases" return 0 fetcher.Run() for I in fetcher.ItemsIter(): if I.Status == apt.pkgAcquireItem.StatDone: continue I.Finished() failed = 1; if failed: print "problems getting release files.." if not list.GetIndexes(fetcher): print "Getting indexes failed" return 0 if fetcher.Run() == apt.pkgAcquire.Failed: print "Downloading indexes failed" return 0 for I in fetcher.ItemsIter(): if I.Status == apt.pkgAcquireItem.StatDone: continue I.Finished() print "Failed to fetch %s %s" %(I.DescURI(), I.ErrorText) failed = 1 if failed: print "Some indexes failed to download.." print "Done" return if __name__ == "__main__": cmds = {'install': do_install, 'remove': do_remove, 'upgrade': do_upgrade, 'dist-upgrade': do_distupgrade, 'check': do_check, 'update': do_update} if len(sys.argv) < 2 or not cmds.has_key(sys.argv[1]): print "Usage: %s [install|update|upgrade|dist-upgrade|check] [..]" % sys.argv[0] sys.exit(1) apt.pkgInit() cache = apt.pkgCacheFile() apply(cmds[sys.argv[1]], (cache, sys.argv[2:]))