2021-02-08 23:34:16 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2022-06-15 10:46:55 -04:00
|
|
|
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2021-02-08 23:34:16 -05:00
|
|
|
|
|
|
|
"""
|
|
|
|
Check if all rules in rules.yml used or not in CI yaml files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
2022-07-13 20:22:08 -04:00
|
|
|
import re
|
2021-02-08 23:34:16 -05:00
|
|
|
import sys
|
|
|
|
from copy import deepcopy
|
2022-07-13 20:22:08 -04:00
|
|
|
from typing import List
|
2021-02-08 23:34:16 -05:00
|
|
|
|
|
|
|
import yaml
|
|
|
|
from idf_ci_utils import IDF_PATH
|
|
|
|
|
|
|
|
ROOT_YML_FP = os.path.join(IDF_PATH, '.gitlab-ci.yml')
|
|
|
|
|
|
|
|
|
|
|
|
def load_yaml(file_path):
|
|
|
|
return yaml.load(open(file_path), Loader=yaml.FullLoader)
|
|
|
|
|
|
|
|
|
|
|
|
class YMLConfig:
|
|
|
|
def __init__(self, root_yml_file_path):
|
|
|
|
self._config = None
|
|
|
|
self._all_extends = None
|
|
|
|
|
|
|
|
self.root_yml = load_yaml(root_yml_file_path)
|
|
|
|
assert self.root_yml
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _list(str_or_list):
|
|
|
|
if isinstance(str_or_list, str):
|
|
|
|
return [str_or_list]
|
|
|
|
if isinstance(str_or_list, list):
|
|
|
|
return str_or_list
|
2022-07-13 20:22:08 -04:00
|
|
|
raise ValueError(
|
|
|
|
'Wrong type: {}. Only supports str or list.'.format(type(str_or_list))
|
|
|
|
)
|
2021-02-08 23:34:16 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
if self._config:
|
|
|
|
return self._config
|
|
|
|
|
|
|
|
all_config = dict()
|
|
|
|
for item in self.root_yml['include']:
|
2022-07-13 20:22:08 -04:00
|
|
|
all_config.update(load_yaml(os.path.join(IDF_PATH, item)))
|
2021-02-08 23:34:16 -05:00
|
|
|
self._config = all_config
|
|
|
|
return self._config
|
|
|
|
|
|
|
|
@property
|
|
|
|
def all_extends(self):
|
|
|
|
if self._all_extends:
|
|
|
|
return self._all_extends
|
|
|
|
|
|
|
|
res = set([])
|
|
|
|
for v in self.config.values():
|
|
|
|
if 'extends' in v:
|
|
|
|
for item in self._list(v['extends']):
|
|
|
|
if item.startswith('.rules:'):
|
|
|
|
res.add(item)
|
|
|
|
self._all_extends = res
|
|
|
|
return self._all_extends
|
|
|
|
|
|
|
|
def exists(self, key):
|
|
|
|
if key in self.all_extends:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-07-13 20:22:08 -04:00
|
|
|
YML_CONFIG = YMLConfig(ROOT_YML_FP)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_needed_rules(rules_yml):
|
2021-02-08 23:34:16 -05:00
|
|
|
res = 0
|
2022-07-13 20:22:08 -04:00
|
|
|
needed_rules = deepcopy(YML_CONFIG.all_extends)
|
2021-02-08 23:34:16 -05:00
|
|
|
with open(rules_yml) as fr:
|
|
|
|
for index, line in enumerate(fr):
|
|
|
|
if line.startswith('.rules:'):
|
|
|
|
key = line.strip().rsplit(':', 1)[0]
|
2022-07-13 20:22:08 -04:00
|
|
|
if not YML_CONFIG.exists(key):
|
|
|
|
print(
|
|
|
|
'{}:{}:WARNING:rule "{}" unused'.format(rules_yml, index, key)
|
|
|
|
)
|
2021-02-08 23:34:16 -05:00
|
|
|
else:
|
|
|
|
needed_rules.remove(key)
|
|
|
|
|
|
|
|
if needed_rules:
|
|
|
|
for item in needed_rules:
|
|
|
|
print('ERROR: missing rule: "{}"'.format(item))
|
|
|
|
res = 1
|
|
|
|
|
|
|
|
if res == 0:
|
|
|
|
print('Pass')
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2022-07-13 20:22:08 -04:00
|
|
|
def parse_submodule_paths(
|
|
|
|
gitsubmodules: str = os.path.join(IDF_PATH, '.gitmodules')
|
|
|
|
) -> List[str]:
|
|
|
|
path_regex = re.compile(r'^\s+path = (.+)$', re.MULTILINE)
|
|
|
|
with open(gitsubmodules, 'r') as f:
|
|
|
|
data = f.read()
|
|
|
|
|
|
|
|
res = []
|
|
|
|
for item in path_regex.finditer(data):
|
|
|
|
res.append(item.group(1))
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
def validate_submodule_patterns():
|
|
|
|
submodule_paths = sorted(['.gitmodules'] + parse_submodule_paths())
|
|
|
|
submodule_paths_in_patterns = sorted(
|
|
|
|
YML_CONFIG.config.get('.patterns-submodule', [])
|
|
|
|
)
|
|
|
|
|
|
|
|
res = 0
|
|
|
|
if submodule_paths != submodule_paths_in_patterns:
|
|
|
|
res = 1
|
|
|
|
print('please update the pattern ".patterns-submodule"')
|
|
|
|
should_remove = set(submodule_paths_in_patterns) - set(submodule_paths)
|
|
|
|
if should_remove:
|
|
|
|
print(f'- should remove: {should_remove}')
|
|
|
|
should_add = set(submodule_paths) - set(submodule_paths_in_patterns)
|
|
|
|
if should_add:
|
|
|
|
print(f'- should add: {should_add}')
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2021-02-08 23:34:16 -05:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
2022-07-13 20:22:08 -04:00
|
|
|
parser.add_argument(
|
|
|
|
'rules_yml',
|
|
|
|
nargs='?',
|
|
|
|
default=os.path.join(IDF_PATH, '.gitlab', 'ci', 'rules.yml'),
|
|
|
|
help='rules.yml file path',
|
|
|
|
)
|
2021-02-08 23:34:16 -05:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-07-13 20:22:08 -04:00
|
|
|
exit_code = 0
|
|
|
|
if validate_needed_rules(args.rules_yml):
|
|
|
|
exit_code = 1
|
|
|
|
if validate_submodule_patterns():
|
|
|
|
exit_code = 1
|
|
|
|
|
|
|
|
sys.exit(exit_code)
|