Skip to content

Commit ae078e9

Browse files
committed
Use a special technique to remove git repos on Windows
1 parent 8c89198 commit ae078e9

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

bench_runner/git.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515

1616
from .util import PathLike
17+
from . import util
1718

1819

1920
def get_log(
@@ -147,7 +148,7 @@ def clone(
147148
if is_hash and (dirname / ".git").is_dir() and get_git_hash(dirname) == branch:
148149
# This is a git repo, and the hash matches
149150
return
150-
shutil.rmtree(dirname)
151+
util.smart_rmtree(dirname)
151152

152153
# Fetching a hash and fetching a branch require different approaches
153154

bench_runner/util.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
import shutil
77
import subprocess
8+
import stat
89
import sys
910
from typing import Iterable, Iterator, Literal, TypeAlias, Union
1011

@@ -105,6 +106,37 @@ def valid_version(version: str) -> bool:
105106
return False
106107

107108

109+
if sys.platform.startswith("win"):
110+
if sys.version_info >= (3, 12):
111+
112+
def smart_rmtree(path: PathLike) -> None:
113+
def onexc(func, path, exc):
114+
# Is the error an access error?
115+
if not os.access(path, os.W_OK):
116+
os.chmod(path, stat.S_IWUSR)
117+
func(path)
118+
else:
119+
raise exc
120+
121+
shutil.rmtree(path, onexc=onexc)
122+
123+
else:
124+
125+
def smart_rmtree(path: PathLike) -> None:
126+
def onerror(func, path, error):
127+
# Is the error an access error?
128+
if not os.access(path, os.W_OK):
129+
os.chmod(path, stat.S_IWUSR)
130+
func(path)
131+
else:
132+
raise exc[1]
133+
134+
shutil.rmtree(path, onerror=onerror)
135+
136+
else:
137+
smart_rmtree = shutil.rmtree
138+
139+
108140
if os.getenv("GITHUB_ACTIONS") == "true":
109141

110142
@contextlib.contextmanager

0 commit comments

Comments
 (0)