Skip to content

Commit

Permalink
Add hashing of deps files (#36)
Browse files Browse the repository at this point in the history
* hashing of deps files

* don't assume hash is present in meta

* formatting

* sort imports

* compat

* oops

* bump META_VERSION

* upgrade ruff

---------

Co-authored-by: Ashley Milsted <[email protected]>
Co-authored-by: Christopher Doris <github.com/cjdoris>
  • Loading branch information
amilsted and Ashley Milsted authored Oct 20, 2024
1 parent febe7d5 commit 04e84ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.4.4
rev: v0.7.0
hooks:
# Run the formatter.
- id: ruff-format
Expand Down
18 changes: 14 additions & 4 deletions src/juliapkg/deps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import json
import logging
import os
Expand All @@ -13,7 +14,7 @@

### META

META_VERSION = 4 # increment whenever the format changes
META_VERSION = 5 # increment whenever the format changes


def load_meta():
Expand Down Expand Up @@ -108,6 +109,11 @@ def depsdict(self):
return ans


def _get_hash(filename):
with open(filename, "rb") as f:
return hashlib.sha256(f.read()).hexdigest()


def can_skip_resolve():
# resolve if we haven't resolved before
deps = load_meta()
Expand Down Expand Up @@ -155,8 +161,9 @@ def can_skip_resolve():
logger.debug("deps file no longer exists %r", filename)
return False
if os.path.getmtime(filename) > fileinfo["timestamp"]:
logger.debug("deps file has changed %r", filename)
return False
if _get_hash(filename) != fileinfo["hash_sha256"]:
logger.debug("deps file has changed %r", filename)
return False
return deps


Expand Down Expand Up @@ -353,7 +360,10 @@ def resolve(force=False, dry_run=False):
"version": str(ver),
"executable": exe,
"deps_files": {
filename: {"timestamp": os.path.getmtime(filename)}
filename: {
"timestamp": os.path.getmtime(filename),
"hash_sha256": _get_hash(filename),
}
for filename in deps_files()
},
"pkgs": [pkg.dict() for pkg in pkgs],
Expand Down
1 change: 1 addition & 0 deletions test/test_compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from juliapkg.compat import Compat, Range, Version

v = Version.parse
Expand Down

0 comments on commit 04e84ad

Please sign in to comment.