diff --git a/tools/build_apps.py b/tools/build_apps.py index 1abd2012c5..d95717fd79 100755 --- a/tools/build_apps.py +++ b/tools/build_apps.py @@ -6,10 +6,10 @@ import argparse import logging -import shutil import sys from find_build_apps import BuildItem, BuildError, setup_logging, BUILD_SYSTEMS +from find_build_apps.common import rmdir, SIZE_JSON_FN def main(): @@ -127,9 +127,9 @@ def main(): if args.size_info: build_info.write_size_info(args.size_info) if not build_info.preserve: - logging.info("Removing build directory {}".format(build_info.build_dir)) + logging.info("Removing build directory {}".format(build_info.build_path)) # we only remove binaries here, log files are still needed by check_build_warnings.py - shutil.rmtree(build_info.build_dir, ignore_errors=True) + rmdir(build_info.build_path, exclude_file_pattern=SIZE_JSON_FN) if failed_builds: logging.error("The following build have failed:") diff --git a/tools/find_build_apps/common.py b/tools/find_build_apps/common.py index 7b0b1c95ef..51368c5ce8 100644 --- a/tools/find_build_apps/common.py +++ b/tools/find_build_apps/common.py @@ -22,6 +22,7 @@ FULL_NAME_PLACEHOLDER = "@f" INDEX_PLACEHOLDER = "@i" IDF_SIZE_PY = os.path.join(os.environ["IDF_PATH"], "tools", "idf_size.py") +SIZE_JSON_FN = 'size.json' SDKCONFIG_LINE_REGEX = re.compile(r"^([^=]+)=\"?([^\"\n]*)\"?\n*$") @@ -68,6 +69,22 @@ def find_first_match(pattern, path): return None +def rmdir(path, exclude_file_pattern=None): + if not exclude_file_pattern: + shutil.rmtree(path, ignore_errors=True) + return + + for root, dirs, files in os.walk(path, topdown=False): + for f in files: + if not fnmatch.fnmatch(f, exclude_file_pattern): + os.remove(os.path.join(root, f)) + for d in dirs: + try: + os.rmdir(os.path.join(root, d)) + except OSError: + pass + + class BuildItem(object): """ Instance of this class represents one build of an application. @@ -242,7 +259,7 @@ class BuildItem(object): if not map_file: raise ValueError('.map file not found under "{}"'.format(self.build_path)) - size_json_fp = os.path.join(self.build_path, 'size.json') + size_json_fp = os.path.join(self.build_path, SIZE_JSON_FN) idf_size_args = [ sys.executable, IDF_SIZE_PY,