remove dir with exclude file pattern

This commit is contained in:
Fu Hanxi
2020-07-27 14:47:15 +08:00
parent fd6a2e406a
commit 0d6f0e9bc3
2 changed files with 20 additions and 3 deletions

View File

@@ -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():
@@ -129,7 +129,7 @@ def main():
if not build_info.preserve:
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_path, ignore_errors=True)
rmdir(build_info.build_path, exclude_file_pattern=SIZE_JSON_FN)
if failed_builds:
logging.error("The following build have failed:")

View File

@@ -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,