|
11 | 11 | import difflib |
12 | 12 | import os |
13 | 13 | 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) |
15 | 19 |
|
16 | 20 | parser = argparse.ArgumentParser(description='Check embedded chart code') |
17 | 21 | parser.add_argument('--update', action='store_true', help='Update binderhub code from values.yaml') |
|
21 | 25 | binderspawner_mixin_py = os.path.join(root, 'binderhub', 'binderspawner_mixin.py') |
22 | 26 | values_yaml = os.path.join(root, 'helm-chart', 'binderhub', 'values.yaml') |
23 | 27 |
|
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 | + |
27 | 31 |
|
28 | 32 | 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) |
31 | 41 | 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'] |
34 | 45 |
|
35 | | - difflines = list(difflib.context_diff(values_code, py_code)) |
| 46 | + difflines = list(difflib.context_diff(values_code.splitlines(), py_code.splitlines())) |
36 | 47 | if difflines: |
37 | 48 | print('\n'.join(difflines)) |
38 | 49 | print('\n') |
39 | 50 | 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])) |
41 | 52 | sys.exit(1) |
0 commit comments