Skip to content

Commit de311d4

Browse files
committed
Allow to choose the Matcher from the command line
Signed-off-by: Olivier Mehani <shtrom@ssji.net>
1 parent eb65492 commit de311d4

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

rotate_backups/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ def __init__(self, rotation_scheme, **options):
353353
and :attr:`strict`).
354354
"""
355355
options.update(rotation_scheme=rotation_scheme)
356+
# Force re-setting the FilenameMatcher
357+
self.timestamp_pattern = self.timestamp_pattern
356358
super(RotateBackups, self).__init__(**options)
357359

358360
@mutable_property
@@ -556,6 +558,19 @@ def timestamp_pattern(self, value):
556558
if component not in pattern.groupindex and required:
557559
raise ValueError("Pattern is missing required capture group! (%s)" % component)
558560
set_property(self, 'timestamp_pattern', pattern)
561+
set_property(self, 'matcher', FilenameMatcher(self.timestamp_pattern))
562+
563+
@mutable_property
564+
def unix_timestamp(self):
565+
return type(self.matcher) == TimestampMatcher
566+
567+
@unix_timestamp.setter
568+
def unix_timestamp(self, value):
569+
if value:
570+
set_property(self, 'matcher', TimestampMatcher())
571+
else:
572+
# Force re-setting the FilenameMatcher
573+
self.timestamp_pattern = self.timestamp_pattern
559574

560575
def rotate_concurrent(self, *locations, **kw):
561576
"""
@@ -719,9 +734,8 @@ def collect_backups(self, location):
719734
logger.info("Scanning %s for backups ..", location)
720735
location.ensure_readable(self.force)
721736

722-
matcher = FilenameMatcher(self.timestamp_pattern)
723737
for entry in natsort(location.context.list_entries(location.directory)):
724-
match = matcher.search(entry)
738+
match = self.matcher.search(entry)
725739
if match:
726740
if self.exclude_list and any(fnmatch.fnmatch(entry, p) for p in self.exclude_list):
727741
logger.verbose("Excluded %s (it matched the exclude list).", entry)

rotate_backups/cli.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@
168168
readable and/or writable for the current user (or the user logged in to a
169169
remote system over SSH).
170170
171+
-U, --unix-timestamp
172+
173+
Consider using mtime timestamps, instead of filenames, to determine the
174+
date of each file.
175+
171176
-S, --syslog=CHOICE
172177
173178
Explicitly enable or disable system logging instead of letting the program
@@ -241,12 +246,12 @@ def main():
241246
selected_locations = []
242247
# Parse the command line arguments.
243248
try:
244-
options, arguments = getopt.getopt(sys.argv[1:], 'M:H:d:w:m:y:t:I:x:jpri:c:C:uS:fnvqh', [
249+
options, arguments = getopt.getopt(sys.argv[1:], 'M:H:d:w:m:y:t:I:x:jpri:c:C:uUS:fnvqh', [
245250
'minutely=', 'hourly=', 'daily=', 'weekly=', 'monthly=', 'yearly=',
246251
'timestamp-pattern=', 'include=', 'exclude=', 'parallel',
247252
'prefer-recent', 'relaxed', 'ionice=', 'config=',
248-
'removal-command=', 'use-sudo', 'syslog=', 'force',
249-
'dry-run', 'verbose', 'quiet', 'help',
253+
'removal-command=', 'use-sudo', 'unix-timestamp', 'syslog=',
254+
'force', 'dry-run', 'verbose', 'quiet', 'help',
250255
])
251256
for option, value in options:
252257
if option in ('-M', '--minutely'):
@@ -284,6 +289,8 @@ def main():
284289
kw['removal_command'] = removal_command
285290
elif option in ('-u', '--use-sudo'):
286291
use_sudo = True
292+
elif option in ('-U', '--unix-timestamp'):
293+
kw['unix_timestamp'] = True
287294
elif option in ('-S', '--syslog'):
288295
use_syslog = coerce_boolean(value)
289296
elif option in ('-f', '--force'):

0 commit comments

Comments
 (0)