ci: check .patterns-submodule in pre-commit

pull/9408/head
Fu Hanxi 2022-07-14 08:22:08 +08:00
rodzic 62ca8e2fb4
commit 6d8ebacb8a
2 zmienionych plików z 62 dodań i 13 usunięć

Wyświetl plik

@ -164,7 +164,6 @@
- "tools/split_paths_by_spaces.py" - "tools/split_paths_by_spaces.py"
.patterns-windows: &patterns-windows .patterns-windows: &patterns-windows
- "tools/windows/**/*" - "tools/windows/**/*"
@ -172,7 +171,6 @@
- "tools/docker/**/*" - "tools/docker/**/*"
.patterns-submodule: &patterns-submodule .patterns-submodule: &patterns-submodule
- "components/asio/asio"
- "components/bootloader/subproject/components/micro-ecc/micro-ecc" - "components/bootloader/subproject/components/micro-ecc/micro-ecc"
- "components/bt/controller/lib_esp32" - "components/bt/controller/lib_esp32"
- "components/bt/controller/lib_esp32c2/esp32c2-bt-lib" - "components/bt/controller/lib_esp32c2/esp32c2-bt-lib"

Wyświetl plik

@ -9,8 +9,10 @@ Check if all rules in rules.yml used or not in CI yaml files.
import argparse import argparse
import os import os
import re
import sys import sys
from copy import deepcopy from copy import deepcopy
from typing import List
import yaml import yaml
from idf_ci_utils import IDF_PATH from idf_ci_utils import IDF_PATH
@ -36,7 +38,9 @@ class YMLConfig:
return [str_or_list] return [str_or_list]
if isinstance(str_or_list, list): if isinstance(str_or_list, list):
return str_or_list return str_or_list
raise ValueError('Wrong type: {}. Only supports str or list.'.format(type(str_or_list))) raise ValueError(
'Wrong type: {}. Only supports str or list.'.format(type(str_or_list))
)
@property @property
def config(self): def config(self):
@ -45,8 +49,7 @@ class YMLConfig:
all_config = dict() all_config = dict()
for item in self.root_yml['include']: for item in self.root_yml['include']:
if not item.endswith('rules.yml'): all_config.update(load_yaml(os.path.join(IDF_PATH, item)))
all_config.update(load_yaml(os.path.join(IDF_PATH, item)))
self._config = all_config self._config = all_config
return self._config return self._config
@ -70,16 +73,20 @@ class YMLConfig:
return False return False
def validate(rules_yml): YML_CONFIG = YMLConfig(ROOT_YML_FP)
yml_config = YMLConfig(ROOT_YML_FP)
def validate_needed_rules(rules_yml):
res = 0 res = 0
needed_rules = deepcopy(yml_config.all_extends) needed_rules = deepcopy(YML_CONFIG.all_extends)
with open(rules_yml) as fr: with open(rules_yml) as fr:
for index, line in enumerate(fr): for index, line in enumerate(fr):
if line.startswith('.rules:'): if line.startswith('.rules:'):
key = line.strip().rsplit(':', 1)[0] key = line.strip().rsplit(':', 1)[0]
if not yml_config.exists(key): if not YML_CONFIG.exists(key):
print('{}:{}:WARNING:rule "{}" unused'.format(rules_yml, index, key)) print(
'{}:{}:WARNING:rule "{}" unused'.format(rules_yml, index, key)
)
else: else:
needed_rules.remove(key) needed_rules.remove(key)
@ -93,10 +100,54 @@ def validate(rules_yml):
return res return res
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
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('rules_yml', nargs='?', default=os.path.join(IDF_PATH, '.gitlab', 'ci', 'rules.yml'), parser.add_argument(
help='rules.yml file path') 'rules_yml',
nargs='?',
default=os.path.join(IDF_PATH, '.gitlab', 'ci', 'rules.yml'),
help='rules.yml file path',
)
args = parser.parse_args() args = parser.parse_args()
sys.exit(validate(args.rules_yml)) exit_code = 0
if validate_needed_rules(args.rules_yml):
exit_code = 1
if validate_submodule_patterns():
exit_code = 1
sys.exit(exit_code)