Add error handler for os.rmtree calls (flutter/engine#30169)

In cases wher rmtree encounters an error, attempt to fix file
permissions and re-run the failing operation.

Issue: https://github.com/flutter/flutter/issues/94492
This commit is contained in:
Chris Bracken
2021-12-07 11:06:50 -08:00
committed by GitHub
parent be166e754a
commit 018966313c

View File

@@ -143,16 +143,34 @@ class ZipFileWithPermissions(zipfile.ZipFile):
return targetpath
def OnErrorRmTree(func, path, exc_info):
"""
Error handler for ``shutil.rmtree``.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
Usage : ``shutil.rmtree(path, onerror=onerror)``
"""
import stat
# Is the error an access error?
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
# Extracts a Dart SDK in //fluter/prebuilts
def ExtractDartSDK(archive, os_name, arch, verbose):
os_arch = '{}-{}'.format(os_name, arch)
dart_sdk = os.path.join(FLUTTER_PREBUILTS_DIR, os_arch, 'dart-sdk')
if os.path.isdir(dart_sdk):
shutil.rmtree(dart_sdk)
shutil.rmtree(dart_sdk, onerror=OnErrorRmTree)
extract_dest = os.path.join(FLUTTER_PREBUILTS_DIR, os_arch, 'temp')
if os.path.isdir(extract_dest):
shutil.rmtree(extract_dest)
shutil.rmtree(extract_dest, onerror=OnErrorRmTree)
os.makedirs(extract_dest, exist_ok=True)
if verbose: