From 04e84ad284247e28d2fcf140952cea51acdb2c57 Mon Sep 17 00:00:00 2001 From: Ashley Milsted Date: Sun, 20 Oct 2024 05:48:52 -0700 Subject: [PATCH] Add hashing of deps files (#36) * 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 Co-authored-by: Christopher Doris --- .pre-commit-config.yaml | 2 +- src/juliapkg/deps.py | 18 ++++++++++++++---- test/test_compat.py | 1 + 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fe9ba38..a34ed84 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/src/juliapkg/deps.py b/src/juliapkg/deps.py index c57c7c5..39ef339 100644 --- a/src/juliapkg/deps.py +++ b/src/juliapkg/deps.py @@ -1,3 +1,4 @@ +import hashlib import json import logging import os @@ -13,7 +14,7 @@ ### META -META_VERSION = 4 # increment whenever the format changes +META_VERSION = 5 # increment whenever the format changes def load_meta(): @@ -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() @@ -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 @@ -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], diff --git a/test/test_compat.py b/test/test_compat.py index 3d15b31..b5f3349 100644 --- a/test/test_compat.py +++ b/test/test_compat.py @@ -1,4 +1,5 @@ import pytest + from juliapkg.compat import Compat, Range, Version v = Version.parse