#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# cruft2.py: the second generation of cruft
# 
# Copyright © 2004 Ed Catmur <ed@catmur.co.uk>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

import sys, os, os.path, stat, pwd
import time
import string, re, textwrap

#sys.path = ["/usr/lib/portage/pym"]+sys.path
#import portage

USERS = [ passwd[0] for passwd in pwd.getpwall() ]

EXCLUDE = [ '/boot', '/etc/opt', '/media', '/mnt', '/opt', '/srv', '/usr/opt', \
		'/usr/src', '/var/opt' ]
EXCLUDE += [ '/var/www' ]
EXCLUDE += [ '/home', '/root' ] \
		+ [ '/var/spool/cron/crontabs/%s' % user for user in USERS ] \
		+ [ '/var/spool/mail/' + user for user in USERS ]
EXCLUDE += [ '/etc/init.d/local', '/usr/local', \
		'/usr/share/applications/local', \
		'/usr/share/control-center-2.0/capplets/local', \
		'/usr/share/faces', '/usr/share/fonts/local', \
		'/usr/share/pixmaps/local', '/usr/share/sounds/local', \
		'/usr/share/texmf/source/latex/local' ]
EXCLUDE += [ '/etc/cron.hourly', '/etc/cron.daily', '/etc/cron.weekly', \
		'/etc/cron.monthly', '/etc/cron.allow', '/etc/cron.deny', \
		'/etc/dnsdomainname', '/etc/group', '/etc/group-', \
		'/etc/gshadow', '/etc/gshadow-', '/etc/hostname', \
		'/etc/hosts', '/etc/hosts.allow', '/etc/hosts.deny', \
		'/etc/hosts.equiv', '/etc/issue', '/etc/issue.net', \
		'/etc/make.conf', '/etc/make.profile', '/etc/localtime', \
		'/etc/motd', '/etc/passwd', '/etc/passwd-', '/etc/portage', \
		'/etc/runlevels', '/etc/shadow', '/etc/shadow-', '/etc/skel', \
		'/etc/xprofile', '/var/cache/edb/virtuals', \
		'/var/cache/edb/world', '/var/lib/portage' ]

SPLIT_REGEXP = re.compile(r"^(.*)-(((?:\d+.)*\d+[a-z]?(?:_(?:alpha|beta|pre|rc|p)\d*)?)(?:-(r\d+))?)$")
def split(package):
	return SPLIT_REGEXP.match(package).groups()

def installed():
	for category in os.listdir('/var/db/pkg'):
		for package in os.listdir('/var/db/pkg/%s' % category):
			name, fullversion, version, revision = split(package)
			yield category, name, fullversion

c_in, c_out = os.popen2("~/bin/cruft --list-for")
def contents(c, p, v):
	for line in file('/var/db/pkg/%s/%s-%s/CONTENTS' % (c, p, v)):
		yield line.split()[:2]
	c_in.write("%s/%s-%s\n" % (c, p, v))
	c_in.flush()
	while True:
		line = c_out.readline()[:-1]
		if line == '':
			break
		if line[-1] == '/':
			yield 'all', line[:-1]
		else:
			yield 'ext', line	# sym, obj or node-dir

fs = {}

def get_contents():
	for c, p, v in installed():
		print >>sys.stderr, (c, p, v)
		for type, path in contents(c, p, v):
			fs[path] = type

def cruft():
	for dirpath, dirnames, filenames in os.walk('/'):
		print >>sys.stderr, "-> %s" % dirpath 
		for x in dirnames:
			path = os.path.join(dirpath, x)
			if path in EXCLUDE:
				dirnames.remove(x)
			else:
				try:
					type = fs.pop(path)
					if type == 'all' or True:
						dirnames.remove(x)
				except KeyError:
					yield path
		for x in filenames:
			path = os.path.join(dirpath, x)
			if path not in EXCLUDE:
				try:
					del fs[path]
				except KeyError:
					yield path

def main():
	get_contents()
	for x in cruft():
		print x
	
import xml.dom.minidom
xml = """
<dataset>
	<block>
		<for package="app-admin/usermin"/>
/usr/libexec/usermin
/etc/usermin
/var/webmin
	</block>
	<block>
		<for package="app-editors/gvim"/>
		<for package="app-editors/vim"/>
		<for package="app-editors/vim-core"/>
		<sh>
echo /usr/bin/{vi,vimdiff,rvim,ex,view,rview,vim}
		</sh>
	</block>
</dataset>
"""
clang = """
app-admin/usermin:
	/usr/libexec/usermin
	/etc/usermin
	/var/usermin
app-editors/gvim app-editors/vim app-editors/vim-core:
	sh: echo /usr/bin/{vi,vimdiff,rvim,ex,view,rview,vim}
dev-util/eclipse-sdk/0:
	sh: echo /usr/lib/eclipse/{Uninstaller,features,plugins}
x11-base/xorg-x11:
	/etc/X11/xorg.xonf
	fontdir:
		CID
		TTF
		ttf
		ukr
		misc
		util
		75dpi
		Type1
		local
		Speedo
		encodings
		encodings/large
		100dpi
		cyrillic
		default
	sh: echo /var/log/Xorg.*.log{,.old}
"""

if __name__ == "__main__":
	main()
