Compare commits

..
1 Commits
Author SHA1 Message Date
imSp4rky 90b4fc8550 feat: identify the running code by source hash 2026-07-30 16:30:03 -06:00
3 changed files with 8 additions and 11 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ pytestmark = pytest.mark.unit
def test_is_hex_and_stable() -> None:
first = code_hash()
assert len(first) == 40
assert len(first) == 32
assert int(first, 16) >= 0
assert first == code_hash()
+6 -9
View File
@@ -16,13 +16,12 @@ def _raise(error: OSError) -> None:
def code_files() -> list[str]:
"""Framework source paths relative to the package root, in a platform-stable order."""
pkg = str(_PKG)
rels = ["__main__.py"]
for name in _CODE_DIRS:
for root, dirs, files in os.walk(os.path.join(pkg, name), onerror=_raise):
for root, dirs, files in os.walk(_PKG / name, onerror=_raise):
dirs[:] = [d for d in dirs if d != "__pycache__"]
rel = os.path.relpath(root, pkg).replace(os.sep, "/")
rels.extend(f"{rel}/{f}" for f in files if f.endswith(".py"))
rel = os.path.relpath(root, _PKG)
rels.extend(os.path.join(rel, f).replace(os.sep, "/") for f in files if f.endswith(".py"))
return sorted(rels)
@@ -31,19 +30,17 @@ def code_hash(
read: Optional[Callable[[str], bytes]] = None,
) -> str:
"""
sha1 of the framework source. Two installs on the same version differ here if their code does.
md5 of the framework source. Identifies the running code, not the release it claims to be.
Pass `files` and `read` to digest a different byte source, such as blobs from git history
(see tools/resolve_code_hash.py). Returns "" when the source cannot be read.
"""
if read is None:
pkg = str(_PKG)
def read(rel: str) -> bytes:
with open(os.path.join(pkg, rel), "rb") as fh:
return fh.read()
return (_PKG / rel).read_bytes()
digest = hashlib.sha1(usedforsecurity=False)
digest = hashlib.md5(usedforsecurity=False)
try:
for rel in code_files() if files is None else files:
digest.update(rel.encode())
+1 -1
View File
@@ -63,7 +63,7 @@ def main(version: bool, debug: bool) -> None:
r" ▀▀▀ ▀▀ █▪ ▀▀▀▀ ▀▀▀ · ▀ ▀ ·▀▀▀ ·▀ ▀.▀▀▀ ▀▀▀ ",
style="ascii.art",
),
f"v [repr.number]{__version__}[/]{f' ({__code_hash__})' if __code_hash__ else ''}"
f"v [repr.number]{__version__}[/]{f' (code hash {__code_hash__})' if __code_hash__ else ''}"
f" - © 2025-{datetime.now().year} - github.com/unshackle-dl/unshackle",
),
(1, 11, 1, 10),