]> arthur.barton.de Git - bup.git/blobdiff - cmd/prune-older-cmd.py
INTEGRAL_ASSIGNMENT_FITS: actually provide return value for clang
[bup.git] / cmd / prune-older-cmd.py
index fe45b95c4bef564b31429e5cf101ccc8aa7a3946..96554233644b0bd789456627a31104834dc133a5 100755 (executable)
@@ -5,7 +5,7 @@ exec "$bup_python" "$0" ${1+"$@"}
 """
 # end of bup preamble
 
-from __future__ import print_function
+from __future__ import absolute_import, print_function
 from collections import defaultdict
 from itertools import groupby
 from sys import stderr
@@ -13,14 +13,16 @@ from time import localtime, strftime, time
 import re, sys
 
 from bup import git, options
+from bup.compat import int_types
 from bup.gc import bup_gc
 from bup.helpers import die_if_errors, log, partition, period_as_secs
+from bup.repo import LocalRepo
 from bup.rm import bup_rm
 
 
 def branches(refnames=()):
-    return ((name[11:], sha) for (name,sha)
-            in git.list_refs(refnames=('refs/heads/' + n for n in refnames),
+    return ((name[11:], sha.encode('hex')) for (name,sha)
+            in git.list_refs(patterns=('refs/heads/' + n for n in refnames),
                              limit_to_heads=True))
 
 def save_name(branch, utc):
@@ -46,14 +48,18 @@ def classify_saves(saves, period_start):
                  (period_start['monthlies'], lambda s: localtime(s[0]).tm_mon),
                  (period_start['yearlies'], lambda s: localtime(s[0]).tm_year))
 
-    # Foreach period, seek back from now to the period's starting time, and
-    # collect the most recent saves
+    # Break the decreasing utc sorted saves up into the respective
+    # period ranges (dailies, monthlies, ...).  Within each range,
+    # group the saves by the period scale (days, months, ...), and
+    # then yield a "keep" action (True, utc) for the newest save in
+    # each group, and a "drop" action (False, utc) for the rest.
     for pstart, time_region_id in tm_ranges:
         matches, rest = partition(lambda s: s[0] >= pstart, rest)
         for region_id, region_saves in groupby(matches, time_region_id):
             for action in retain_newest_in_region(list(region_saves)):
                 yield action
 
+    # Finally, drop any saves older than the specified periods
     for save in rest:
         yield False, save
 
@@ -80,8 +86,8 @@ opt, flags, roots = o.parse(sys.argv[1:])
 if not opt.unsafe:
     o.fatal('refusing to run dangerous, experimental command without --unsafe')
 
-now = int(time()) if not opt.wrt else opt.wrt
-if not isinstance(now, (int, long)):
+now = int(time()) if opt.wrt is None else opt.wrt
+if not isinstance(now, int_types):
     o.fatal('--wrt value ' + str(now) + ' is not an integer')
 
 period_start = {}
@@ -126,10 +132,15 @@ git.check_repo_or_die()
 # This could be more efficient, but for now just build the whole list
 # in memory and let bup_rm() do some redundant work.
 
+def parse_info(f):
+    author_secs = f.readline().strip()
+    return int(author_secs)
+
 removals = []
 for branch, branch_id in branches(roots):
     die_if_errors()
-    saves = git.rev_list(branch_id.encode('hex'))
+    saves = ((utc, oidx.decode('hex')) for (oidx, utc) in
+             git.rev_list(branch_id, format='%at', parse=parse_info))
     for keep_save, (utc, id) in classify_saves(saves, period_start):
         assert(keep_save in (False, True))
         # FIXME: base removals on hashes
@@ -140,7 +151,8 @@ for branch, branch_id in branches(roots):
 
 if not opt.pretend:
     die_if_errors()
-    bup_rm(removals, compression=opt.compress, verbosity=opt.verbose)
+    repo = LocalRepo()
+    bup_rm(repo, removals, compression=opt.compress, verbosity=opt.verbose)
     if opt.gc:
         die_if_errors()
         bup_gc(threshold=opt.gc_threshold,