3 # Copyright (C) 2007 Florian Reitmeir <florian@reitmeir.org>
4 # Copyright (C) 2008 Joerg Jaspert <joerg@debian.org>
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 # requires: python-dateutil
30 from datetime import datetime
31 from datetime import timedelta
32 from optparse import OptionParser
35 {'days':14, 'interval':0},
36 {'days':31, 'interval':7},
37 {'days':365, 'interval':31},
38 {'days':3650, 'interval':365},
40 # keep 14 days, all each day
41 # keep 31 days, 1 each 7th day
42 # keep 365 days, 1 each 31th day
43 # keep 3650 days, 1 each 365th day
46 TODAY = datetime.today()
53 def all_files(pattern, search_path, pathsep=os.pathsep):
54 """ Given a search path, yield all files matching the pattern. """
55 for path in search_path.split(pathsep):
56 for match in glob.glob(os.path.join(path, pattern)):
59 def parse_file_dates(list):
61 # dump_2006.05.02-11:52:01.bz2
62 p = re.compile('^\./dump_([0-9]{4})\.([0-9]{2})\.([0-9]{2})-([0-9]{2}):([0-9]{2}):([0-9]{2})(.xz)?$')
66 d = datetime(int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4)), int(m.group(5)), int(m.group(6)))
67 out.append({'name': file, 'date': d})
70 def prepare_rules(rules):
75 'days':timedelta(days=rule['days']),
76 'interval':timedelta(days=rule['interval'])}
80 def expire(rules, list):
81 t_rules=prepare_rules(rules)
87 print "current file to expire: " + file['name']
90 # check if rule applies
91 if (file['date'] < (TODAY-rule['days'])):
93 print "move to next rule"
97 if (last['date'] - file['date']) < rule['interval']:
99 print "unlink file:" + file['name']
103 os.unlink(file['name'])
107 print "kept file:" + file['name']
110 parser = OptionParser()
111 parser.add_option("-d", "--directory", dest="directory",
112 help="directory name", metavar="Name")
113 parser.add_option("-f", "--pattern", dest="pattern",
114 help="Pattern maybe some glob", metavar="*.backup")
115 parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
117 parser.add_option("-n", "--no-action", action="store_true", dest="noaction", default=False,
118 help="just prints what would be done, this implies verbose")
119 parser.add_option("-p", "--print", action="store_true", dest="printfiles", default=False,
120 help="just print the filenames that should be deleted, this forbids verbose")
122 (options, args) = parser.parse_args()
124 if (not options.directory):
125 parser.error("no directory to check given")
134 if options.printfiles:
138 files = sorted( list(all_files(options.pattern,options.directory)), reverse=True );
143 files_dates = parse_file_dates(files);
144 expire(RULES, files_dates)