Skip to content

Commit 5f80c94

Browse files
committed
Add FilestatMatcher
Signed-off-by: Olivier Mehani <shtrom@ssji.net>
1 parent 7664f28 commit 5f80c94

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

rotate_backups/__init__.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,33 @@ def timestamp_pattern(self, value):
346346
set_property(self, 'timestamp_pattern', pattern)
347347

348348

349-
class RotateBackups(PropertyManager):
349+
class FilestatMatch(Match):
350+
entry: str = None
351+
field: str = None
352+
353+
def __init__(self, entry: str, timestamp='mtime'):
354+
self.entry = entry
355+
self.field = f'st_{timestamp}'
356+
357+
def match_to_datetime(self) -> datetime.datetime:
358+
return datetime.datetime.fromtimestamp(
359+
getattr(os.stat(self.entry), self.field)
360+
)
361+
362+
363+
class FilestatMatcher(Matcher):
364+
timestamp: str = None
365+
366+
def __init__(self, timestamp: str = 'mtime'):
367+
self.timestamp = timestamp
350368

369+
def search(self, location: str, entry: str) -> FilestatMatch:
370+
return FilestatMatch(
371+
os.path.join(location.directory, entry),
372+
self.timestamp)
373+
374+
375+
class RotateBackups(PropertyManager):
351376
"""Python API for the ``rotate-backups`` program."""
352377

353378
_matcher: Matcher = FilenameMatcher(TIMESTAMP_PATTERN)
@@ -366,6 +391,9 @@ def __init__(self, rotation_scheme, **options):
366391
"""
367392
options.update(rotation_scheme=rotation_scheme)
368393
super(RotateBackups, self).__init__(**options)
394+
if self.stat_timestamp:
395+
logger.info("Using file mtime to determine file date")
396+
self.matcher = FilestatMatcher()
369397

370398
@mutable_property
371399
def config_file(self):
@@ -506,6 +534,12 @@ def rotation_scheme(self):
506534
in the dictionary.
507535
"""
508536

537+
@mutable_property
538+
def stat_timestamp(self):
539+
"""
540+
Whether to use the files' mtime instead of parsing their name.
541+
"""
542+
509543
@mutable_property
510544
def strict(self):
511545
"""

0 commit comments

Comments
 (0)