以前有人写过一个distclean.py,但是现在已经不适用了,所以我改写了一个,希望对大家有帮助。
Code:
#!/usr/bin/env python
## filename : edistclean.py
## Copyright (c) 2003, Fredrik Arnerup (e97_far@e.kth.se)
## Copyright (c) 2005, Bob Wolfe (rufeng2000@hotmail.com)
import sys, os, os.path, portage
opt_delete = 0
if len(sys.argv) > 1:
if sys.argv[1] == 'delete':
opt_delete = 1
else:
print 'usage :', sys.argv[0], '[delete]'
sys.exit(1)
distdir = portage.settings['DISTDIR']
print 'DISTDIR =', distdir
fetchs = os.popen("emerge -pvef world 2>&1 1>/dev/null").readlines()
print '"emerge -pef world" returns', len(fetchs) / 2, 'files.'
files = {}
for s in fetchs:
if s == '\n':
continue
file = s.split(' ')[0].split('/')[-1]
files[file] = 1
try:
list = portage.listdir(distdir)
except os.OSError:
sys.exit('Failed to read ' + distdir)
size = 0; count = 0
for file in list:
abs_file = distdir + '/' + file
if (os.path.isfile(abs_file) and (not os.path.islink(abs_file))
and (not file in files)):
size += os.stat(abs_file).st_size
count += 1
if not opt_delete:
print 'Would remove', abs_file
else:
try:
os.remove(abs_file)
print 'Removed', abs_file
except OSError:
print 'Failed to remove', abs_file
size /= 1048576 ## MB
print '%i files, total size: %i MB' % (count, size)
if not opt_delete:
print 'use "', sys.argv[0], 'delete " to remove unused files.'
阅读(777) | 评论(0) | 转发(0) |