From 9b328772a63c14ed10fe2683c077bed173affc14 Mon Sep 17 00:00:00 2001 From: Philip Sakievich Date: Wed, 12 Mar 2025 21:19:24 -0600 Subject: [PATCH] Handler for overlapping attributes --- lib/spack/spack/fetch_strategy.py | 23 ++++++++--------------- lib/spack/spack/package_base.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/spack/spack/fetch_strategy.py b/lib/spack/spack/fetch_strategy.py index 61f4608af91..a540ffa37c7 100644 --- a/lib/spack/spack/fetch_strategy.py +++ b/lib/spack/spack/fetch_strategy.py @@ -1617,24 +1617,17 @@ def for_package_version(pkg, version=None): else: ref_type = "commit" if version.is_commit else "tag" ref_value = version.ref - kwargs = {ref_type: ref_value, "no_cache": True} - kwargs["git"] = pkg.git - version_attrs = pkg.versions.get(version) - if version_attrs and version_attrs.get("git"): - kwargs["git"] = version_attrs["git"] - - kwargs["submodules"] = getattr(pkg, "submodules", False) + kwargs = {ref_type: ref_value, "no_cache": ref_type != "commit"} + kwargs["git"] = pkg.version_or_package_attr("git", version) + kwargs["submodules"] = pkg.version_or_package_attr("submodules", version, False) # if the ref_version is a known version from the package, use that version's - # submodule specifications - if hasattr(pkg.version, "ref_version"): - ref_version_attributes = pkg.versions.get(pkg.version.ref_version) - if ref_version_attributes: - kwargs["submodules"] = ref_version_attributes.get( - "submodules", kwargs["submodules"] - ) - kwargs["git"] = ref_version_attributes.get("git", kwargs["git"]) + # attributes + ref_version = getattr(pkg.version, "ref_version") + if ref_version: + kwargs["git"] = pkg.version_or_package_attr("git", ref_version) + kwargs["submodules"] = pkg.version_or_package_attr("submodules", ref_version, False) fetcher = GitFetchStrategy(**kwargs) return fetcher diff --git a/lib/spack/spack/package_base.py b/lib/spack/spack/package_base.py index 7877c74dcbf..58892eb7ffc 100644 --- a/lib/spack/spack/package_base.py +++ b/lib/spack/spack/package_base.py @@ -989,6 +989,18 @@ def detect_dev_src_change(self) -> bool: assert dev_path_var and record, "dev_path variant and record must be present" return fsys.recursive_mtime_greater_than(dev_path_var.value, record.installation_time) + def version_or_package_attr(self, attr, version, default=None): + """ + Get an attribute that could be on the version or package with preference to the version + """ + version_attrs = self.versions.get(version) + if version_attrs and attr in version_attrs: + return version_attrs.get(attr) + value = getattr(self, attr, default) + if value is None: + raise PackageError(f"{attr} attribute not defined on {self.name}") + return value + @property def needs_commit(self) -> bool: """