X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=dak%2Fclean_suites.py;h=314c6121727a843427bde0c22cbcabe06df6e7e8;hb=245c6549dbacaeab5ee36ec74372b1df8675b477;hp=85d4158ffa37ed1d5b118080e0c6a8990bc22c8a;hpb=2075358234415c9f2906beea2cb220af09ca5c61;p=dak.git diff --git a/dak/clean_suites.py b/dak/clean_suites.py index 85d4158f..314c6121 100755 --- a/dak/clean_suites.py +++ b/dak/clean_suites.py @@ -34,6 +34,7 @@ ################################################################################ +import errno import os import stat import sys @@ -65,7 +66,7 @@ Clean old packages from suites. ################################################################################ def check_binaries(now_date, session): - print "Checking for orphaned binary packages..." + Logger.log(["Checking for orphaned binary packages..."]) # Get the list of binary packages not in a suite and mark them for # deletion. @@ -107,7 +108,7 @@ def check_binaries(now_date, session): ######################################## def check_sources(now_date, session): - print "Checking for orphaned source packages..." + Logger.log(["Checking for orphaned source packages..."]) # Get the list of source packages not in a suite and not used by # any binaries. @@ -173,7 +174,7 @@ def check_files(now_date, session): # and then mark the file for deletion. This probably masks a bug somwhere # else but is better than collecting cruft forever - print "Checking for unused files..." + Logger.log(["Checking for unused files..."]) q = session.execute(""" UPDATE files_archive_map af SET last_used = :last_used @@ -199,7 +200,7 @@ def clean_binaries(now_date, session): # XXX: why doesn't this remove the files here as well? I don't think it # buys anything keeping this separate - print "Deleting from binaries table... " + Logger.log(["Deleting from binaries table... "]) q = session.execute(""" DELETE FROM binaries b USING files f @@ -221,7 +222,7 @@ def clean(now_date, archives, max_delete, session): count = 0 size = 0 - print "Cleaning out packages..." + Logger.log(["Cleaning out packages..."]) morguedir = cnf.get("Dir::Morgue", os.path.join("Dir::Pool", 'morgue')) morguesubdir = cnf.get("Clean-Suites::MorgueSubDir", 'pool') @@ -237,7 +238,7 @@ def clean(now_date, archives, max_delete, session): os.makedirs(dest) # Delete from source - print "Deleting from source table... " + Logger.log(["Deleting from source table..."]) q = session.execute(""" WITH deleted_sources AS ( @@ -271,7 +272,7 @@ def clean(now_date, archives, max_delete, session): old_files = session.query(ArchiveFile).filter('files_archive_map.last_used <= (SELECT delete_date FROM archive_delete_date ad WHERE ad.archive_id = files_archive_map.archive_id)').join(Archive) if max_delete is not None: old_files = old_files.limit(max_delete) - print "Limiting removals to %d" % max_delete + Logger.log(["Limiting removals to %d" % max_delete]) if archives is not None: archive_ids = [ a.archive_id for a in archives ] @@ -296,7 +297,7 @@ def clean(now_date, archives, max_delete, session): dest_filename = dest + '/' + os.path.basename(filename) # If the destination file exists; try to find another filename to use - if os.path.exists(dest_filename): + if os.path.lexists(dest_filename): dest_filename = utils.find_next_free(dest_filename) if not Options["No-Action"]: @@ -316,7 +317,6 @@ def clean(now_date, archives, max_delete, session): if count > 0: Logger.log(["total", count, utils.size_type(size)]) - print "Cleaned %d files, %s." % (count, utils.size_type(size)) # Delete entries in files no longer referenced by any archive query = """ @@ -331,7 +331,7 @@ def clean(now_date, archives, max_delete, session): ################################################################################ def clean_maintainers(now_date, session): - print "Cleaning out unused Maintainer entries..." + Logger.log(["Cleaning out unused Maintainer entries..."]) # TODO Replace this whole thing with one SQL statement q = session.execute(""" @@ -354,19 +354,19 @@ SELECT m.id, m.name FROM maintainer m if count > 0: Logger.log(["total", count]) - print "Cleared out %d maintainer entries." % (count) ################################################################################ def clean_fingerprints(now_date, session): - print "Cleaning out unused fingerprint entries..." + Logger.log(["Cleaning out unused fingerprint entries..."]) # TODO Replace this whole thing with one SQL statement q = session.execute(""" SELECT f.id, f.fingerprint FROM fingerprint f WHERE f.keyring IS NULL AND NOT EXISTS (SELECT 1 FROM binaries b WHERE b.sig_fpr = f.id) - AND NOT EXISTS (SELECT 1 FROM source s WHERE s.sig_fpr = f.id)""") + AND NOT EXISTS (SELECT 1 FROM source s WHERE s.sig_fpr = f.id) + AND NOT EXISTS (SELECT 1 FROM acl_per_source aps WHERE aps.created_by_id = f.id)""") count = 0 @@ -382,7 +382,39 @@ SELECT f.id, f.fingerprint FROM fingerprint f if count > 0: Logger.log(["total", count]) - print "Cleared out %d fingerprint entries." % (count) + +################################################################################ + +def clean_byhash(now_date, session): + cnf = Config() + suite_suffix = cnf.find("Dinstall::SuiteSuffix", "") + + Logger.log(["Cleaning out unused by-hash files..."]) + + q = session.execute(""" + DELETE FROM hashfile h + USING suite s, archive a + WHERE s.id = h.suite_id + AND a.id = s.archive_id + AND h.unreferenced + a.stayofexecution < CURRENT_TIMESTAMP + RETURNING a.path, s.suite_name, h.path""") + count = q.rowcount + + if not Options["No-Action"]: + for base, suite, path in q: + filename = os.path.join(base, 'dists', suite, suite_suffix, path) + try: + os.unlink(filename) + except OSError as exc: + if exc.errno != errno.ENOENT: + raise + Logger.log(['database referred to non-existing file', filename]) + else: + Logger.log(['delete hashfile', suite, path]) + session.commit() + + if count > 0: + Logger.log(["total", count]) ################################################################################ @@ -391,7 +423,7 @@ def clean_empty_directories(session): Removes empty directories from pool directories. """ - print "Cleaning out empty directories..." + Logger.log(["Cleaning out empty directories..."]) count = 0 @@ -488,6 +520,7 @@ def main(): clean(now_date, archives, max_delete, session) clean_maintainers(now_date, session) clean_fingerprints(now_date, session) + clean_byhash(now_date, session) clean_empty_directories(session) session.rollback()