Use git cache by default

This commit is contained in:
Michal Sojka 2025-04-18 11:41:38 +02:00
parent c309f53ac9
commit c619c573f2
2 changed files with 28 additions and 12 deletions

View file

@ -116,8 +116,7 @@ usage: ros2nix [-h]
[--output-dir OUTPUT_DIR] [--fetch] [--use-per-package-src] [--output-dir OUTPUT_DIR] [--fetch] [--use-per-package-src]
[--patches | --no-patches] [--distro DISTRO] [--patches | --no-patches] [--distro DISTRO]
[--src-param SRC_PARAM] [--source-root SOURCE_ROOT] [--src-param SRC_PARAM] [--source-root SOURCE_ROOT]
[--cache-file CACHE_FILE] [--do-check] [--no-cache] [--do-check] [--extra-build-inputs DEP1,DEP2,...]
[--extra-build-inputs DEP1,DEP2,...]
[--extra-propagated-build-inputs DEP1,DEP2,...] [--extra-propagated-build-inputs DEP1,DEP2,...]
[--extra-check-inputs DEP1,DEP2,...] [--extra-check-inputs DEP1,DEP2,...]
[--extra-native-build-inputs DEP1,DEP2,...] [--flake] [--extra-native-build-inputs DEP1,DEP2,...] [--flake]
@ -173,10 +172,8 @@ options:
Set sourceRoot attribute value in the generated Nix Set sourceRoot attribute value in the generated Nix
expression. Substring '{package_name}' gets replaced expression. Substring '{package_name}' gets replaced
with the package name. (default: None) with the package name. (default: None)
--cache-file CACHE_FILE --no-cache Don't use cache of git checkout sha265 hashes across
Path to a json-file to store sha265 hashes of generation runs. (default: False)
checkouts persistently to cache them across generation
runs. (default: None)
--do-check Set doCheck attribute to true (default: False) --do-check Set doCheck attribute to true (default: False)
--extra-build-inputs DEP1,DEP2,... --extra-build-inputs DEP1,DEP2,...
Additional dependencies to add to the generated Nix Additional dependencies to add to the generated Nix

View file

@ -14,8 +14,8 @@ import os
import re import re
import subprocess import subprocess
import sys import sys
import json
from contextlib import contextmanager from contextlib import contextmanager
from pathlib import Path
from textwrap import dedent, indent from textwrap import dedent, indent
from typing import Iterable, Set, List from typing import Iterable, Set, List
@ -27,6 +27,23 @@ from superflore.utils import err, ok, resolve_dep, warn
from .nix_expression import NixExpression, NixLicense from .nix_expression import NixExpression, NixLicense
# Copied from https://github.com/srstevenson/xdg-base-dirs
# Copyright © Scott Stevenson <scott@stevenson.io>
# Less than 10 lines, no need to mention full ISC license here.
def _path_from_env(variable: str, default: Path) -> Path:
if (value := os.environ.get(variable)) and (path := Path(value)).is_absolute():
return path
return default
def xdg_cache_home() -> Path:
"""Return a Path corresponding to XDG_CACHE_HOME."""
return _path_from_env("XDG_CACHE_HOME", Path.home() / ".cache")
cache_file = xdg_cache_home() / "ros2nix" / "git-cache.json"
def resolve_dependencies(deps: Iterable[str]) -> Set[str]: def resolve_dependencies(deps: Iterable[str]) -> Set[str]:
return set(itertools.chain.from_iterable(map(resolve_dependency, deps))) return set(itertools.chain.from_iterable(map(resolve_dependency, deps)))
@ -282,7 +299,9 @@ def ros2nix(args):
"Substring '{package_name}' gets replaced with the package name.", "Substring '{package_name}' gets replaced with the package name.",
) )
parser.add_argument( parser.add_argument(
"--cache-file", help="Path to a json-file to store sha265 hashes of checkouts persistently to cache them across generation runs." "--no-cache",
action="store_true",
help="Don't use cache of git checkout sha265 hashes across generation runs.",
) )
parser.add_argument( parser.add_argument(
"--do-check", "--do-check",
@ -361,8 +380,8 @@ def ros2nix(args):
expressions: dict[str, str] = {} expressions: dict[str, str] = {}
git_cache = {} git_cache = {}
if args.cache_file is not None and os.path.exists(args.cache_file): if not args.no_cache and os.path.exists(cache_file):
with open(args.cache_file) as f: with open(cache_file) as f:
git_cache = json.load(f) git_cache = json.load(f)
patch_filenames = set() patch_filenames = set()
@ -581,8 +600,8 @@ def ros2nix(args):
generate_default(args) generate_default(args)
# TODO generate also release.nix (for testing/CI)? # TODO generate also release.nix (for testing/CI)?
if args.cache_file is not None: if not args.no_cache:
with open(args.cache_file, "w") as f: with open(cache_file, "w") as f:
json.dump(git_cache, f) json.dump(git_cache, f)
if args.compare and compare_failed: if args.compare and compare_failed: