Skip to content

Commit bcebf81

Browse files
committed
reverse generated-python direction
Make binderspawner_mixin.py canonical, instead of python-in-yaml easier to edit with editors, linters, etc. removes use of redundant pyyaml
1 parent 98940b2 commit bcebf81

File tree

7 files changed

+46
-34
lines changed

7 files changed

+46
-34
lines changed

binderhub/binderspawner_mixin.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
Helpers for creating BinderSpawners
33
44
FIXME:
5-
This file is defined in helm-chart/binderhub/values.yaml and is copied to
6-
binderhub/binderspawner_mixin.py by setup.py so that it can be used by other JupyterHub
7-
container spawners.
5+
This file is defined in binderhub/binderspawner_mixin.py
6+
and is copied to helm-chart/binderhub/values.yaml
7+
by ci/check_embedded_chart_code.py
88
9-
The BinderHub repo is just used as the distribution mechanism for this spawner, BinderHub
10-
itself doesn't require this code.
9+
The BinderHub repo is just used as the distribution mechanism for this spawner,
10+
BinderHub itself doesn't require this code.
1111
1212
Longer term options include:
1313
- Move BinderSpawnerMixin to a separate Python package and include it in the Z2JH Hub

ci/check_embedded_chart_code.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import difflib
1212
import os
1313
import sys
14-
import yaml
14+
from ruamel.yaml import YAML
15+
16+
yaml = YAML(typ="rt")
17+
yaml.preserve_quotes = True
18+
yaml.indent(mapping=2, sequence=4, offset=2)
1519

1620
parser = argparse.ArgumentParser(description='Check embedded chart code')
1721
parser.add_argument('--update', action='store_true', help='Update binderhub code from values.yaml')
@@ -21,21 +25,28 @@
2125
binderspawner_mixin_py = os.path.join(root, 'binderhub', 'binderspawner_mixin.py')
2226
values_yaml = os.path.join(root, 'helm-chart', 'binderhub', 'values.yaml')
2327

24-
with open(values_yaml) as f:
25-
values = yaml.safe_load(f)
26-
values_code = values['jupyterhub']['hub']['extraConfig']['0-binderspawnermixin'].splitlines()
28+
with open(binderspawner_mixin_py, 'r') as f:
29+
py_code = f.read()
30+
2731

2832
if args.update:
29-
with open(binderspawner_mixin_py, 'w') as f:
30-
f.write(values['jupyterhub']['hub']['extraConfig']['0-binderspawnermixin'])
33+
with open(values_yaml) as f:
34+
values = yaml.load(f)
35+
values_code = values['jupyterhub']['hub']['extraConfig']['0-binderspawnermixin']
36+
if values_code != py_code:
37+
print(f"Generating {values_yaml} from {binderspawner_mixin_py}")
38+
values['jupyterhub']['hub']['extraConfig']['0-binderspawnermixin'] = py_code
39+
with open(values_yaml, "w") as f:
40+
yaml.dump(values, f)
3141
else:
32-
with open(binderspawner_mixin_py, 'r') as f:
33-
py_code = f.read().splitlines()
42+
with open(values_yaml) as f:
43+
values = yaml.load(f)
44+
values_code = values['jupyterhub']['hub']['extraConfig']['0-binderspawnermixin']
3445

35-
difflines = list(difflib.context_diff(values_code, py_code))
46+
difflines = list(difflib.context_diff(values_code.splitlines(), py_code.splitlines()))
3647
if difflines:
3748
print('\n'.join(difflines))
3849
print('\n')
3950
print('Values code is not in sync with binderhub/binderspawner_mixin.py')
40-
print('Run `python {} --update` to update binderhub/binderspawner_mixin.py from values.yaml'.format(sys.argv[0]))
51+
print('Run `python {} --update` to update values.yaml from binderhub/binderspawner_mixin.py'.format(sys.argv[0]))
4152
sys.exit(1)

dev-requirements.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ pycurl
1010
pytest
1111
pytest-asyncio
1212
pytest-cov
13-
# pyyaml is used by the scripts in tools/ and could absolutely be replaced by
14-
# using ruamel.yaml. ruamel.yaml is more powerful than pyyaml, and that is
15-
# relevant when writing YAML files back after having loaded them from disk.
16-
pyyaml
1713
jupyter-repo2docker>=2021.08.0
1814
requests
1915
ruamel.yaml>=0.15

helm-chart/binderhub/files/binderhub_config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import os
33
from functools import lru_cache
44
from urllib.parse import urlparse
5-
import yaml
5+
from ruamel.yaml import YAML
6+
7+
yaml = YAML(typ="safe")
68

79

810
def _merge_dictionaries(a, b):
@@ -34,7 +36,7 @@ def _load_values():
3436
if os.path.exists(path):
3537
print(f"Loading {path}")
3638
with open(path) as f:
37-
values = yaml.safe_load(f)
39+
values = yaml.load(f)
3840
cfg = _merge_dictionaries(cfg, values)
3941
else:
4042
print(f"No config at {path}")

helm-chart/binderhub/values.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ jupyterhub:
6868
Helpers for creating BinderSpawners
6969
7070
FIXME:
71-
This file is defined in helm-chart/binderhub/values.yaml and is copied to
72-
binderhub/binderspawner_mixin.py by setup.py so that it can be used by other JupyterHub
73-
container spawners.
71+
This file is defined in binderhub/binderspawner_mixin.py
72+
and is copied to helm-chart/binderhub/values.yaml
73+
by ci/check_embedded_chart_code.py
7474
75-
The BinderHub repo is just used as the distribution mechanism for this spawner, BinderHub
76-
itself doesn't require this code.
75+
The BinderHub repo is just used as the distribution mechanism for this spawner,
76+
BinderHub itself doesn't require this code.
7777
7878
Longer term options include:
7979
- Move BinderSpawnerMixin to a separate Python package and include it in the Z2JH Hub

tools/generate-json-schema.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
from collections.abc import MutableMapping
1616

17-
import yaml
17+
from ruamel.yaml import YAML
18+
yaml = YAML(typ="safe")
1819

1920
here_dir = os.path.abspath(os.path.dirname(__file__))
2021
schema_yaml = os.path.join(
@@ -50,7 +51,7 @@ def run():
5051
# Using these sets, we can validate further manually by printing the results
5152
# of set operations.
5253
with open(schema_yaml) as f:
53-
schema = yaml.safe_load(f)
54+
schema = yaml.load(f)
5455

5556
# Drop what isn't relevant for a values.schema.json file packaged with the
5657
# Helm chart, such as the description keys only relevant for our

tools/validate-against-schema.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python3
2-
import jsonschema
32
import os
4-
import yaml
3+
4+
import jsonschema
5+
from ruamel.yaml import YAML
6+
yaml = YAML(typ="safe")
57

68
here_dir = os.path.abspath(os.path.dirname(__file__))
79
schema_yaml = os.path.join(
@@ -18,13 +20,13 @@
1820
)
1921

2022
with open(schema_yaml) as f:
21-
schema = yaml.safe_load(f)
23+
schema = yaml.load(f)
2224
with open(values_yaml) as f:
23-
values = yaml.safe_load(f)
25+
values = yaml.load(f)
2426
with open(lint_and_validate_values_yaml) as f:
25-
lint_and_validate_values = yaml.safe_load(f)
27+
lint_and_validate_values = yaml.load(f)
2628
with open(binderhub_chart_config_yaml) as f:
27-
binderhub_chart_config_yaml = yaml.safe_load(f)
29+
binderhub_chart_config_yaml = yaml.load(f)
2830

2931
# Validate values.yaml against schema
3032
print("Validating values.yaml against schema.yaml...")

0 commit comments

Comments
 (0)