forked from circleops/circle-ecosystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linter.py
89 lines (71 loc) · 2.34 KB
/
linter.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
# Copyright (c) 2022 Circle Internet Financial Trading Company Limited.
# All rights reserved.
#
# Circle Internet Financial Trading Company Limited CONFIDENTIAL
# This file includes unpublished proprietary source code of Circle Internet
# Financial Trading Company Limited, Inc. The copyright notice above does not
# evidence any actual or intended publication of such source code. Disclosure
# of this source code or any related proprietary information is strictly
# prohibited without the express written permission of Circle Internet
# Financial Trading Company Limited.
import logging
import sys
from glob import glob
import yaml
from jsonschema import validate
from jsonschema.exceptions import ValidationError
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
)
LOGGER = logging.getLogger(__name__)
UUID_SET = set()
def lint(file_path: str) -> dict:
"""
Lint YAML file.
:param file_path: directory path of the file
:return: dictionary of YAML contents
"""
with open('ecosystem_schema.yml', 'r') as stream:
try:
schema = yaml.safe_load(stream)
except yaml.YAMLError as e:
LOGGER.error('Fail to load schema.', e)
with open(file_path, 'r') as stream:
try:
yaml_dict = yaml.safe_load(stream)
except yaml.YAMLError as e:
LOGGER.error(f'Fail to load file {yaml_dict}', e)
validate(yaml_dict, schema)
return yaml_dict
def check_uuid(yaml_dict: dict) -> None:
"""
Verifies UUID is unique.
:param yaml_dict: dictionary of YAML contents
:return: None
"""
yaml_id = yaml_dict['id']
if yaml_id in UUID_SET:
raise ValueError(f'Duplicate UUID {yaml_id}.')
UUID_SET.add(yaml_id)
def main():
files = glob('catalog/**/*.yml', recursive=True) + glob('catalog/**/*.yaml', recursive=True)
failed = False
pass_count = 0
for file_path in files:
try:
yaml_dict = lint(file_path)
check_uuid(yaml_dict)
pass_count += 1
except (ValidationError, ValueError) as e:
LOGGER.error(e)
failed = True
LOGGER.info(f'PASS: {pass_count}/{len(files)}')
if failed:
LOGGER.info('FAIL')
sys.exit(1)
else:
LOGGER.info('SUCCESS')
sys.exit()
if __name__ == '__main__':
main()