X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=rhona;h=7c5dbb29011b381d6b52ba8d1ff44b6144143327;hb=f2c2f346bfc5737f906077cc9d54e7f286898122;hp=c7ee05bc92909685ee9b15125bb4314d293549e2;hpb=1f27b6cf94ec1a0f3d5ee354a2bd0659f437c369;p=dak.git diff --git a/rhona b/rhona index c7ee05bc..7c5dbb29 100755 --- a/rhona +++ b/rhona @@ -2,7 +2,7 @@ # rhona, cleans up unassociated binary and source packages # Copyright (C) 2000, 2001 James Troup -# $Id: rhona,v 1.16 2001-06-22 23:30:21 troup Exp $ +# $Id: rhona,v 1.19 2001-11-24 18:42:10 troup Exp $ # 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 @@ -18,7 +18,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -################################################################################################### +################################################################################ # 07:05| well.. *shrug*.. no, probably not.. but to fix it, # | we're going to have to implement reference counting @@ -27,34 +27,35 @@ # # 07:05| elmo: Augh! -################################################################################################### +################################################################################ import os, pg, stat, string, sys, time import apt_pkg import utils -################################################################################################### +################################################################################ -projectB = None -Cnf = None +projectB = None; +Cnf = None; +Options = None; now_date = None; # mark newly "deleted" things as deleted "now" delete_date = None; # delete things marked "deleted" earler than this -################################################################################################### +################################################################################ -def usage (exit_code): - print """Usage: rhona [OPTION]... [CHANGES]... - -D, --debug=VALUE debug - -n, --no-action don't do anything - -v, --verbose be verbose - -V, --version display version number and exit""" +def usage (exit_code=0): + print """Usage: rhona [OPTIONS] +Clean old packages from suites. + + -n, --no-action don't do anything + -h, --help show this help and exit""" sys.exit(exit_code) -################################################################################################### - +################################################################################ + def check_binaries(): global delete_date, now_date; - + print "Checking for orphaned binary packages..." # Get the list of binary packages not in a suite and mark them for @@ -103,7 +104,7 @@ SELECT s.id, s.file FROM source s #### byebye and sources go byebye is 0 instead of StayOfExecution) ql = q.getresult(); - + projectB.query("BEGIN WORK"); for i in ql: source_id = i[0]; @@ -161,7 +162,7 @@ SELECT id FROM files f file_id = i[0]; projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (now_date, file_id)); projectB.query("COMMIT WORK"); - + def clean_binaries(): global delete_date, now_date; @@ -171,7 +172,7 @@ def clean_binaries(): # XXX: why doesn't this remove the files here as well? I don't think it # buys anything keeping this separate print "Cleaning binaries from the DB..." - if not Cnf["Rhona::Options::No-Action"]: + if not Options["No-Action"]: before = time.time(); sys.stdout.write("[Deleting from binaries table... "); projectB.query("DELETE FROM binaries WHERE EXISTS (SELECT id FROM files WHERE binaries.file = files.id AND files.last_used <= '%s')" % (delete_date)); @@ -188,15 +189,15 @@ def clean(): dest = Cnf["Dir::Morgue"] + '/' + Cnf["Rhona::MorgueSubDir"] + '/' + date; if not os.path.exists(dest): os.mkdir(dest); - + # Delete from source - if not Cnf["Rhona::Options::No-Action"]: + if not Options["No-Action"]: before = time.time(); sys.stdout.write("[Deleting from source table... "); projectB.query("DELETE FROM dsc_files WHERE EXISTS (SELECT df.id FROM source s, files f, dsc_files df WHERE f.last_used <= '%s' AND s.file = f.id AND s.id = df.source AND df.id = dsc_files.id)" % (delete_date)); projectB.query("DELETE FROM source WHERE EXISTS (SELECT id FROM files WHERE source.file = files.id AND files.last_used <= '%s')" % (delete_date)); sys.stdout.write("done. (%d seconds)]\n" % (int(time.time()-before))); - + # Delete files from the pool q = projectB.query("SELECT l.path, f.filename FROM location l, files f WHERE f.last_used <= '%s' AND l.id = f.location" % (delete_date)); for i in q.getresult(): @@ -207,7 +208,7 @@ def clean(): if os.path.isfile(filename): if os.path.islink(filename): count = count + 1; - if Cnf["Rhona::Options::No-Action"]: + if Options["No-Action"]: print "Removing symlink %s..." % (filename); else: os.unlink(filename); @@ -219,16 +220,16 @@ def clean(): # If the destination file exists; try to find another filename to use if os.path.exists(dest_filename): dest_filename = utils.find_next_free(dest_filename); - - if Cnf["Rhona::Options::No-Action"]: + + if Options["No-Action"]: print "Cleaning %s -> %s ..." % (filename, dest_filename); else: utils.move(filename, dest_filename); else: utils.fubar("%s is neither symlink nor file?!" % (filename)); - + # Delete from the 'files' table - if not Cnf["Rhona::Options::No-Action"]: + if not Options["No-Action"]: before = time.time(); sys.stdout.write("[Deleting from files table... "); projectB.query("DELETE FROM files WHERE last_used <= '%s'" % (delete_date)); @@ -238,7 +239,7 @@ def clean(): def clean_maintainers(): print "Cleaning out unused Maintainer entries..." - + q = projectB.query(""" SELECT m.id FROM maintainer m WHERE NOT EXISTS (SELECT id FROM binaries b WHERE b.maintainer = m.id) @@ -249,7 +250,7 @@ SELECT m.id FROM maintainer m projectB.query("BEGIN WORK"); for i in ql: maintainer_id = i[0]; - if not Cnf["Rhona::Options::No-Action"]: + if not Options["No-Action"]: projectB.query("DELETE FROM maintainer WHERE id = %s" % (maintainer_id)); count = count + 1; projectB.query("COMMIT WORK"); @@ -257,34 +258,30 @@ SELECT m.id FROM maintainer m if count > 0: sys.stderr.write("Cleared out %d maintainer entries.\n" % (count)); +################################################################################ + def main(): - global Cnf, projectB, delete_date, now_date; - - apt_pkg.init(); - - Cnf = apt_pkg.newConfiguration(); - apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file()); + global Cnf, Options, projectB, delete_date, now_date; + + Cnf = utils.get_conf() + for i in ["Help", "No-Action" ]: + if not Cnf.has_key("Rhona::Options::%s" % (i)): + Cnf["Rhona::Options::%s" % (i)] = ""; projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"])); - Arguments = [('D',"debug","Rhona::Options::Debug", "IntVal"), - ('h',"help","Rhona::Options::Help"), - ('n',"no-action","Rhona::Options::No-Action"), - ('V',"version","Rhona::Options::Version")]; - + Arguments = [('h',"help","Rhona::Options::Help"), + ('n',"no-action","Rhona::Options::No-Action")]; + apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv); Options = Cnf.SubTree("Rhona::Options") if Options["Help"]: - usage(0); - - if Options["Version"]: - print "rhona version 0.0000000000"; - usage(0); + usage(); now_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time())); delete_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-int(Cnf["Rhona::StayOfExecution"]))); - + check_binaries(); clean_binaries(); check_sources(); @@ -292,6 +289,8 @@ def main(): clean(); clean_maintainers(); +################################################################################ + if __name__ == '__main__': main()