-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathchains_validator.py
91 lines (62 loc) · 2.41 KB
/
chains_validator.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from functools import reduce
from typing import Callable, List
import yaml
import json
from urllib.parse import unquote, urlparse
from pathlib import PurePosixPath
import os.path
import os
class Types:
def __init__(self, types_config):
local_path = Types.__remote_url_to_local(types_config["url"])
with open(local_path) as fin:
self.types_dict = json.load(fin)
self.is_v14 = Types.__is_v14(self.types_dict)
self.overrides_common = types_config.get("overridesCommon", False)
@staticmethod
def __is_v14(types_dict):
# ParaId types is only has to be defined for v14 types
return "ParaId" in types_dict["types"].keys()
@staticmethod
def __remote_url_to_local(url: str):
path_parts = PurePosixPath(unquote(urlparse(url).path)).parts
after_chains_index = path_parts.index("chains")
# dirname = os.path.dirname(__file__)
relative_path_parts = path_parts[(after_chains_index + 1):]
local_path = os.path.join("", *relative_path_parts)
return local_path
@staticmethod
def parse_from(chain_config):
types_config = chain_config.get("types")
return None if types_config is None else Types(types_config)
class Chain:
def __init__(self, chain_config):
self.config = chain_config
self.types = Types.parse_from(chain_config)
self.name = chain_config["name"]
def v14_types_should_override_common(chain: Chain) -> List[str]:
if chain.types is not None and chain.types.is_v14 and not chain.types.overrides_common:
return ["v14 types should have overrideCommon set to True"]
else:
return []
ALL_RULES: List[Callable[[Chain], List[str]]] = [
v14_types_should_override_common
]
with open("chains_validator.yaml") as fin:
config = yaml.safe_load(fin)
found_problems = False
for chain_file in config["chain_files"]:
with open(chain_file) as fin:
all_chains_config = json.load(fin)
for chain_config in all_chains_config:
try:
chain = Chain(chain_config)
chain_problems = reduce(lambda acc, rule: acc + rule(chain), ALL_RULES, [])
except Exception as e:
chain_problems = [str(e)]
for problem in chain_problems:
print(f"{chain_file}->{chain_config['name']}: {problem}")
if len(chain_problems) > 0:
found_problems = True
if found_problems:
exit(1)