CIScanTests.py 6.1 KB

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