check_tools_files_patterns.py 3.4 KB

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