Skip to content

Commit 95300de

Browse files
committed
Make reserved definitions flow-style for module injection too
1 parent d7a9dfa commit 95300de

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

scripts/spack_manifest/injection/modules.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
Projections,
1111
)
1212

13+
# For sequence definitions, we keep it in the flow-style format (i.e., [a, b, c]) rather than block style
14+
# as it is more compact for reserved definitions.
15+
class YamlExplicitFlowStyleSequence(list[str]):
16+
pass
17+
18+
def yaml_explicit_flow_style_sequence_representer(dumper, data):
19+
"""
20+
Custom representer for YAML to ensure that some sequences are represented in flow style.
21+
This is necessary for sequences that are used as definitions in spack manifests.
22+
"""
23+
return dumper.represent_sequence("tag:yaml.org,2002:seq", data, flow_style=True)
24+
25+
yaml.add_representer(YamlExplicitFlowStyleSequence, yaml_explicit_flow_style_sequence_representer)
26+
1327
##################
1428
# Main functions #
1529
##################
@@ -36,9 +50,13 @@ def main():
3650
manifest=manifest_with_projections, root_spec=deployment_name, packages=packages
3751
)
3852

53+
finalized_manifest: dict[str, Any] = enforce_explicit_flow_style_definitions(
54+
manifest_with_projections_and_includes
55+
)
56+
3957
# Output the modified manifest
4058
dumped_manifest: str = yaml.dump(
41-
manifest_with_projections_and_includes,
59+
finalized_manifest,
4260
default_flow_style=False,
4361
sort_keys=False,
4462
)
@@ -49,6 +67,22 @@ def main():
4967
with open(args.output, "w") as output_file:
5068
output_file.write(dumped_manifest)
5169

70+
def enforce_explicit_flow_style_definitions(manifest: dict[str, Any]) -> dict[str, Any]:
71+
"""
72+
Ensure that the 'definitions' section of the manifest is represented in flow style.
73+
This is necessary for spack manifests to ensure that definitions are correctly interpreted.
74+
"""
75+
if "spack" in manifest and "definitions" in manifest["spack"]:
76+
definitions: list[dict[str, Any]] = manifest["spack"]["definitions"]
77+
for i in range(len(definitions)):
78+
definition = definitions[i]
79+
if len(definition) > 0:
80+
reserved_definition, reserved_value_list = list(definition.items())[0]
81+
82+
if reserved_definition.startswith("_"):
83+
manifest["spack"]["definitions"][i][reserved_definition] = YamlExplicitFlowStyleSequence(reserved_value_list)
84+
85+
return manifest
5286

5387
def inject_projections(
5488
manifest: str, root_spec: str, packages: set[str]

0 commit comments

Comments
 (0)