From 153433d47d685f60539425e81cf87e4c3f28288b Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Fri, 28 Jan 2022 15:21:12 +0800 Subject: [PATCH] ci: build_pytest_app will now remove the non-test apps simplify the cli as well --- .gitlab/ci/build.yml | 23 +++---- tools/ci/build_pytest_apps.py | 118 +++++++++++++++++----------------- tools/ci/idf_ci_utils.py | 114 ++++++++++++++++++++++++-------- 3 files changed, 154 insertions(+), 101 deletions(-) diff --git a/.gitlab/ci/build.yml b/.gitlab/ci/build.yml index 8e814b5755..6bd7d56360 100644 --- a/.gitlab/ci/build.yml +++ b/.gitlab/ci/build.yml @@ -42,63 +42,56 @@ build_pytest_examples_esp32: - .build_pytest_template - .rules:build:example_test-esp32 script: - - python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir examples --target esp32 --size-info $SIZE_INFO_LOCATION -vv + - run_cmd python tools/ci/build_pytest_apps.py examples --target esp32 --size-info $SIZE_INFO_LOCATION -vv build_pytest_examples_esp32s2: extends: - .build_pytest_template - .rules:build:example_test-esp32s2 script: - - python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir examples --target esp32s2 --size-info $SIZE_INFO_LOCATION -vv + - run_cmd python tools/ci/build_pytest_apps.py examples --target esp32s2 --size-info $SIZE_INFO_LOCATION -vv build_pytest_examples_esp32s3: extends: - .build_pytest_template - .rules:build:example_test-esp32s3 script: - - python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir examples --target esp32s3 --size-info $SIZE_INFO_LOCATION -vv + - run_cmd python tools/ci/build_pytest_apps.py examples --target esp32s3 --size-info $SIZE_INFO_LOCATION -vv build_pytest_examples_esp32c3: extends: - .build_pytest_template - .rules:build:example_test-esp32c3 script: - - python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir examples --target esp32c3 --size-info $SIZE_INFO_LOCATION -vv + - run_cmd python tools/ci/build_pytest_apps.py examples --target esp32c3 --size-info $SIZE_INFO_LOCATION -vv build_pytest_components_esp32: extends: - .build_pytest_template - .rules:build:component_ut-esp32 script: - - python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir components --target esp32 --size-info $SIZE_INFO_LOCATION -vv + - run_cmd python tools/ci/build_pytest_apps.py components --target esp32 --size-info $SIZE_INFO_LOCATION -vv build_pytest_components_esp32s2: extends: - .build_pytest_template - .rules:build:component_ut-esp32s2 script: - - python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir components --target esp32s2 --size-info $SIZE_INFO_LOCATION -vv + - run_cmd python tools/ci/build_pytest_apps.py components --target esp32s2 --size-info $SIZE_INFO_LOCATION -vv build_pytest_components_esp32s3: extends: - .build_pytest_template - .rules:build:component_ut-esp32s3 script: - - python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir components --target esp32s3 --size-info $SIZE_INFO_LOCATION -vv - -build_pytest_components_esp32c2: - extends: - - .build_pytest_template - - .rules:build:component_ut-esp32c2 - script: - - python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir components --target esp32c2 --size-info $SIZE_INFO_LOCATION -vv + - run_cmd python tools/ci/build_pytest_apps.py components --target esp32s3 --size-info $SIZE_INFO_LOCATION -vv build_pytest_components_esp32c3: extends: - .build_pytest_template - .rules:build:component_ut-esp32c3 script: - - python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir components --target esp32c3 --size-info $SIZE_INFO_LOCATION -vv + - run_cmd python tools/ci/build_pytest_apps.py components --target esp32c3 --size-info $SIZE_INFO_LOCATION -vv .build_template_app_template: extends: .build_template diff --git a/tools/ci/build_pytest_apps.py b/tools/ci/build_pytest_apps.py index f26e92fdb6..e5683d17fa 100644 --- a/tools/ci/build_pytest_apps.py +++ b/tools/ci/build_pytest_apps.py @@ -9,39 +9,39 @@ import argparse import logging import os import sys +from collections import defaultdict from typing import List -from idf_ci_utils import IDF_PATH, get_pytest_dirs +from idf_ci_utils import IDF_PATH, get_pytest_cases try: from build_apps import build_apps - from find_apps import find_apps, find_builds_for_app - from find_build_apps import BuildItem, CMakeBuildSystem, config_rules_from_str, setup_logging + from find_apps import find_builds_for_app + from find_build_apps import BuildItem, config_rules_from_str, setup_logging except ImportError: sys.path.append(os.path.join(IDF_PATH, 'tools')) from build_apps import build_apps - from find_apps import find_apps, find_builds_for_app - from find_build_apps import BuildItem, CMakeBuildSystem, config_rules_from_str, setup_logging + from find_apps import find_builds_for_app + from find_build_apps import BuildItem, config_rules_from_str, setup_logging def main(args: argparse.Namespace) -> None: - if args.all_pytest_apps: - paths = get_pytest_dirs(args.under_dir) - args.recursive = True - elif args.paths is None: - paths = [os.getcwd()] - else: - paths = args.paths + pytest_cases = [] + for path in args.paths: + pytest_cases += get_pytest_cases(path, args.target) - app_dirs = [] - for path in paths: - app_dirs += find_apps(CMakeBuildSystem, path, args.recursive, [], args.target) + paths = set() + app_configs = defaultdict(set) + for case in pytest_cases: + paths.add(case.app_path) + app_configs[case.app_path].add(case.config) + + app_dirs = list(paths) if not app_dirs: - logging.error('No apps found') - sys.exit(1) + raise RuntimeError('No apps found') - logging.info('Found {} apps'.format(len(app_dirs))) + logging.info(f'Found {len(app_dirs)} apps') app_dirs.sort() # Find compatible configurations of each app, collect them as BuildItems @@ -50,61 +50,58 @@ def main(args: argparse.Namespace) -> None: for app_dir in app_dirs: app_dir = os.path.realpath(app_dir) build_items += find_builds_for_app( - app_dir, - app_dir, - 'build_@t_@w', - f'{app_dir}/build_@t_@w/build.log', - args.target, - 'cmake', - config_rules, - True, + app_path=app_dir, + work_dir=app_dir, + build_dir='build_@t_@w', + build_log=f'{app_dir}/build_@t_@w/build.log', + target_arg=args.target, + build_system='cmake', + config_rules=config_rules, ) - logging.info('Found {} builds'.format(len(build_items))) + logging.info(f'Found {len(build_items)} builds') build_items.sort(key=lambda x: x.build_path) # type: ignore - build_apps(build_items, args.parallel_count, args.parallel_index, False, args.build_verbose, True, None, - args.size_info) + # auto clean up the binaries if no flag --preserve-all + if args.preserve_all is False: + for item in build_items: + if item.config_name not in app_configs[item.app_dir]: + item.preserve = False + + build_apps( + build_items=build_items, + parallel_count=args.parallel_count, + parallel_index=args.parallel_index, + dry_run=False, + build_verbose=args.build_verbose, + keep_going=True, + output_build_list=None, + size_info=args.size_info, + ) if __name__ == '__main__': - parser = argparse.ArgumentParser(description='Tool to generate build steps for IDF apps') - parser.add_argument( - '--recursive', - action='store_true', - help='Look for apps in the specified directories recursively.', + parser = argparse.ArgumentParser( + description='Build all the pytest apps under specified paths. Will auto remove those non-test apps binaries' ) parser.add_argument('--target', required=True, help='Build apps for given target.') parser.add_argument( '--config', default=['sdkconfig.ci=default', 'sdkconfig.ci.*=', '=default'], action='append', - help='Adds configurations (sdkconfig file names) to build. This can either be ' + - 'FILENAME[=NAME] or FILEPATTERN. FILENAME is the name of the sdkconfig file, ' + - 'relative to the project directory, to be used. Optional NAME can be specified, ' + - 'which can be used as a name of this configuration. FILEPATTERN is the name of ' + - 'the sdkconfig file, relative to the project directory, with at most one wildcard. ' + - 'The part captured by the wildcard is used as the name of the configuration.', + help='Adds configurations (sdkconfig file names) to build. This can either be ' + + 'FILENAME[=NAME] or FILEPATTERN. FILENAME is the name of the sdkconfig file, ' + + 'relative to the project directory, to be used. Optional NAME can be specified, ' + + 'which can be used as a name of this configuration. FILEPATTERN is the name of ' + + 'the sdkconfig file, relative to the project directory, with at most one wildcard. ' + + 'The part captured by the wildcard is used as the name of the configuration.', ) parser.add_argument( - '-p', '--paths', - nargs='*', - help='One or more app paths. Will use the current path if not specified.' + 'paths', + nargs='+', + help='One or more app paths. Will use the current path if not specified.', ) parser.add_argument( - '--all-pytest-apps', - action='store_true', - help='Look for all pytest apps. "--paths" would be ignored if specify this flag.' - ) - parser.add_argument( - '--under-dir', - help='Build only the pytest apps under this directory if specified. ' - 'Would be ignored if "--all-pytest-apps" is unflagged.' - ) - parser.add_argument( - '--parallel-count', - default=1, - type=int, - help='Number of parallel build jobs.' + '--parallel-count', default=1, type=int, help='Number of parallel build jobs.' ) parser.add_argument( '--parallel-index', @@ -115,7 +112,7 @@ if __name__ == '__main__': parser.add_argument( '--size-info', type=argparse.FileType('a'), - help='If specified, the test case name and size info json will be written to this file' + help='If specified, the test case name and size info json will be written to this file', ) parser.add_argument( '-v', @@ -128,6 +125,11 @@ if __name__ == '__main__': action='store_true', help='Enable verbose output from build system.', ) + parser.add_argument( + '--preserve-all', + action='store_true', + help='add this flag to preserve the binaries for all apps', + ) arguments = parser.parse_args() setup_logging(arguments) main(arguments) diff --git a/tools/ci/idf_ci_utils.py b/tools/ci/idf_ci_utils.py index 6327e4fa44..9866a5c23d 100644 --- a/tools/ci/idf_ci_utils.py +++ b/tools/ci/idf_ci_utils.py @@ -4,13 +4,20 @@ # SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Apache-2.0 # +import io import logging import os import subprocess import sys -from typing import List +from contextlib import redirect_stdout +from typing import TYPE_CHECKING, List -IDF_PATH = os.path.abspath(os.getenv('IDF_PATH', os.path.join(os.path.dirname(__file__), '..', '..'))) +if TYPE_CHECKING: + from _pytest.nodes import Function + +IDF_PATH = os.path.abspath( + os.getenv('IDF_PATH', os.path.join(os.path.dirname(__file__), '..', '..')) +) def get_submodule_dirs(full_path: bool = False) -> List: @@ -21,9 +28,21 @@ def get_submodule_dirs(full_path: bool = False) -> List: """ dirs = [] try: - lines = subprocess.check_output( - ['git', 'config', '--file', os.path.realpath(os.path.join(IDF_PATH, '.gitmodules')), - '--get-regexp', 'path']).decode('utf8').strip().split('\n') + lines = ( + subprocess.check_output( + [ + 'git', + 'config', + '--file', + os.path.realpath(os.path.join(IDF_PATH, '.gitmodules')), + '--get-regexp', + 'path', + ] + ) + .decode('utf8') + .strip() + .split('\n') + ) for line in lines: _, path = line.split(' ') if full_path: @@ -38,7 +57,11 @@ def get_submodule_dirs(full_path: bool = False) -> List: def _check_git_filemode(full_path): # type: (str) -> bool try: - stdout = subprocess.check_output(['git', 'ls-files', '--stage', full_path]).strip().decode('utf-8') + stdout = ( + subprocess.check_output(['git', 'ls-files', '--stage', full_path]) + .strip() + .decode('utf-8') + ) except subprocess.CalledProcessError: return True @@ -74,8 +97,12 @@ def get_git_files(path: str = IDF_PATH, full_path: bool = False) -> List[str]: # folder if no `.git` folder found in `cwd`. workaround_env = os.environ.copy() workaround_env.pop('GIT_DIR', None) - files = subprocess.check_output(['git', 'ls-files'], cwd=path, env=workaround_env) \ - .decode('utf8').strip().split('\n') + files = ( + subprocess.check_output(['git', 'ls-files'], cwd=path, env=workaround_env) + .decode('utf8') + .strip() + .split('\n') + ) except Exception as e: # pylint: disable=W0703 logging.warning(str(e)) files = [] @@ -86,30 +113,61 @@ def is_in_directory(file_path: str, folder: str) -> bool: return os.path.realpath(file_path).startswith(os.path.realpath(folder) + os.sep) -def get_pytest_dirs(folder: str) -> List[str]: +class PytestCase: + def __init__(self, test_path: str, target: str, config: str, case: str): + self.app_path = os.path.dirname(test_path) + self.test_path = test_path + self.target = target + self.config = config + self.case = case + + def __repr__(self) -> str: + return f'{self.test_path}: {self.target}.{self.config}.{self.case}' + + +class PytestCollectPlugin: + def __init__(self, target: str) -> None: + self.target = target + self.nodes: List[PytestCase] = [] + + def pytest_collection_modifyitems(self, items: List['Function']) -> None: + for item in items: + try: + file_path = str(item.path) + except AttributeError: + # pytest 6.x + file_path = item.fspath + + target = self.target + if hasattr(item, 'callspec'): + config = item.callspec.params.get('config', 'default') + else: + config = 'default' + case_name = item.originalname + + self.nodes.append(PytestCase(file_path, target, config, case_name)) + + +def get_pytest_cases(folder: str, target: str) -> List[PytestCase]: import pytest - from _pytest.nodes import Item + from _pytest.config import ExitCode - class CollectPlugin: - def __init__(self) -> None: - self.nodes: List[Item] = [] + collector = PytestCollectPlugin(target) - def pytest_collection_modifyitems(self, items: List[Item]) -> None: - for item in items: - self.nodes.append(item) + with io.StringIO() as buf: + with redirect_stdout(buf): + res = pytest.main(['--collect-only', folder, '-q', '--target', target], plugins=[collector]) + if res.value != ExitCode.OK: + if res.value == ExitCode.NO_TESTS_COLLECTED: + print(f'WARNING: no pytest app found for target {target} under folder {folder}') + else: + print(buf.getvalue()) + raise RuntimeError('pytest collection failed') - collector = CollectPlugin() + return collector.nodes - res = pytest.main(['--collect-only', '-q', folder], plugins=[collector]) - if res.value != 0: - raise RuntimeError('pytest collection failed') - sys.stdout.flush() # print instantly +def get_pytest_app_paths(folder: str, target: str) -> List[str]: + nodes = get_pytest_cases(folder, target) - try: - test_file_paths = set(node.path for node in collector.nodes) - except AttributeError: - # pytest 6.x - test_file_paths = set(node.fspath for node in collector.nodes) - - return [os.path.dirname(file) for file in test_file_paths] + return list({node.app_path for node in nodes})