From 10841f48b3e62ed94a61befd65970fc9937fd0f8 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 6 Dec 2021 14:25:36 -0800 Subject: [PATCH] Add error handler for os.rmtree calls 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 --- tools/download_dart_sdk.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tools/download_dart_sdk.py b/tools/download_dart_sdk.py index d77d8ccdeb932..85de872b96455 100755 --- a/tools/download_dart_sdk.py +++ b/tools/download_dart_sdk.py @@ -143,16 +143,34 @@ def _extract_member(self, member, targetpath, pwd): 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: