One day, I decided to start tracking with git the edits I had made to files in /etc.

The problem is: I didn't know which one they were.

So I wrote this script to find them. You need python-apt to run it.

Click here to download it

#! /usr/bin/python
# Check files in /etc for
# 1) files changed from the default ones
# 2) files which are contained in packages but are not conffiles
# 3) files which are not contained in any packages (though they are possibly
#    _created_ by some package at configuration time)

import apt
import subprocess
import hashlib
import os
import glob

__version__ = '0.1'

c = apt.Cache()

def main():
    pkg_etc_files = []

    for package in c:
        etc_files = filter( startswith( '/etc/' ), package.installed_files)
        if etc_files:
            cmd = "dpkg --status %s" % package.name
    #        print cmd.split()
            dpkg = subprocess.Popen( cmd.split(), stdout=subprocess.PIPE )
            pkg_info = dpkg.stdout.readlines()
            conf_info = filter( startswith( ' /etc' ), pkg_info )
    #        print pkg_info, conf_info
            for conffile in etc_files:
                if not os.path.isfile( conffile ):
                    # Probably a directory:
                    continue
                f_hand = open( conffile )
                f_cont = f_hand.read()
                f_hand.close()
                true_md5 = hashlib.md5( f_cont ).hexdigest()
                
                appropr_line = filter( startswith( ' %s ' % conffile ), conf_info )
                try:
                    assert len( appropr_line ) == 1
                    appropr_line = appropr_line[0]
                    theorical_md5 = appropr_line.split()[1]
                    if true_md5 != theorical_md5:
                        print "\t\t\t\033[1m%s changed\033[0m" % conffile
                except:
                    print "%s is installed in /etc but is apparently not a conffile" % conffile
            
            pkg_etc_files.extend( etc_files )
    
    print """\n\n\t\033[1mMoreover, the following files in /etc aren't contained in any installed package
    \t(either they are created on-the-fly, or they are part of an uninstalled package):\033[0m\n"""
    
    
    
    etc_files = set( all_files( '/etc' ) )
    
    etc_files.difference_update( pkg_etc_files )
    
    print ", ".join( etc_files )


def startswith( what ):
    return (lambda s : s.startswith( what ))

def all_files( root ):
    files_list = []
    walker = os.walk( root )
    while True:
        try:
            basedir, subdirs, files = walker.next()
            new = [os.path.join( basedir, a_file ) for a_file in files]
            files_list.extend( new )
        except StopIteration:
            break
    
    return files_list


if __name__ == "__main__":
    main()