From afe3c2bc1bc79f3cd1f37707a1893774f2a3acce Mon Sep 17 00:00:00 2001 From: Rikka Date: Sun, 14 Mar 2021 09:51:39 +0800 Subject: [PATCH] Fix "rm_rf" in build.py on Windows prebuilt/windows-x86_64/bin/libpython2.7.dll prebuilt/windows-x86_64/lib/python2.7/config/libpython2.7.dll.a These two files in NDK has read-only attribute on Windows, remove these files with Python will get "WindowsError: [Error 5] Access is denied". It will finally make "build.py ndk" unable to remove the "magisk" folder. This commit add a onerror callback for "shutil.rmtree" which clear the "read-only" attribute and retry. --- build.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/build.py b/build.py index d9c97ce5b..dea5d8e19 100755 --- a/build.py +++ b/build.py @@ -11,6 +11,7 @@ import lzma import platform import urllib.request import os.path as op +import stat from distutils.dir_util import copy_tree @@ -96,9 +97,16 @@ def rm(file): raise +def rm_on_error(func, path, _): + # Remove a read-only file on Windows will get "WindowsError: [Error 5] Access is denied" + # Clear the "read-only" and retry + os.chmod(path, stat.S_IWRITE) + os.unlink(path) + + def rm_rf(path): vprint(f'rm -rf {path}') - shutil.rmtree(path, ignore_errors=True) + shutil.rmtree(path, ignore_errors=True, onerror=rm_on_error) def mkdir(path, mode=0o755):