allow packages to request no submodules be updated (#40409)

* allow packages to request no submodules be updated when self.submodules is a
  callable function

* Extend the test added in Allow more fine-grained control over what submodules are
  updated: part 2 #27293 to include this case

* Update the type signature for the submodules arg of version() in directives.py

---------

Co-authored-by: tjfulle <tjfulle@users.noreply.github.com>
This commit is contained in:
Tim Fuller 2024-02-15 23:36:37 -07:00 committed by GitHub
parent d31e503e5b
commit 79087d08d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 5 deletions

View file

@ -34,7 +34,7 @@ class OpenMpi(Package):
import functools
import os.path
import re
from typing import Any, Callable, List, Optional, Set, Tuple, Union
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Set, Tuple, Union
import llnl.util.lang
import llnl.util.tty.color
@ -57,6 +57,9 @@ class OpenMpi(Package):
VersionLookupError,
)
if TYPE_CHECKING:
import spack.package_base
__all__ = [
"DirectiveError",
"DirectiveMeta",
@ -349,6 +352,7 @@ def remove_directives(arg):
return _decorator
SubmoduleCallback = Callable[["spack.package_base.PackageBase"], Union[str, List[str], bool]]
directive = DirectiveMeta.directive
@ -380,7 +384,7 @@ def version(
tag: Optional[str] = None,
branch: Optional[str] = None,
get_full_repo: Optional[bool] = None,
submodules: Optional[bool] = None,
submodules: Union[SubmoduleCallback, Optional[bool]] = None,
submodules_delete: Optional[bool] = None,
# other version control
svn: Optional[str] = None,

View file

@ -929,9 +929,12 @@ def clone(self, dest=None, commit=None, branch=None, tag=None, bare=False):
git_commands = []
submodules = self.submodules
if callable(submodules):
submodules = list(submodules(self.package))
git_commands.append(["submodule", "init", "--"] + submodules)
git_commands.append(["submodule", "update", "--recursive"])
submodules = submodules(self.package)
if submodules:
if isinstance(submodules, str):
submodules = [submodules]
git_commands.append(["submodule", "init", "--"] + submodules)
git_commands.append(["submodule", "update", "--recursive"])
elif submodules:
git_commands.append(["submodule", "update", "--init", "--recursive"])

View file

@ -363,3 +363,30 @@ def test_gitsubmodules_delete(
assert not os.path.isdir(file_path)
file_path = os.path.join(s.package.stage.source_path, "third_party/submodule1")
assert not os.path.isdir(file_path)
@pytest.mark.disable_clean_stage_check
def test_gitsubmodules_falsey(
mock_git_repository, default_mock_concretization, mutable_mock_repo, monkeypatch
):
"""
Test GitFetchStrategy behavior when callable submodules returns Falsey
"""
def submodules_callback(package):
return False
type_of_test = "tag-branch"
t = mock_git_repository.checks[type_of_test]
# Construct the package under test
s = default_mock_concretization("git-test")
args = copy.copy(t.args)
args["submodules"] = submodules_callback
monkeypatch.setitem(s.package.versions, Version("git"), args)
s.package.do_stage()
with working_dir(s.package.stage.source_path):
file_path = os.path.join(s.package.stage.source_path, "third_party/submodule0/r0_file_0")
assert not os.path.isfile(file_path)
file_path = os.path.join(s.package.stage.source_path, "third_party/submodule1/r0_file_1")
assert not os.path.isfile(file_path)