feat: add the option to store sha256 cache in a file

cache is also used for per package source checkouts and
<git-rev>-<package-prefix> is used as a key
This commit is contained in:
Max Beutelspacher 2025-02-25 21:16:32 +01:00
parent c35b0dc7aa
commit c309f53ac9
2 changed files with 31 additions and 9 deletions

View file

@ -14,6 +14,7 @@ import os
import re
import subprocess
import sys
import json
from contextlib import contextmanager
from textwrap import dedent, indent
from typing import Iterable, Set, List
@ -280,6 +281,9 @@ def ros2nix(args):
help="Set sourceRoot attribute value in the generated Nix expression. "
"Substring '{package_name}' gets replaced with the package name.",
)
parser.add_argument(
"--cache-file", help="Path to a json-file to store sha265 hashes of checkouts persistently to cache them across generation runs."
)
parser.add_argument(
"--do-check",
action="store_true",
@ -357,6 +361,9 @@ def ros2nix(args):
expressions: dict[str, str] = {}
git_cache = {}
if args.cache_file is not None and os.path.exists(args.cache_file):
with open(args.cache_file) as f:
git_cache = json.load(f)
patch_filenames = set()
for source in args.source:
@ -418,15 +425,21 @@ def ros2nix(args):
merge_base = merge_base_to_upstream(head)
head = check_output(f"git rev-list {merge_base} -1 -- .".split())
if not args.use_per_package_src and toplevel in git_cache: # only use cache if not using separate checkout per package
info = git_cache[toplevel]
def cache_key(prefix, rev):
if args.use_per_package_src:
return f"{prefix}-{rev}"
return rev
# Latest commit present in the upstream repo. If
# the local repository doesn't have additional
# commits, it is the same as HEAD. Should work
# even with detached HEAD.
upstream_rev = merge_base_to_upstream(head)
if cache_key(prefix, upstream_rev) in git_cache:
info = git_cache[cache_key(prefix, upstream_rev)]
upstream_rev = info["rev"]
else:
# Latest commit present in the upstream repo. If
# the local repository doesn't have additional
# commits, it is the same as HEAD. Should work
# even with detached HEAD.
upstream_rev = merge_base_to_upstream(head)
info = json.loads(
subprocess.check_output(
["nix-prefetch-git", "--quiet"]
@ -438,7 +451,7 @@ def ros2nix(args):
+ [toplevel, upstream_rev],
).decode()
)
git_cache[toplevel] = info
git_cache[cache_key(prefix, upstream_rev)] = {k : info[k] for k in ["rev", "sha256"]}
match = re.match("https://github.com/(?P<owner>[^/]*)/(?P<repo>.*?)(.git|/.*)?$", url)
sparse_checkout = f"""sparseCheckout = ["{prefix}"];
@ -568,6 +581,10 @@ def ros2nix(args):
generate_default(args)
# TODO generate also release.nix (for testing/CI)?
if args.cache_file is not None:
with open(args.cache_file, "w") as f:
json.dump(git_cache, f)
if args.compare and compare_failed:
err("Some files are not up-to-date")
return 2