CIScanTests.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import argparse
  2. import errno
  3. import json
  4. import logging
  5. import os
  6. from collections import defaultdict
  7. from copy import deepcopy
  8. from find_apps import find_apps
  9. from find_build_apps import BUILD_SYSTEM_CMAKE, BUILD_SYSTEMS
  10. from idf_py_actions.constants import PREVIEW_TARGETS, SUPPORTED_TARGETS
  11. from ttfw_idf.IDFAssignTest import ExampleAssignTest, TestAppsAssignTest
  12. TEST_LABELS = {
  13. 'example_test': 'BOT_LABEL_EXAMPLE_TEST',
  14. 'test_apps': 'BOT_LABEL_CUSTOM_TEST',
  15. 'component_ut': ['BOT_LABEL_UNIT_TEST',
  16. 'BOT_LABEL_UNIT_TEST_32',
  17. 'BOT_LABEL_UNIT_TEST_S2',
  18. 'BOT_LABEL_UNIT_TEST_C3'],
  19. }
  20. BUILD_ALL_LABELS = [
  21. 'BOT_LABEL_BUILD',
  22. 'BOT_LABEL_BUILD_ALL_APPS',
  23. 'BOT_LABEL_REGULAR_TEST',
  24. 'BOT_LABEL_WEEKEND_TEST',
  25. ]
  26. def _has_build_all_label():
  27. for label in BUILD_ALL_LABELS:
  28. if os.getenv(label):
  29. return True
  30. return False
  31. def _judge_build_or_not(action, build_all): # type: (str, bool) -> (bool, bool)
  32. """
  33. :return: (build_or_not_for_test_related_apps, build_or_not_for_non_related_apps)
  34. """
  35. if build_all or _has_build_all_label() or (not os.getenv('BOT_TRIGGER_WITH_LABEL')):
  36. logging.info('Build all apps')
  37. return True, True
  38. labels = TEST_LABELS[action]
  39. if not isinstance(labels, list):
  40. labels = [labels]
  41. for label in labels:
  42. if os.getenv(label):
  43. logging.info('Build only test cases apps')
  44. return True, False
  45. logging.info('Skip all')
  46. return False, False
  47. def output_json(apps_dict_list, target, build_system, output_dir):
  48. output_path = os.path.join(output_dir, 'scan_{}_{}.json'.format(target.lower(), build_system))
  49. with open(output_path, 'w') as fw:
  50. fw.writelines([json.dumps(app) + '\n' for app in apps_dict_list])
  51. def main():
  52. parser = argparse.ArgumentParser(description='Scan the required build tests')
  53. parser.add_argument('test_type',
  54. choices=TEST_LABELS.keys(),
  55. help='Scan test type')
  56. parser.add_argument('paths', nargs='+',
  57. help='One or more app paths')
  58. parser.add_argument('-b', '--build-system',
  59. choices=BUILD_SYSTEMS.keys(),
  60. default=BUILD_SYSTEM_CMAKE)
  61. parser.add_argument('-c', '--ci-config-file',
  62. required=True,
  63. help='gitlab ci config target-test file')
  64. parser.add_argument('-o', '--output-path',
  65. required=True,
  66. help='output path of the scan result')
  67. parser.add_argument('--exclude', nargs='*',
  68. help='Ignore specified directory. Can be used multiple times.')
  69. parser.add_argument('--preserve', action='store_true',
  70. help='add this flag to preserve artifacts for all apps')
  71. parser.add_argument('--build-all', action='store_true',
  72. help='add this flag to build all apps')
  73. args = parser.parse_args()
  74. build_test_case_apps, build_standalone_apps = _judge_build_or_not(args.test_type, args.build_all)
  75. if not os.path.exists(args.output_path):
  76. try:
  77. os.makedirs(args.output_path)
  78. except OSError as e:
  79. if e.errno != errno.EEXIST:
  80. raise e
  81. SUPPORTED_TARGETS.extend(PREVIEW_TARGETS)
  82. if (not build_standalone_apps) and (not build_test_case_apps):
  83. for target in SUPPORTED_TARGETS:
  84. output_json([], target, args.build_system, args.output_path)
  85. SystemExit(0)
  86. paths = set([os.path.join(os.getenv('IDF_PATH'), path) if not os.path.isabs(path) else path for path in args.paths])
  87. test_cases = []
  88. for path in paths:
  89. if args.test_type == 'example_test':
  90. assign = ExampleAssignTest(path, args.ci_config_file)
  91. elif args.test_type in ['test_apps', 'component_ut']:
  92. assign = TestAppsAssignTest(path, args.ci_config_file)
  93. else:
  94. raise SystemExit(1) # which is impossible
  95. test_cases.extend(assign.search_cases())
  96. '''
  97. {
  98. <target>: {
  99. 'test_case_apps': [<app_dir>], # which is used in target tests
  100. 'standalone_apps': [<app_dir>], # which is not
  101. },
  102. ...
  103. }
  104. '''
  105. scan_info_dict = defaultdict(dict)
  106. # store the test cases dir, exclude these folders when scan for standalone apps
  107. default_exclude = args.exclude if args.exclude else []
  108. build_system = args.build_system.lower()
  109. build_system_class = BUILD_SYSTEMS[build_system]
  110. for target in SUPPORTED_TARGETS:
  111. exclude_apps = deepcopy(default_exclude)
  112. if build_test_case_apps:
  113. scan_info_dict[target]['test_case_apps'] = set()
  114. for case in test_cases:
  115. app_dir = case.case_info['app_dir']
  116. app_target = case.case_info['target']
  117. if app_target.lower() != target.lower():
  118. continue
  119. _apps = find_apps(build_system_class, app_dir, True, exclude_apps, target.lower())
  120. if _apps:
  121. scan_info_dict[target]['test_case_apps'].update(_apps)
  122. exclude_apps.append(app_dir)
  123. else:
  124. scan_info_dict[target]['test_case_apps'] = set()
  125. if build_standalone_apps:
  126. scan_info_dict[target]['standalone_apps'] = set()
  127. for path in paths:
  128. scan_info_dict[target]['standalone_apps'].update(
  129. find_apps(build_system_class, path, True, exclude_apps, target.lower()))
  130. else:
  131. scan_info_dict[target]['standalone_apps'] = set()
  132. test_case_apps_preserve_default = True if build_system == 'cmake' else False
  133. for target in SUPPORTED_TARGETS:
  134. apps = []
  135. for app_dir in scan_info_dict[target]['test_case_apps']:
  136. apps.append({
  137. 'app_dir': app_dir,
  138. 'build_system': args.build_system,
  139. 'target': target,
  140. 'preserve': args.preserve or test_case_apps_preserve_default
  141. })
  142. for app_dir in scan_info_dict[target]['standalone_apps']:
  143. apps.append({
  144. 'app_dir': app_dir,
  145. 'build_system': args.build_system,
  146. 'target': target,
  147. 'preserve': args.preserve
  148. })
  149. output_path = os.path.join(args.output_path, 'scan_{}_{}.json'.format(target.lower(), build_system))
  150. with open(output_path, 'w') as fw:
  151. fw.writelines([json.dumps(app) + '\n' for app in apps])
  152. if __name__ == '__main__':
  153. main()