Skip to content

Commit 2bb1b13

Browse files
committed
give a try to ruamel.yaml
1 parent 9441467 commit 2bb1b13

File tree

6 files changed

+40
-36
lines changed

6 files changed

+40
-36
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Jinja2
22
jsonschema
33
pytest
4-
PyYAML
4+
ruamel.yaml >=0.15.1, <0.18.0

stackinator/builder.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
from datetime import datetime
1010

1111
import jinja2
12-
import yaml
12+
from ruamel.yaml import YAML
1313

1414
from . import VERSION, cache, root_logger, spack_util
1515

16+
yaml = YAML()
17+
1618

1719
def install(src, dst, *, ignore=None, symlinks=False):
1820
"""Call shutil.copytree or shutil.copy2. copy2 is used if `src` is not a directory.
@@ -345,7 +347,7 @@ def generate(self, recipe):
345347
if repo_yaml.exists() and repo_yaml.is_file():
346348
# open repos.yaml file and reat the list of repos
347349
with repo_yaml.open() as fid:
348-
raw = yaml.load(fid, Loader=yaml.Loader)
350+
raw = yaml.load(fid)
349351
P = raw["repos"]
350352

351353
self._logger.debug(f"the system configuration has a repo file {repo_yaml} refers to {P}")

stackinator/cache.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import os
22
import pathlib
33

4-
import yaml
4+
from ruamel.yaml import YAML
55

66
from . import schema
77

8+
yaml = YAML()
9+
810

911
def configuration_from_file(file, mount):
1012
with file.open() as fid:
1113
# load the raw yaml input
12-
raw = yaml.load(fid, Loader=yaml.Loader)
14+
raw = yaml.load(fid)
1315

1416
# validate the yaml
1517
schema.CacheValidator.validate(raw)

stackinator/recipe.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import re
44

55
import jinja2
6-
import yaml
6+
from ruamel.yaml import YAML
77

88
from . import cache, root_logger, schema, spack_util
99
from .etc import envvars
1010

11+
yaml = YAML()
12+
1113

1214
class Recipe:
1315
@property
@@ -58,7 +60,7 @@ def __init__(self, args):
5860
raise FileNotFoundError(f"The recipe path '{compiler_path}' does not contain compilers.yaml")
5961

6062
with compiler_path.open() as fid:
61-
raw = yaml.load(fid, Loader=yaml.Loader)
63+
raw = yaml.load(fid)
6264
schema.CompilersValidator.validate(raw)
6365
self.generate_compiler_specs(raw)
6466

@@ -77,7 +79,7 @@ def __init__(self, args):
7779
self.packages = None
7880
if packages_path.is_file():
7981
with packages_path.open() as fid:
80-
self.packages = yaml.load(fid, Loader=yaml.Loader)
82+
self.packages = yaml.load(fid)
8183

8284
self._logger.debug("creating packages")
8385

@@ -86,7 +88,7 @@ def __init__(self, args):
8688
recipe_packages_path = self.path / "packages.yaml"
8789
if recipe_packages_path.is_file():
8890
with recipe_packages_path.open() as fid:
89-
raw = yaml.load(fid, Loader=yaml.Loader)
91+
raw = yaml.load(fid)
9092
recipe_packages = raw["packages"]
9193

9294
# load system/packages.yaml -> system_packages (if it exists)
@@ -95,7 +97,7 @@ def __init__(self, args):
9597
if system_packages_path.is_file():
9698
# load system yaml
9799
with system_packages_path.open() as fid:
98-
raw = yaml.load(fid, Loader=yaml.Loader)
100+
raw = yaml.load(fid)
99101
system_packages = raw["packages"]
100102

101103
# extract gcc packages from system packages
@@ -115,7 +117,7 @@ def __init__(self, args):
115117
if network_path.is_file():
116118
self._logger.debug(f"opening {network_path}")
117119
with network_path.open() as fid:
118-
raw = yaml.load(fid, Loader=yaml.Loader)
120+
raw = yaml.load(fid)
119121
if "packages" in raw:
120122
network_packages = raw["packages"]
121123
if "mpi" in raw:
@@ -138,7 +140,7 @@ def __init__(self, args):
138140
raise FileNotFoundError(f"The recipe path '{environments_path}' does not contain environments.yaml")
139141

140142
with environments_path.open() as fid:
141-
raw = yaml.load(fid, Loader=yaml.Loader)
143+
raw = yaml.load(fid)
142144
# add a special environment that installs tools required later in the build process.
143145
# currently we only need squashfs for creating the squashfs file.
144146
raw["uenv_tools"] = {
@@ -256,7 +258,7 @@ def config(self, config_path):
256258
raise FileNotFoundError(f"The recipe path '{config_path}' does not contain config.yaml")
257259

258260
with config_path.open() as fid:
259-
raw = yaml.load(fid, Loader=yaml.Loader)
261+
raw = yaml.load(fid)
260262
schema.ConfigValidator.validate(raw)
261263
self._config = raw
262264

@@ -315,7 +317,7 @@ def environment_view_meta(self):
315317
@property
316318
def modules_yaml(self):
317319
with self.modules.open() as fid:
318-
raw = yaml.load(fid, Loader=yaml.Loader)
320+
raw = yaml.load(fid)
319321
raw["modules"]["default"]["roots"]["tcl"] = (pathlib.Path(self.mount) / "modules").as_posix()
320322
return yaml.dump(raw)
321323

test_stackinator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# "jinja2",
66
# "jsonschema",
77
# "pytest",
8-
# "pyYAML",
8+
# "ruamel.yaml >=0.15.1, <0.18.0",
99
# ]
1010
# ///
1111

unittests/test_schema.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
import jsonschema
77
import pytest
8-
import yaml
8+
from ruamel.yaml import YAML
99

1010
import stackinator.schema as schema
1111

12+
yaml = YAML()
13+
1214

1315
@pytest.fixture
1416
def test_path():
@@ -38,7 +40,7 @@ def recipe_paths(test_path, recipes):
3840
def test_config_yaml(yaml_path):
3941
# test that the defaults are set as expected
4042
with open(yaml_path / "config.defaults.yaml") as fid:
41-
raw = yaml.load(fid, Loader=yaml.Loader)
43+
raw = yaml.load(fid)
4244
schema.ConfigValidator.validate(raw)
4345
assert raw["store"] == "/user-environment"
4446
assert raw["spack"]["commit"] is None
@@ -57,10 +59,7 @@ def test_config_yaml(yaml_path):
5759
repo: https://github.com/spack/spack.git
5860
commit: develop-packages
5961
""")
60-
raw = yaml.load(
61-
config,
62-
Loader=yaml.Loader,
63-
)
62+
raw = yaml.load(config)
6463
schema.ConfigValidator.validate(raw)
6564
assert raw["spack"]["commit"] is None
6665
assert raw["spack"]["packages"]["commit"] is not None
@@ -78,10 +77,7 @@ def test_config_yaml(yaml_path):
7877
packages:
7978
repo: https://github.com/spack/spack.git
8079
""")
81-
raw = yaml.load(
82-
config,
83-
Loader=yaml.Loader,
84-
)
80+
raw = yaml.load(config)
8581
schema.ConfigValidator.validate(raw)
8682
assert raw["spack"]["commit"] == "develop"
8783
assert raw["spack"]["packages"]["commit"] is None
@@ -91,7 +87,7 @@ def test_config_yaml(yaml_path):
9187

9288
# full config
9389
with open(yaml_path / "config.full.yaml") as fid:
94-
raw = yaml.load(fid, Loader=yaml.Loader)
90+
raw = yaml.load(fid)
9591
schema.ConfigValidator.validate(raw)
9692
assert raw["store"] == "/alternative-point"
9793
assert raw["spack"]["commit"] == "6408b51"
@@ -107,28 +103,28 @@ def test_config_yaml(yaml_path):
107103
spack:
108104
repo: https://github.com/spack/spack.git
109105
""")
110-
raw = yaml.load(config, Loader=yaml.Loader)
106+
raw = yaml.load(config)
111107
schema.ConfigValidator.validate(raw)
112108

113109

114110
def test_recipe_config_yaml(recipe_paths):
115111
# validate the config.yaml in the test recipes
116112
for p in recipe_paths:
117113
with open(p / "config.yaml") as fid:
118-
raw = yaml.load(fid, Loader=yaml.Loader)
114+
raw = yaml.load(fid)
119115
schema.ConfigValidator.validate(raw)
120116

121117

122118
def test_compilers_yaml(yaml_path):
123119
# test that the defaults are set as expected
124120
with open(yaml_path / "compilers.defaults.yaml") as fid:
125-
raw = yaml.load(fid, Loader=yaml.Loader)
121+
raw = yaml.load(fid)
126122
schema.CompilersValidator.validate(raw)
127123
assert raw["gcc"] == {"version": "10.2"}
128124
assert raw["llvm"] is None
129125

130126
with open(yaml_path / "compilers.full.yaml") as fid:
131-
raw = yaml.load(fid, Loader=yaml.Loader)
127+
raw = yaml.load(fid)
132128
schema.CompilersValidator.validate(raw)
133129
assert raw["gcc"] == {"version": "11"}
134130
assert raw["llvm"] == {"version": "13"}
@@ -139,13 +135,13 @@ def test_recipe_compilers_yaml(recipe_paths):
139135
# validate the compilers.yaml in the test recipes
140136
for p in recipe_paths:
141137
with open(p / "compilers.yaml") as fid:
142-
raw = yaml.load(fid, Loader=yaml.Loader)
138+
raw = yaml.load(fid)
143139
schema.CompilersValidator.validate(raw)
144140

145141

146142
def test_environments_yaml(yaml_path):
147143
with open(yaml_path / "environments.full.yaml") as fid:
148-
raw = yaml.load(fid, Loader=yaml.Loader)
144+
raw = yaml.load(fid)
149145
schema.EnvironmentsValidator.validate(raw)
150146

151147
# the defaults-env does not set fields
@@ -186,7 +182,7 @@ def test_environments_yaml(yaml_path):
186182
# check that only allowed fields are accepted
187183
# from an example that was silently validated
188184
with open(yaml_path / "environments.err-providers.yaml") as fid:
189-
raw = yaml.load(fid, Loader=yaml.Loader)
185+
raw = yaml.load(fid)
190186
with pytest.raises(
191187
jsonschema.exceptions.ValidationError,
192188
match=r"Additional properties are not allowed \('providers' was unexpected",
@@ -198,7 +194,7 @@ def test_recipe_environments_yaml(recipe_paths):
198194
# validate the environments.yaml in the test recipes
199195
for p in recipe_paths:
200196
with open(p / "environments.yaml") as fid:
201-
raw = yaml.load(fid, Loader=yaml.Loader)
197+
raw = yaml.load(fid)
202198
schema.EnvironmentsValidator.validate(raw)
203199

204200

@@ -210,5 +206,7 @@ def test_unique_properties():
210206
"""
211207
)
212208

213-
with pytest.raises(Exception):
214-
yaml.load(invalid_config, Loader=yaml.Loader)
209+
from ruamel.yaml.constructor import DuplicateKeyError
210+
211+
with pytest.raises(DuplicateKeyError):
212+
yaml.load(invalid_config)

0 commit comments

Comments
 (0)