Skip to content

Commit a8d1f82

Browse files
committed
Dump quoted strings for prerelease projections, updated expected output
1 parent d339d37 commit a8d1f82

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

scripts/spack_manifest/injection/prerelease.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@
1212
)
1313

1414

15+
# PyYaml by default dumps unquoted strings if they look unambiguous, and quoted strings otherwise.
16+
# Unquoted strings, for some reason, cause issues when they are used as projections in spack manifests.
17+
# PyYaml dumps '{name}/prX-Y' as a quoted str as it has '{' at the front and causes ambiguity (good for projections)
18+
# But 'ROOT_SPEC/prX-Y/VERSION-{hash:7}' is dumped as an unquoted str as it is unambiguous (bad for projections)
19+
# So we need to wrap projections in a custom class that forces PyYaml to dump them as quoted strings.
20+
class YamlExplicitQuotedString(str):
21+
pass
22+
23+
24+
def yaml_explicit_quoted_string_representer(dumper, data):
25+
"""
26+
Custom representer for YAML to ensure that some strings are quoted explicitly.
27+
This is necessary for strings that are used as projections in spack manifests.
28+
"""
29+
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="'")
30+
31+
32+
yaml.add_representer(YamlExplicitQuotedString, yaml_explicit_quoted_string_representer)
33+
34+
### Actual methods begin here ###
35+
36+
1537
def inject_prerelease_information(
1638
manifest_path: str,
1739
version: str,
@@ -79,7 +101,8 @@ def add_namespace_to_other_projection_versions(
79101
f"Updating projection '{projection_name}' from '{projection_value}' to '{new_projection_value}'"
80102
)
81103

82-
manifest["spack"]["modules"]["default"]["tcl"]["projections"][projection_name] = new_projection_value
104+
# Ensures that the new projection is a quoted string when dumped so spack does projected modules correctly, see top of file.
105+
manifest["spack"]["modules"]["default"]["tcl"]["projections"][projection_name] = YamlExplicitQuotedString(new_projection_value)
83106

84107
return manifest
85108

@@ -177,7 +200,10 @@ def main():
177200
args = parse_args(sys.argv[1:])
178201

179202
injected_manifest: str = inject_prerelease_information(
180-
args.manifest, args.version, args.keep_root_spec_intact, args.spack_packages_path
203+
args.manifest,
204+
args.version,
205+
args.keep_root_spec_intact,
206+
args.spack_packages_path,
181207
)
182208

183209
print(injected_manifest)

tests/scripts/spack_manifest/injection/outputs/expected.prerelease.spack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ spack:
4444
- oasis3-mct
4545
projections:
4646
access-om2: '{name}/pr12-12'
47-
mom5: access-om2/pr12-12/{name}/2023.11.09-{hash:7}
47+
mom5: 'access-om2/pr12-12/{name}/2023.11.09-{hash:7}'
4848
repos::
4949
- /some/spack-packages
5050
- $spack/var/spack/repos/builtin

0 commit comments

Comments
 (0)