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