check_tools_files_patterns.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2021 Espressif Systems (Shanghai) CO LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import argparse
  17. import fnmatch
  18. import glob
  19. import os
  20. import sys
  21. import yaml
  22. from idf_ci_utils import IDF_PATH, get_git_files, magic_check, magic_check_bytes, translate
  23. # Monkey patch starts
  24. # glob.glob will ignore all files starts with ``.``
  25. # don't ignore them here
  26. # need to keep the same argument as glob._ishidden
  27. def _ishidden(path): # pylint: disable=W0613
  28. return False
  29. fnmatch.translate = translate
  30. glob.magic_check = magic_check
  31. glob.magic_check_bytes = magic_check_bytes
  32. glob._ishidden = _ishidden # pylint: disable=W0212
  33. # ends here
  34. def check(pattern_yml, exclude_list):
  35. rules_dict = yaml.load(open(pattern_yml), Loader=yaml.FullLoader)
  36. rules_patterns_set = set()
  37. for k, v in rules_dict.items():
  38. if k.startswith('.pattern') and isinstance(v, list):
  39. rules_patterns_set.update(v)
  40. rules_files_set = set()
  41. for pat in rules_patterns_set:
  42. rules_files_set.update(glob.glob(os.path.join(IDF_PATH, pat), recursive=True))
  43. exclude_patterns_set = set()
  44. exclude_patterns_set.update([path.split('#')[0].strip() for path in open(exclude_list).readlines() if path])
  45. exclude_files_set = set()
  46. for pat in exclude_patterns_set:
  47. exclude_files_set.update(glob.glob(os.path.join(IDF_PATH, pat), recursive=True))
  48. missing_files = set()
  49. git_files = get_git_files(os.path.join(IDF_PATH, 'tools'), full_path=True)
  50. for f in git_files:
  51. if f in rules_files_set or f in exclude_files_set:
  52. continue
  53. missing_files.add(os.path.relpath(f, IDF_PATH))
  54. return missing_files, rules_patterns_set.intersection(exclude_patterns_set)
  55. if __name__ == '__main__':
  56. parser = argparse.ArgumentParser(description='check if all tools files are in rules patterns or exclude list')
  57. parser.add_argument('-c', '--pattern-yml',
  58. default=os.path.join(IDF_PATH, '.gitlab', 'ci', 'rules.yml'),
  59. help='yml file path included file patterns')
  60. parser.add_argument('-e', '--exclude-list',
  61. default=os.path.join(IDF_PATH, 'tools', 'ci', 'exclude_check_tools_files.txt'),
  62. help='exclude list path')
  63. args = parser.parse_args()
  64. res = 0
  65. not_included_files, dup_patterns = check(args.pattern_yml, args.exclude_list)
  66. if not_included_files:
  67. print('Missing Files: (please add to tools/ci/exclude_check_tools_files.txt')
  68. for file in not_included_files:
  69. print(file)
  70. res = 1
  71. if dup_patterns:
  72. print('Duplicated Patterns: (please check .gitlab/ci/rules.yml and tools/ci/exclude_check_tools_files.txt')
  73. for pattern in dup_patterns:
  74. print(pattern)
  75. res = 1
  76. sys.exit(res)