Handle errors on Windows where some of the files have their read-only bit set (#242)

This commit is contained in:
Joel Challis
2026-07-01 02:48:30 +01:00
committed by GitHub
parent b87b18e9d4
commit 8c6339493d
2 changed files with 18 additions and 2 deletions
+17
View File
@@ -1,7 +1,10 @@
"""Useful helper functions.
"""
import os
import sys
import stat
import json
import shutil
from functools import lru_cache
from pathlib import Path
@@ -18,6 +21,20 @@ def AbsPath(arg): # noqa: N802
return arg
def rmtree(path):
"""Safe version of shutil.rmtree which handles errors on Windows where some of the files have their read-only bit set."""
def remove_readonly(func, path, _):
"""Clear the readonly bit and reattempt the removal."""
os.chmod(path, stat.S_IWRITE)
func(path)
if sys.version_info >= (3, 12):
# See https://docs.python.org/3.12/whatsnew/3.12.html#shutil
return shutil.rmtree(path, onexc=remove_readonly)
else:
return shutil.rmtree(path, onerror=remove_readonly)
def is_qmk_firmware(qmk_firmware):
"""Returns True if the given Path() is a qmk_firmware clone.
"""
+1 -2
View File
@@ -5,12 +5,11 @@ import shlex
import subprocess
import sys
from pathlib import Path
from shutil import rmtree
from milc import cli
from milc.questions import choice, question, yesno
from qmk_cli.git import git_clone
from qmk_cli.helpers import AbsPath, is_qmk_firmware
from qmk_cli.helpers import AbsPath, is_qmk_firmware, rmtree
default_base = 'https://github.com'
default_repo = 'qmk_firmware'