Skip to content

Handle Access Denied error when cleaning up build directory on Windows #960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion scripts/gha/build_testapps.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import os
import platform
import shutil
import stat
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -697,11 +698,17 @@ def _run(args, timeout=_DEFAULT_RUN_TIMEOUT_SECONDS, capture_output=False, text=
check=check)


def _handle_readonly_file(func, path, excinfo):
"""Function passed into shutil.rmtree to handle Access Denied error"""
os.chmod(path, stat.S_IWRITE)
func(path) # will re-throw if a different error occurrs


def _rm_dir_safe(directory_path):
"""Removes directory at given path. No error if dir doesn't exist."""
logging.info("Deleting %s...", directory_path)
try:
shutil.rmtree(directory_path)
shutil.rmtree(directory_path, onerror=_handle_readonly_file)
except OSError as e:
# There are two known cases where this can happen:
# The directory doesn't exist (FileNotFoundError)
Expand Down