Skip to content

Commit 8272bc3

Browse files
committed
Refactor usage of shutil.rmtree in other parts of setuptools
1 parent 6ddac39 commit 8272bc3

File tree

3 files changed

+9
-33
lines changed

3 files changed

+9
-33
lines changed

setuptools/command/bdist_wheel.py

+5-28
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import os
1010
import re
1111
import shutil
12-
import stat
1312
import struct
1413
import sys
1514
import sysconfig
@@ -18,23 +17,19 @@
1817
from email.generator import BytesGenerator, Generator
1918
from email.policy import EmailPolicy
2019
from glob import iglob
21-
from shutil import rmtree
22-
from typing import TYPE_CHECKING, Callable, Literal, cast
20+
from typing import Literal, cast
2321
from zipfile import ZIP_DEFLATED, ZIP_STORED
2422

2523
from packaging import tags, version as _packaging_version
2624
from wheel.metadata import pkginfo_to_metadata
2725
from wheel.wheelfile import WheelFile
2826

29-
from .. import Command, __version__
27+
from .. import Command, __version__, _shutil
3028
from ..warnings import SetuptoolsDeprecationWarning
3129
from .egg_info import egg_info as egg_info_cls
3230

3331
from distutils import log
3432

35-
if TYPE_CHECKING:
36-
from _typeshed import ExcInfo
37-
3833

3934
def safe_name(name: str) -> str:
4035
"""Convert an arbitrary string to a standard distribution name
@@ -148,21 +143,6 @@ def safer_version(version: str) -> str:
148143
return safe_version(version).replace("-", "_")
149144

150145

151-
def remove_readonly(
152-
func: Callable[..., object],
153-
path: str,
154-
excinfo: ExcInfo,
155-
) -> None:
156-
remove_readonly_exc(func, path, excinfo[1])
157-
158-
159-
def remove_readonly_exc(
160-
func: Callable[..., object], path: str, exc: BaseException
161-
) -> None:
162-
os.chmod(path, stat.S_IWRITE)
163-
func(path)
164-
165-
166146
class bdist_wheel(Command):
167147
description = "create a wheel distribution"
168148

@@ -458,7 +438,7 @@ def run(self):
458438
shutil.copytree(self.dist_info_dir, distinfo_dir)
459439
# Egg info is still generated, so remove it now to avoid it getting
460440
# copied into the wheel.
461-
shutil.rmtree(self.egginfo_dir)
441+
_shutil.rmtree(self.egginfo_dir)
462442
else:
463443
# Convert the generated egg-info into dist-info.
464444
self.egg2dist(self.egginfo_dir, distinfo_dir)
@@ -483,10 +463,7 @@ def run(self):
483463
if not self.keep_temp:
484464
log.info(f"removing {self.bdist_dir}")
485465
if not self.dry_run:
486-
if sys.version_info < (3, 12):
487-
rmtree(self.bdist_dir, onerror=remove_readonly)
488-
else:
489-
rmtree(self.bdist_dir, onexc=remove_readonly_exc)
466+
_shutil.rmtree(self.bdist_dir)
490467

491468
def write_wheelfile(
492469
self, wheelfile_base: str, generator: str = f"setuptools ({__version__})"
@@ -570,7 +547,7 @@ def egg2dist(self, egginfo_path: str, distinfo_path: str) -> None:
570547
def adios(p: str) -> None:
571548
"""Appropriately delete directory, file or link."""
572549
if os.path.exists(p) and not os.path.islink(p) and os.path.isdir(p):
573-
shutil.rmtree(p)
550+
_shutil.rmtree(p)
574551
elif os.path.exists(p):
575552
os.unlink(p)
576553

setuptools/command/editable_wheel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from types import TracebackType
2828
from typing import TYPE_CHECKING, Protocol, TypeVar, cast
2929

30-
from .. import Command, _normalization, _path, errors, namespaces
30+
from .. import Command, _normalization, _path, _shutil, errors, namespaces
3131
from .._path import StrPath
3232
from ..compat import py312
3333
from ..discovery import find_package_path
@@ -773,7 +773,7 @@ def _is_nested(pkg: str, pkg_path: str, parent: str, parent_path: str) -> bool:
773773

774774
def _empty_dir(dir_: _P) -> _P:
775775
"""Create a directory ensured to be empty. Existing files may be removed."""
776-
shutil.rmtree(dir_, ignore_errors=True)
776+
_shutil.rmtree(dir_, ignore_errors=True)
777777
os.makedirs(dir_)
778778
return dir_
779779

setuptools/command/rotate.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from __future__ import annotations
22

33
import os
4-
import shutil
54
from typing import ClassVar
65

7-
from setuptools import Command
6+
from .. import Command, _shutil
87

98
from distutils import log
109
from distutils.errors import DistutilsOptionError
@@ -61,6 +60,6 @@ def run(self) -> None:
6160
log.info("Deleting %s", f)
6261
if not self.dry_run:
6362
if os.path.isdir(f):
64-
shutil.rmtree(f)
63+
_shutil.rmtree(f)
6564
else:
6665
os.unlink(f)

0 commit comments

Comments
 (0)