From Media Design: Networked & Lens-Based wiki
Jump to navigation Jump to search

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.


#! /usr/bin/python
import apt

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

def savepkgs(f, debs):
    print "saving file: " + f
    fp = open(f, 'w')
    rsl = fp.write('\n'.join(debs))
    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:
    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 = []
        for pkg in pkgs:
            if pkg not in cache:
            deb = cache[pkg]
    except apt.cache.LockFailedException:
        print "LockFailedException: are you root??"
    except SystemError:
        print "SystemErrorException: package manager running??"
    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

To make it work save the script (as and save the packages list (as packages.txt) in the same folder. Install python-apt and run this in the terminal: python -i packages.txt

It takes a while but it will work :)