diff --git a/package/cloudshell/iac/terraform/services/local_dir_service.py b/package/cloudshell/iac/terraform/services/local_dir_service.py index 2c7861c..e076a07 100644 --- a/package/cloudshell/iac/terraform/services/local_dir_service.py +++ b/package/cloudshell/iac/terraform/services/local_dir_service.py @@ -8,6 +8,26 @@ from cloudshell.iac.terraform.services.sandox_data import SandboxDataHandler +def handle_remove_readonly(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 + + class LocalDir: @staticmethod def delete_local_temp_dir(sandbox_data_handler: SandboxDataHandler, tf_working_dir: str): @@ -20,7 +40,7 @@ def delete_local_temp_dir(sandbox_data_handler: SandboxDataHandler, tf_working_d tmp_folder_found = True tf_path = Path(tf_path.parent.absolute()) tf_path_str = str(tf_path) - shutil.rmtree(tf_path_str) + shutil.rmtree(tf_path_str, onerror=handle_remove_readonly) sandbox_data_handler.set_tf_working_dir("") @staticmethod diff --git a/package/requirements.txt b/package/requirements.txt index de572b4..7285c7e 100644 --- a/package/requirements.txt +++ b/package/requirements.txt @@ -2,4 +2,4 @@ cloudshell-shell-core==5.0.5 requests==2.25.1 cloudshell-automation-api==2021.1.0.181140 retry==0.9.2 -python-hcl2==2.0.1 +python-hcl2==3.0.5 diff --git a/package/version.txt b/package/version.txt index d2d61a7..e2cac26 100644 --- a/package/version.txt +++ b/package/version.txt @@ -1 +1 @@ -1.2.2 \ No newline at end of file +1.2.3 \ No newline at end of file