#!/usr/bin/python

import glob
import os
import sys

# dict that maps path -> list of packages that contain the path
files = {}

for lf in glob.glob ('/var/lib/dpkg/info/*.list'):
    package = lf[19:-5]
    for file in [line.strip () for line in open (lf)]:
        if not file in files:
            files[file] = []
        files[file].append (package)

dirs = sys.path[1:] # sys.path[0] is the dir that contains this script

def raise_error (e):
    raise e

for dir in dirs:
    if not os.path.isdir (dir):
        continue

    for dirpath, dirnames, filenames in os.walk (dir, onerror = raise_error):
        for filename in filenames:
            path = os.path.join (dirpath, filename)

            # symlinks are OK as long as they point to an existing file
            if os.path.islink (path):
                if not os.path.exists (path):
                    print 'broken link: %s' % (path)
                continue

            # .pyc files should be accompanied by a corresponding .py file
            if path.endswith ('.pyc'):
                if not os.path.exists (path[:-1]):
                    print 'orphaned .pyc: %s' % (path)
                continue

            # other files should be in dpkg's datbase
            if path not in files:
                if filename == '__init__.py' and os.path.getsize (path) == 0:
                    # possibly a python-central-created __init__.py
                    continue

                print 'untracked: %s' % (path)
