# (*) recent stuff: unchanged
#
+import sys
import os, os.path
-from datetime import datetime
+from datetime import datetime, timedelta
from glob import glob
import re
import traceback
def age_days (self):
return self.age.days
- def age_weeks (self):
- return self.age.days/7
-
# oldest first
@staticmethod
def sort_age (file1, file2):
elif w1!=PREFERRED_WEEKDAY and w2==PREFERRED_WEEKDAY:
return 1
- month_marrier=None
+ month_barrier=None
+ week_barrier=None
@staticmethod
- def compute_month_barrier ():
- if not File.month_marrier:
- # find the exact datetime for a month change
- # KEEP_FILES_IN_MONTH_AFTER months ago +
- year=now.year
- month=now.month
- day=1
- if now.month>=KEEP_FILES_IN_MONTH_AFTER+1:
- month -= KEEP_FILES_IN_MONTH_AFTER
- else:
- year -= 1
- month += (12-KEEP_FILES_IN_MONTH_AFTER)
- File.month_marrier = datetime (year=year, month=month, day=day)
-
- return File.month_marrier
+ def _compute_barriers ():
+ if File.month_barrier:
+ return
+ # find the exact datetime for a month change
+ # KEEP_FILES_IN_MONTH_AFTER months ago +
+ year=now.year
+ month=now.month
+ day=1
+ if now.month>=KEEP_FILES_IN_MONTH_AFTER+1:
+ month -= KEEP_FILES_IN_MONTH_AFTER
+ else:
+ year -= 1
+ month += (12-KEEP_FILES_IN_MONTH_AFTER)
+ File.month_barrier = datetime (year=year, month=month, day=day)
+ # find the next monday morning
+ remaining_days=(7-File.month_barrier.weekday())%7
+ File.week_barrier=File.month_barrier+timedelta(days=remaining_days)
+
+ @staticmethod
+ def compute_month_barrier(): File._compute_barriers(); return File.month_barrier
+ @staticmethod
+ def compute_week_barrier(): File._compute_barriers(); return File.week_barrier
# returns a key for grouping files, the cleanup then
# preserving one entry in the set of files with same group
if self.datetime <= File.compute_month_barrier():
return self.datetime.strftime("%Y%m")
else:
- return "week%d"%self.age_weeks()
+ weeks=(self.datetime-File.compute_week_barrier()).days/7
+ weeks += 1
+ return "week%02d"%weeks
# all files in a given timeslot (either month or week)
class Group:
print " << %s - %s d old"%(f.filename, f.age_days()),
if entries >=2:
f=self.list[-1]
- print ">> %s - %s d old"%(f.filename, f.age_days())
+ print "|| %s - %s d old >>"%(f.filename, f.age_days())
groupnames=self.groups.keys()
groupnames.sort()
groupnames.reverse()
print " %s"%file
elif self.options.verbose:
print " Found %d groups"%len(groupnames),
- for g in groupnames: print "%s->%d"%(k,len(self.groups[g].files)),
+ for g in groupnames: print "%s->%d"%(g,len(self.groups[g].files)),
print ''
# sort on number of entries
help="run in extra verbose mode")
parser.add_option ("-n","--dry-run",dest='dry_run',action='store_true',default=False,
help="dry run")
+ parser.add_option ("-o","--offset",dest='offset',action='store',type='int',default=0,
+ help="pretend we run <offset> days in the future")
(options, args) = parser.parse_args()
if options.extra_verbose: options.verbose=True
+ try:
+ #options.offset=int(options.offset)
+ print 'offset=%d'%options.offset
+ if options.offset !=0:
+ global now
+ now += timedelta(days=options.offset)
+ except:
+ traceback.print_exc()
+ print "Offset not understood %s - expect an int. number of days"%options.offset
+ sys.exit(1)
# args can be directories, or patterns, like
# main /db-backup /db-backup-f8/*bz2