From 72243114b525a481b12a948f8821ce2fdbc4c059 Mon Sep 17 00:00:00 2001 From: CXSforHPU <19511928573@163.com> Date: Sat, 17 Oct 2026 02:05:35 +0800 Subject: [PATCH] [tools][mkdist] fix repeated RTT_ROOT injection and rmtree read-only failure on rebuild --- tools/mkdist.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/tools/mkdist.py b/tools/mkdist.py index 7024d43d7edf..49bc74689d32 100644 --- a/tools/mkdist.py +++ b/tools/mkdist.py @@ -46,16 +46,21 @@ def do_copy_file(src, dst): shutil.copy2(src, dst) def do_copy_folder(src_dir, dst_dir, ignore=None): - import shutil + import shutil, stat + + def _remove_readonly(func, path, excinfo): + os.chmod(path, stat.S_IWRITE) + func(path) + # check source directory if not os.path.exists(src_dir): return try: if os.path.exists(dst_dir): - shutil.rmtree(dst_dir) - except: - print('Deletes folder: %s failed.' % dst_dir) + shutil.rmtree(dst_dir, onerror = _remove_readonly) + except Exception as e: + print('Deletes folder: %s failed: %s\n' % (dst_dir, e)) return shutil.copytree(src_dir, dst_dir, ignore = ignore) @@ -102,14 +107,17 @@ def bsp_copy_files(bsp_root, dist_dir): ignore_patterns('build', '__pycache__', 'dist', '*.pyc', '*.old', '*.map', 'rtthread.bin', '.sconsign.dblite', '*.elf', '*.axf', 'cconfig.h')) def bsp_update_sconstruct(dist_dir): - with open(os.path.join(dist_dir, 'SConstruct'), 'r') as f: + sconstruct_path = os.path.join(dist_dir, 'SConstruct') + with open(sconstruct_path, 'r') as f: data = f.readlines() - with open(os.path.join(dist_dir, 'SConstruct'), 'w') as f: + # idempotency: if the marker is already present, do nothing on re-runs + if any(line.find('# set RTT_ROOT') != -1 for line in data): + return + with open(sconstruct_path, 'w') as f: for line in data: - if line.find('RTT_ROOT') != -1: - if line.find('sys.path') != -1: - f.write('# set RTT_ROOT\n') - f.write('if not os.getenv("RTT_ROOT"): \n RTT_ROOT="rt-thread"\n\n') + if line.find('RTT_ROOT') != -1 and line.find('sys.path') != -1: + f.write('# set RTT_ROOT\n') + f.write('if not os.getenv("RTT_ROOT"): \n RTT_ROOT="rt-thread"\n\n') f.write(line) def bsp_update_kconfig_testcases(dist_dir):