-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from collections import namedtuple | ||
import logging | ||
import os | ||
import time | ||
|
||
lgr = logging.getLogger(__name__) | ||
|
||
|
||
class FileFingerprint(namedtuple("FileFingerprint", "mtime_ns ctime_ns size inode")): | ||
@classmethod | ||
def for_file(cls, path): | ||
"""Simplistic generic file fingerprinting based on ctime, mtime, and size | ||
""" | ||
try: | ||
# we can't take everything, since atime can change, etc. | ||
# So let's take some | ||
s = os.stat(path, follow_symlinks=True) | ||
fprint = cls.from_stat(s) | ||
lgr.log(5, "Fingerprint for %s: %s", path, fprint) | ||
return fprint | ||
except Exception as exc: | ||
lgr.debug(f"Cannot fingerprint {path}: {exc}") | ||
|
||
@classmethod | ||
def from_stat(cls, s): | ||
return cls(s.st_mtime_ns, s.st_ctime_ns, s.st_size, s.st_ino) | ||
|
||
def modified_in_window(self, min_dtime): | ||
return abs(time.time() - self.mtime_ns * 1e-9) < min_dtime | ||
|
||
def to_tuple(self): | ||
return tuple(self) | ||
|
||
|
||
class DirFingerprint: | ||
def __init__(self): | ||
self.last_modified = None | ||
self.tree_fprints = {} | ||
|
||
def add_file(self, path, fprint: FileFingerprint): | ||
self.tree_fprints[path] = fprint | ||
if self.last_modified is None or self.last_modified < fprint.mtime_ns: | ||
self.last_modified = fprint.mtime_ns | ||
|
||
def modified_in_window(self, min_dtime): | ||
if self.last_modified is None: | ||
return False | ||
else: | ||
return abs(time.time() - self.last_modified * 1e-9) < min_dtime | ||
|
||
def to_tuple(self): | ||
return sum(sorted(self.tree_fprints.items()), ()) |