Robert Escriva

Things I wish I learned earlier

Finding Least-Used Debian Packages

So I was wondering what software I use least/most on my system, and wrote a little script to tell me when each package was last used. To assign a time stamp to each package, I take the maximum access time of all files belonging to the package.

import os
import datetime

DPKG_INFO_DIR = '/var/lib/dpkg/info/'

def package_used(package_name):
    path      = os.path.join(DPKG_INFO_DIR, package_name + '.list')
    f         = open(path)
    last_used = datetime.datetime.min

    for file in f:
        try:
            atime     = os.stat(file[:-1]).st_atime
        except OSError, e:
            if e.errno == 2:
                continue
            raise
        dt_atime  = datetime.datetime.fromtimestamp(atime)
        last_used = max(dt_atime, last_used)

    return (last_used, package_name)

def package_listings():
    for file in os.listdir(DPKG_INFO_DIR):
        if file.endswith('.list'):
            last_used, pkgname = package_used(file[:-5])
            print last_used, pkgname

if __name__ == '__main__':
    package_listings()

Update 2009-12-03: To use this script you should have /usr mounted without 'noatime' or 'relatime' options for a significant period of time (perhaps a week or two) so that access times are updated.

Copyright © 2010 Robert Escriva ¦ Powered by Firmant