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.
This commit is contained in:
Rikka 2021-03-14 09:51:39 +08:00 committed by GitHub
parent 82f8948fd4
commit afe3c2bc1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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):