summaryrefslogtreecommitdiffstats
path: root/src/common/options/validate-options.py
blob: 5bc5d4d4673632296ba714d89533b6b3c991d79c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3

import argparse
import fileinput
import sys
import yaml
from typing import Any, Dict


class ValidationError(Exception):
    pass


OptionType = Dict[str, Any]


def validate_see_also(opt: OptionType, opts: Dict[str, OptionType]) -> None:
    see_also = opt.get('see_also')
    if see_also is None:
        return
    for ref in see_also:
        if ref not in opts:
            msg = f'see_also contains "{ref}". But it is not found.'
            raise ValidationError(msg)


def main() -> None:
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('yamls', nargs='*')
    opts = parser.parse_args()
    options = {}
    for yaml_file in opts.yamls:
        with open(yaml_file) as f:
            yml = yaml.load(f, yaml.SafeLoader)
            options.update({opt['name']: opt for opt in yml['options']})
    for name, opt in options.items():
        try:
            validate_see_also(opt, options)
        except ValidationError as e:
            raise Exception(f'failed to validate "{name}": {e}')


if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        print(e, file=sys.stderr)
        sys.exit(1)