|
11 | 11 | Projections, |
12 | 12 | Specs |
13 | 13 | ) |
| 14 | +from scripts.spack_manifest.injection.yaml_representer import ( |
| 15 | + YamlExplicitFlowStyleSequence, |
| 16 | + YamlExplicitQuotedString, |
| 17 | + yaml_explicit_flow_style_sequence_representer, |
| 18 | + yaml_explicit_quoted_string_representer |
| 19 | +) |
14 | 20 |
|
15 | 21 |
|
16 | | -# PyYaml by default dumps unquoted strings if they look unambiguous, and quoted strings otherwise. |
17 | | -# PyYaml dumps '{name}/prX-Y' as a quoted str as it has '{' at the front and causes ambiguity |
18 | | -# But 'ROOT_SPEC/.dependencies/prX-Y/VERSION-{hash:7}' is dumped as an unquoted str as it is unambiguous |
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 | | -def yaml_explicit_quoted_string_representer(dumper, data): |
24 | | - """ |
25 | | - Custom representer for YAML to ensure that some strings are quoted explicitly. |
26 | | - This is necessary for strings that are used as projections in spack manifests. |
27 | | - """ |
28 | | - return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="'") |
29 | | - |
30 | | -# For sequence definitions, we keep it in the flow-style format (i.e., [a, b, c]) rather than block style |
31 | | -# as it is more compact for reserved definitions. |
32 | | -class YamlExplicitFlowStyleSequence(list[str]): |
33 | | - pass |
34 | | - |
35 | | -def yaml_explicit_flow_style_sequence_representer(dumper, data): |
36 | | - """ |
37 | | - Custom representer for YAML to ensure that some sequences are represented in flow style. |
38 | | - This is necessary for sequences that are used as definitions in spack manifests. |
39 | | - """ |
40 | | - return dumper.represent_sequence("tag:yaml.org,2002:seq", data, flow_style=True) |
41 | | - |
42 | | - |
| 22 | +# The yaml representer sometimes dumps ambiguous strings in the case of projections like `{name}/...` as unquoted strings, |
| 23 | +# which is not handled by spack very well. |
43 | 24 | yaml.add_representer(YamlExplicitQuotedString, yaml_explicit_quoted_string_representer) |
| 25 | + |
| 26 | +# We represent reserved definitions as in flow-style sequences (eg. `[a]` rather than `- a`), so it is more compact. |
44 | 27 | yaml.add_representer(YamlExplicitFlowStyleSequence, yaml_explicit_flow_style_sequence_representer) |
45 | 28 |
|
46 | 29 | ### Actual methods begin here ### |
47 | 30 |
|
48 | | - |
49 | 31 | def inject_prerelease_information( |
50 | 32 | manifest_path: str, |
51 | 33 | version: str, |
|
0 commit comments