spec: simplify string formatting (#46609)

This PR shorten the string representation for concrete specs,
in order to make it more legible.

Signed-off-by: Massimiliano Culpo <massimiliano.culpo@gmail.com>
This commit is contained in:
Massimiliano Culpo 2024-09-27 21:59:14 +02:00 committed by GitHub
parent cd5c85ba13
commit 639854ba8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 20 additions and 18 deletions

View File

@ -4016,8 +4016,12 @@ def format_path(
return str(path_ctor(*output_path_components))
def __str__(self):
if self._concrete:
return self.format("{name}{@version}{/hash:7}")
if not self._dependencies:
return self.format()
root_str = [self.format()]
sorted_dependencies = sorted(
self.traverse(root=False), key=lambda x: (x.name, x.abstract_hash)

View File

@ -676,11 +676,13 @@ def test_build_manifest_visitor(tmpdir):
assert all(os.path.islink(f) for f in visitor.symlinks)
def test_text_relocate_if_needed(install_mockery, mock_fetch, monkeypatch, capfd):
spec = Spec("needs-text-relocation").concretized()
install_cmd(str(spec))
def test_text_relocate_if_needed(install_mockery, temporary_store, mock_fetch, monkeypatch, capfd):
install_cmd("needs-text-relocation")
specs = temporary_store.db.query("needs-text-relocation")
assert len(specs) == 1
manifest = get_buildfile_manifest(specs[0])
manifest = get_buildfile_manifest(spec)
assert join_path("bin", "exe") in manifest["text_to_relocate"]
assert join_path("bin", "otherexe") not in manifest["text_to_relocate"]
assert join_path("bin", "secretexe") not in manifest["text_to_relocate"]

View File

@ -933,15 +933,16 @@ def test_push_to_build_cache(
"""
)
env_cmd("create", "test", "./spack.yaml")
with ev.read("test"):
concrete_spec = Spec("patchelf").concretized()
with ev.read("test") as current_env:
current_env.concretize()
install_cmd("--keep-stage")
concrete_spec = list(current_env.roots())[0]
spec_json = concrete_spec.to_json(hash=ht.dag_hash)
json_path = str(tmp_path / "spec.json")
with open(json_path, "w") as ypfd:
ypfd.write(spec_json)
install_cmd("--add", "--keep-stage", json_path)
for s in concrete_spec.traverse():
ci.push_to_build_cache(s, mirror_url, True)

View File

@ -16,8 +16,10 @@ def modulefile_content(request):
"""Returns a function that generates the content of a module file as a list of lines."""
writer_cls = getattr(request.module, "writer_cls")
def _impl(spec_str, module_set_name="default", explicit=True):
spec = spack.spec.Spec(spec_str).concretized()
def _impl(spec_like, module_set_name="default", explicit=True):
if isinstance(spec_like, str):
spec_like = spack.spec.Spec(spec_like)
spec = spec_like.concretized()
generator = writer_cls(spec, module_set_name, explicit)
generator.write(overwrite=True)
written_module = pathlib.Path(generator.layout.filename)

View File

@ -388,7 +388,7 @@ def test_setup_environment(self, modulefile_content, module_configuration):
spec = spack.spec.Spec("mpileaks")
spec.concretize()
content = modulefile_content(str(spec["callpath"]))
content = modulefile_content(spec["callpath"])
assert len([x for x in content if "setenv FOOBAR" in x]) == 1
assert len([x for x in content if "setenv FOOBAR {callpath}" in x]) == 1

View File

@ -715,13 +715,6 @@ def test_exceptional_paths_for_constructor(self):
def test_spec_formatting(self, default_mock_concretization):
spec = default_mock_concretization("multivalue-variant cflags=-O2")
# Since the default is the full spec see if the string rep of
# spec is the same as the output of spec.format()
# ignoring whitespace (though should we?) and ignoring dependencies
spec_string = str(spec)
idx = spec_string.index(" ^")
assert spec_string[:idx] == spec.format().strip()
# Testing named strings ie {string} and whether we get
# the correct component
# Mixed case intentional to test both