From 018966313c98827cb6b5323202d3ab27979e837f Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Tue, 7 Dec 2021 11:06:50 -0800 Subject: [PATCH] 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 --- engine/src/flutter/tools/download_dart_sdk.py | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/tools/download_dart_sdk.py b/engine/src/flutter/tools/download_dart_sdk.py index d77d8ccdeb..85de872b96 100755 --- a/engine/src/flutter/tools/download_dart_sdk.py +++ b/engine/src/flutter/tools/download_dart_sdk.py @@ -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: