SoftwareSnapshot: Difference between revisions

From XPUB & Lens-Based wiki
Line 79: Line 79:


</source>
</source>
I've uploaded the list of packages that I installed with this script after I completely deleted 10.04 and started fresh with 11.04. You can find it here [[File:packages.txt|here]]

Revision as of 18:22, 2 July 2011

Software Snapshot

This script uses python-apt to manipulate the packages that are installed on a machine. This way you can back-up and transfer your software. Potentially it also gives a nice overview of which packages get used on the course.

There are three options.

  • With -f FILE --snapshot you save all currently installed packages in FILE. This includes all packages from Ubuntu not only the ones you installed manually!
  • With --install FILE you install all packages that are listed in FILE
  • With -f OUT --diff FILE1 FILE2 FILE3 ... you save all packages from FILE1 which are not in FILE2 or FILE3 etc. in OUT. This is useful to filter out all packages that get installed by Ubuntu.

YOU'LL HAVE TO INSTALL THE PACKAGE PYTHON-APT!

#! /usr/bin/python
import apt

def loadpkgs(f):
    print "loading file: " + f
    fp = open(f, 'r')
    rsl = fp.read().splitlines()
    fp.close()
    return rsl

def savepkgs(f, debs):
    print "saving file: " + f
    fp = open(f, 'w')
    rsl = fp.write('\n'.join(debs))
    fp.close()
    return rsl

def cbdiff(option, opt_str, value, parser):
    print "calculating difference between: " + ', '.join(parser.rargs)
    main = loadpkgs(parser.rargs.pop(0))
    for f in parser.rargs:
        sub = loadpkgs(f)
        main = [pkg for pkg in main if pkg not in sub]
    savepkgs(parser.values.filename, main)

def cbsnapshot(option, opt_str, value, parser):
    print "creating a snapshot"
    cache = apt.Cache()
    debinst = []
    for deb in cache:
        if deb.is_installed:
            debinst.append(deb.name)
    print "saving file: " + parser.values.filename
    savepkgs(parser.values.filename, debinst)

def cbinstall(option, opt_str, value, parser):
    print "marking packages for installation"
    pkgs = loadpkgs(value)
    cache = apt.Cache()
    notincache = []
    try:
        for pkg in pkgs:
            if pkg not in cache:
                notincache.append(pkg)
                continue
            deb = cache[pkg]
            deb.mark_install()
        cache.commit()
    except apt.cache.LockFailedException:
        print "LockFailedException: are you root??"
        exit()
    except SystemError:
        print "SystemErrorException: package manager running??"
        exit()
    print "packages installed"
    if notincache: print "missing packages on system: " + ', '.join(notincache)

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", "--file", action="store", dest="filename",  default="out.sss", help="Specify an output file. Default is out.sss")
parser.add_option("-s", "--snapshot", action="callback", callback=cbsnapshot, help="Make a snapshot of all installed packages.")
parser.add_option("-i", "--install", action="callback", callback=cbinstall, type="string", help="Install all packages in a file.")
parser.add_option("-d", "--diff", action="callback", callback=cbdiff, help="Compute the difference between two or more snapshots. List all snapshot files as arguments. Start with largest file.")

(options, args) = parser.parse_args()

I've uploaded the list of packages that I installed with this script after I completely deleted 10.04 and started fresh with 11.04. You can find it here File:Packages.txt