From 00516319a84d3f304e80ad6090db54b9ccd4b5d3 Mon Sep 17 00:00:00 2001 From: Artturin Date: Tue, 17 Oct 2023 06:56:02 +0300 Subject: [PATCH] update source attributes other than `src` --- nix_update/__init__.py | 7 +++++++ nix_update/eval.py | 30 ++++++++++++++++++++++-------- nix_update/options.py | 2 ++ nix_update/update.py | 2 +- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/nix_update/__init__.py b/nix_update/__init__.py index 0e85689..4885d61 100644 --- a/nix_update/__init__.py +++ b/nix_update/__init__.py @@ -87,6 +87,12 @@ def parse_args(args: list[str]) -> Options: help="Attribute name within the file evaluated", ) + parser.add_argument( + "--src-attr", + help="Src attribute", + default="src", + ) + a = parser.parse_args(args) return Options( import_path=os.path.realpath(a.file), @@ -101,6 +107,7 @@ def parse_args(args: list[str]) -> Options: version=a.version, version_preference=VersionPreference.from_str(a.version), attribute=a.attribute, + source_attribute=a.src_attr, test=a.test, version_regex=a.version_regex, review=a.review, diff --git a/nix_update/eval.py b/nix_update/eval.py index 66e1008..c35ba4e 100644 --- a/nix_update/eval.py +++ b/nix_update/eval.py @@ -91,10 +91,20 @@ def __post_init__( def eval_expression( - escaped_import_path: str, attr: str, flake: bool, system: str | None + escaped_import_path: str, + attr: str, + source_attr: str, + flake: bool, + system: str | None, ) -> str: system = f'"{system}"' if system else "builtins.currentSystem" + source_attrs = source_attr.rpartition(".") + source_attr_last = source_attrs[-1] or source_attr + source_attr_all_but_last = ( + f".{source_attrs[0]}" if source_attr_last != source_attr else "" + ) + if flake: let_bindings = f""" inherit (builtins) getFlake stringLength substring; @@ -132,17 +142,17 @@ def eval_expression( else if pkg ? isPhpExtension then raw_version_position else - sanitizePosition (builtins.unsafeGetAttrPos "src" pkg); + sanitizePosition (builtins.unsafeGetAttrPos "{source_attr_last}" pkg{source_attr_all_but_last}); in {{ name = pkg.name; old_version = pkg.version or (builtins.parseDrvName pkg.name).version; inherit raw_version_position; filename = position.file; line = position.line; - urls = pkg.src.urls or null; - url = pkg.src.url or null; - rev = pkg.src.rev or null; - hash = pkg.src.outputHash or null; + urls = pkg.{source_attr}.urls or null; + url = pkg.{source_attr}.url or null; + rev = pkg.{source_attr}.rev or null; + hash = pkg.{source_attr}.outputHash or null; go_modules = pkg.goModules.outputHash or null; go_modules_old = pkg.go-modules.outputHash or null; cargo_deps = pkg.cargoDeps.outputHash or null; @@ -161,14 +171,18 @@ def eval_expression( yarn_deps = pkg.offlineCache.outputHash or null; tests = builtins.attrNames (pkg.passthru.tests or {{}}); has_update_script = {has_update_script}; - src_homepage = pkg.src.meta.homepage or null; + src_homepage = pkg.{source_attr}.meta.homepage or null; changelog = pkg.meta.changelog or null; }}""" def eval_attr(opts: Options) -> Package: expr = eval_expression( - opts.escaped_import_path, opts.escaped_attribute, opts.flake, opts.system + opts.escaped_import_path, + opts.escaped_attribute, + opts.source_attribute, + opts.flake, + opts.system, ) cmd = [ "nix", diff --git a/nix_update/options.py b/nix_update/options.py index e1be59f..3daf0b7 100644 --- a/nix_update/options.py +++ b/nix_update/options.py @@ -8,6 +8,7 @@ @dataclass class Options: attribute: str + source_attribute: str flake: bool = False version: str = "stable" version_preference: VersionPreference = VersionPreference.STABLE @@ -29,4 +30,5 @@ class Options: def __post_init__(self) -> None: self.escaped_attribute = ".".join(map(json.dumps, self.attribute.split("."))) + self.escaped_source_attribute = ".".join(map(json.dumps, self.source_attribute.split("."))) self.escaped_import_path = json.dumps(self.import_path) diff --git a/nix_update/update.py b/nix_update/update.py index 1c80de8..8f09b3b 100644 --- a/nix_update/update.py +++ b/nix_update/update.py @@ -141,7 +141,7 @@ def git_prefetch(x: tuple[str, tuple[str, str]]) -> tuple[str, str]: def update_src_hash(opts: Options, filename: str, current_hash: str) -> None: - target_hash = nix_prefetch(opts, "src") + target_hash = nix_prefetch(opts, opts.source_attribute) replace_hash(filename, current_hash, target_hash)