check_tools_files_patterns.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. import argparse
  6. import os
  7. import sys
  8. from pathlib import Path
  9. from typing import Set, Tuple
  10. import yaml
  11. from idf_ci_utils import IDF_PATH, get_git_files
  12. def check(pattern_yml: str, exclude_list: str) -> Tuple[Set, Set]:
  13. rules_dict = yaml.load(open(pattern_yml), Loader=yaml.FullLoader)
  14. rules_patterns_set = set()
  15. for k, v in rules_dict.items():
  16. if k.startswith('.pattern') and k != '.patterns-python-files' and isinstance(v, list):
  17. rules_patterns_set.update(v)
  18. rules_files_set: Set = set()
  19. idf_path = Path(IDF_PATH)
  20. for pat in rules_patterns_set:
  21. rules_files_set.update(idf_path.glob(pat))
  22. exclude_patterns_set = set()
  23. for line in open(exclude_list).readlines():
  24. pat = line.split('#')[0].strip()
  25. if pat:
  26. exclude_patterns_set.add(pat)
  27. exclude_files_set: Set = set()
  28. for pat in exclude_patterns_set:
  29. exclude_files_set.update(idf_path.glob(pat))
  30. missing_files = set()
  31. git_files = get_git_files(os.path.join(IDF_PATH, 'tools'), full_path=True)
  32. for f in git_files:
  33. f = Path(f)
  34. if f in rules_files_set or f in exclude_files_set:
  35. continue
  36. missing_files.add(os.path.relpath(f, IDF_PATH))
  37. return missing_files, rules_patterns_set.intersection(exclude_patterns_set)
  38. if __name__ == '__main__':
  39. parser = argparse.ArgumentParser(description='check if all tools files are in rules patterns or exclude list')
  40. parser.add_argument('-c', '--pattern-yml',
  41. default=os.path.join(IDF_PATH, '.gitlab', 'ci', 'rules.yml'),
  42. help='yml file path included file patterns')
  43. parser.add_argument('-e', '--exclude-list',
  44. default=os.path.join(IDF_PATH, 'tools', 'ci', 'exclude_check_tools_files.txt'),
  45. help='exclude list path')
  46. args = parser.parse_args()
  47. res = 0
  48. not_included_files, dup_patterns = check(args.pattern_yml, args.exclude_list)
  49. if not_included_files or dup_patterns:
  50. res = 1
  51. print('This test is used for making sure of all the tools dir files are recorded in .gitlab/ci/rules.yml to '
  52. 'trigger the related tests, except those files should be excluded.')
  53. if not_included_files:
  54. print('Missing Files:')
  55. for file in not_included_files:
  56. print('\t' + str(file))
  57. print('Please add these files or glob patterns to ".gitlab/ci/rules.yml" and put related files under '
  58. '".patterns-<test_group>" block to trigger related tests.\n'
  59. 'Or add them to "tools/ci/exclude_check_tools_files.txt" to exclude them.')
  60. if dup_patterns:
  61. print('Duplicated Patterns:')
  62. for pattern in dup_patterns:
  63. print('\t' + pattern)
  64. print('Please remove them from tools/ci/exclude_check_tools_files.txt')
  65. sys.exit(res)