Skip to content

Commit

Permalink
change the method of file hashing to be compatible with Windows and i…
Browse files Browse the repository at this point in the history
…gnore trailing newlines
  • Loading branch information
jhidding committed Oct 7, 2023
1 parent 5ae36ab commit ee1bde8
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions entangled/filedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
from .utility import normal_relative, ensure_parent


def hexdigest(s: str) -> str:
"""Creates a MD5 hash digest from a string. Before hashing, the string has
linefeed `\\r` characters and trailing newlines removed, and the string
is encoded as UTF-8."""
content = s.replace("\r", "").rstrip("\n").encode()
return hashlib.sha256(content).hexdigest()


@dataclass
class FileStat:
path: Path
Expand All @@ -26,10 +34,10 @@ class FileStat:
@staticmethod
def from_path(path: Path, deps: Optional[list[Path]]):
stat = os.stat(path)
with open(path, "rb") as f:
hash = hashlib.sha256(f.read())
with open(path, "r") as f:
digest = hexdigest(f.read())
return FileStat(
path, deps, datetime.fromtimestamp(stat.st_mtime), hash.hexdigest()
path, deps, datetime.fromtimestamp(stat.st_mtime), digest
)

def __lt__(self, other: FileStat) -> bool:
Expand Down Expand Up @@ -136,9 +144,7 @@ def files(self) -> Iterable[Path]:
return self._files.keys()

def check(self, path: Path, content: str) -> bool:
return (
hashlib.sha256(content.encode()).hexdigest() == self._files[path].hexdigest
)
return hexdigest(content) == self._files[path].hexdigest

@staticmethod
def initialize() -> FileDB:
Expand Down

0 comments on commit ee1bde8

Please sign in to comment.