Skip to content

Commit ee1bde8

Browse files
committed
change the method of file hashing to be compatible with Windows and ignore trailing newlines
1 parent 5ae36ab commit ee1bde8

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

entangled/filedb.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
from .utility import normal_relative, ensure_parent
1717

1818

19+
def hexdigest(s: str) -> str:
20+
"""Creates a MD5 hash digest from a string. Before hashing, the string has
21+
linefeed `\\r` characters and trailing newlines removed, and the string
22+
is encoded as UTF-8."""
23+
content = s.replace("\r", "").rstrip("\n").encode()
24+
return hashlib.sha256(content).hexdigest()
25+
26+
1927
@dataclass
2028
class FileStat:
2129
path: Path
@@ -26,10 +34,10 @@ class FileStat:
2634
@staticmethod
2735
def from_path(path: Path, deps: Optional[list[Path]]):
2836
stat = os.stat(path)
29-
with open(path, "rb") as f:
30-
hash = hashlib.sha256(f.read())
37+
with open(path, "r") as f:
38+
digest = hexdigest(f.read())
3139
return FileStat(
32-
path, deps, datetime.fromtimestamp(stat.st_mtime), hash.hexdigest()
40+
path, deps, datetime.fromtimestamp(stat.st_mtime), digest
3341
)
3442

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

138146
def check(self, path: Path, content: str) -> bool:
139-
return (
140-
hashlib.sha256(content.encode()).hexdigest() == self._files[path].hexdigest
141-
)
147+
return hexdigest(content) == self._files[path].hexdigest
142148

143149
@staticmethod
144150
def initialize() -> FileDB:

0 commit comments

Comments
 (0)