|
发表于 2008-9-27 01:47:43
|
显示全部楼层
Here is a python script to clean the /var/cache/pacman/pkg directory while allowing you to specify how many package versions should be retained.
#!/usr/bin/env python
"""pkg_clean - a simple python script to clean up the /var/cache/pacman/pkg directory.
More versatile than 'pacman -Sc' in that you can select how many old versions
to keep.
Usage: pkg_clean {-p} {-v} <# of copies to keep>
# of copies to keep - (required) how many generations of each package to keep
-p - (optional) preview what would be deleted; forces verbose (-v) mode.
-v - (optional) show deleted packages."""
# Note that the determination of package age is done by simply looking at the date-time
# modified stamp on the file. There is just enough variation in the way package version
# is done that I thought this would be simpler & just as good.
# Also note that you must be root to run this script.
import getopt
import os
import re
import sys
# cleanup does the actual pkg file deletion
def cleanup(run_list):
# strictly speaking only the first of these globals needs to be listed.
global n''deleted, opt''verbose, opt''preview, n''to_keep
# return if the run_list is too short
if len(run''list) <= n''to_keep: return
# Build list of tuples (date-time file modified, file name)
dtm''list = [(os.stat(tfn)[8], tfn) for tfn in run''list]
# Sort the list by date-time
dtm_list.sort()
# Build list of the filenames to delete (all but last n''to''keep).
kill''list = [tfn[1] for tfn in dtm''list[:-n''to''keep]]
if opt''verbose: print kill''list
n''deleted += len(kill''list)
if not opt_preview:
for dfn in kill_list:
os.unlink(dfn)
######################################################################
# mainline processing
# process command line options
try:
opts, pargs = getopt.getopt(sys.argv[1:], 'vp')
opt_dict = dict(opts)
opt''preview = opt''dict.has_key('-p')
opt''verbose = opt''dict.has_key('-v')
if opt''preview: opt''verbose = True
if len(pargs) <code></code> 1:
n''to''keep = pargs[0]
else:
raise getopt.GetoptError("missing required argument.")
try:
n''to''keep = int(n''to''keep)
if n''to''keep <= 0: raise ValueError
except ValueError, e:
raise getopt.GetoptError("# of copies to keep must be numeric > 0!")
except getopt.GetoptError, msg:
print "Error:",msg,"\n",''''doc''''
sys.exit(1)
# change to the pkg directory & get a sorted list of its contents
os.chdir('/var/cache/pacman/pkg')
pkg_fns = os.listdir('.')
pkg_fns.sort()
# for pkg e.g. 'gnome-common-2.8.0-1.pkg.tar.gz' group(1) is 'gnome-common'.
bpat = re.compile(r'(.+)-[^-]+-.+\.pkg.tar.gz$')
n_deleted = 0
pkg''base''nm = ''
# now look for "runs" of the same package differing only in version info.
for run''end in range(len(pkg''fns)):
fn = pkg''fns[run''end]
mo = bpat.match(fn) # test for a match of the package name pattern
if mo:
tbase = mo.group(1) # gets the 'base' package name
# is it a new base name, i.e. the start of a new run?
if tbase != pkg''base''nm: # if so then process the prior run
if pkg''base''nm != '':
cleanup(pkg''fns[run''start:run_end])
pkg''base''nm = tbase # & setup for the new run
run''start = run''end
else:
print >>sys.stderr, "File '"+fn+"' doesn't match package pattern!"
# catch the final run of the list
run_end += 1
cleanup(pkg''fns[run''start:run_end])
if opt_verbose:
print n_deleted,"files deleted." |
|