From c619c573f2db7f8a9f5f7b4fbbecb9fc808f5fcb Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Fri, 18 Apr 2025 11:41:38 +0200 Subject: [PATCH] Use git cache by default --- README.md | 9 +++------ ros2nix/ros2nix.py | 31 +++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bddd2cd..b05a4c6 100644 --- a/README.md +++ b/README.md @@ -116,8 +116,7 @@ usage: ros2nix [-h] [--output-dir OUTPUT_DIR] [--fetch] [--use-per-package-src] [--patches | --no-patches] [--distro DISTRO] [--src-param SRC_PARAM] [--source-root SOURCE_ROOT] - [--cache-file CACHE_FILE] [--do-check] - [--extra-build-inputs DEP1,DEP2,...] + [--no-cache] [--do-check] [--extra-build-inputs DEP1,DEP2,...] [--extra-propagated-build-inputs DEP1,DEP2,...] [--extra-check-inputs DEP1,DEP2,...] [--extra-native-build-inputs DEP1,DEP2,...] [--flake] @@ -173,10 +172,8 @@ options: Set sourceRoot attribute value in the generated Nix expression. Substring '{package_name}' gets replaced with the package name. (default: None) - --cache-file CACHE_FILE - Path to a json-file to store sha265 hashes of - checkouts persistently to cache them across generation - runs. (default: None) + --no-cache Don't use cache of git checkout sha265 hashes across + generation runs. (default: False) --do-check Set doCheck attribute to true (default: False) --extra-build-inputs DEP1,DEP2,... Additional dependencies to add to the generated Nix diff --git a/ros2nix/ros2nix.py b/ros2nix/ros2nix.py index 64a757e..157ef24 100755 --- a/ros2nix/ros2nix.py +++ b/ros2nix/ros2nix.py @@ -14,8 +14,8 @@ import os import re import subprocess import sys -import json from contextlib import contextmanager +from pathlib import Path from textwrap import dedent, indent 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 +# Copied from https://github.com/srstevenson/xdg-base-dirs +# Copyright © Scott Stevenson +# 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]: 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.", ) 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( "--do-check", @@ -361,8 +380,8 @@ 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: + if not args.no_cache and os.path.exists(cache_file): + with open(cache_file) as f: git_cache = json.load(f) patch_filenames = set() @@ -581,8 +600,8 @@ 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: + if not args.no_cache: + with open(cache_file, "w") as f: json.dump(git_cache, f) if args.compare and compare_failed: