From 8272bc3186fb5991e290c2afd31d1f5fb2d74fb5 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Tue, 15 Oct 2024 17:20:45 +0100 Subject: [PATCH] Refactor usage of shutil.rmtree in other parts of setuptools --- setuptools/command/bdist_wheel.py | 33 +++++----------------------- setuptools/command/editable_wheel.py | 4 ++-- setuptools/command/rotate.py | 5 ++--- 3 files changed, 9 insertions(+), 33 deletions(-) diff --git a/setuptools/command/bdist_wheel.py b/setuptools/command/bdist_wheel.py index 8cf91538f9..5855a8a832 100644 --- a/setuptools/command/bdist_wheel.py +++ b/setuptools/command/bdist_wheel.py @@ -9,7 +9,6 @@ import os import re import shutil -import stat import struct import sys import sysconfig @@ -18,23 +17,19 @@ from email.generator import BytesGenerator, Generator from email.policy import EmailPolicy from glob import iglob -from shutil import rmtree -from typing import TYPE_CHECKING, Callable, Literal, cast +from typing import Literal, cast from zipfile import ZIP_DEFLATED, ZIP_STORED from packaging import tags, version as _packaging_version from wheel.metadata import pkginfo_to_metadata from wheel.wheelfile import WheelFile -from .. import Command, __version__ +from .. import Command, __version__, _shutil from ..warnings import SetuptoolsDeprecationWarning from .egg_info import egg_info as egg_info_cls from distutils import log -if TYPE_CHECKING: - from _typeshed import ExcInfo - def safe_name(name: str) -> str: """Convert an arbitrary string to a standard distribution name @@ -148,21 +143,6 @@ def safer_version(version: str) -> str: return safe_version(version).replace("-", "_") -def remove_readonly( - func: Callable[..., object], - path: str, - excinfo: ExcInfo, -) -> None: - remove_readonly_exc(func, path, excinfo[1]) - - -def remove_readonly_exc( - func: Callable[..., object], path: str, exc: BaseException -) -> None: - os.chmod(path, stat.S_IWRITE) - func(path) - - class bdist_wheel(Command): description = "create a wheel distribution" @@ -458,7 +438,7 @@ def run(self): shutil.copytree(self.dist_info_dir, distinfo_dir) # Egg info is still generated, so remove it now to avoid it getting # copied into the wheel. - shutil.rmtree(self.egginfo_dir) + _shutil.rmtree(self.egginfo_dir) else: # Convert the generated egg-info into dist-info. self.egg2dist(self.egginfo_dir, distinfo_dir) @@ -483,10 +463,7 @@ def run(self): if not self.keep_temp: log.info(f"removing {self.bdist_dir}") if not self.dry_run: - if sys.version_info < (3, 12): - rmtree(self.bdist_dir, onerror=remove_readonly) - else: - rmtree(self.bdist_dir, onexc=remove_readonly_exc) + _shutil.rmtree(self.bdist_dir) def write_wheelfile( self, wheelfile_base: str, generator: str = f"setuptools ({__version__})" @@ -570,7 +547,7 @@ def egg2dist(self, egginfo_path: str, distinfo_path: str) -> None: def adios(p: str) -> None: """Appropriately delete directory, file or link.""" if os.path.exists(p) and not os.path.islink(p) and os.path.isdir(p): - shutil.rmtree(p) + _shutil.rmtree(p) elif os.path.exists(p): os.unlink(p) diff --git a/setuptools/command/editable_wheel.py b/setuptools/command/editable_wheel.py index 30570e092a..db9a50c3af 100644 --- a/setuptools/command/editable_wheel.py +++ b/setuptools/command/editable_wheel.py @@ -27,7 +27,7 @@ from types import TracebackType from typing import TYPE_CHECKING, Protocol, TypeVar, cast -from .. import Command, _normalization, _path, errors, namespaces +from .. import Command, _normalization, _path, _shutil, errors, namespaces from .._path import StrPath from ..compat import py312 from ..discovery import find_package_path @@ -773,7 +773,7 @@ def _is_nested(pkg: str, pkg_path: str, parent: str, parent_path: str) -> bool: def _empty_dir(dir_: _P) -> _P: """Create a directory ensured to be empty. Existing files may be removed.""" - shutil.rmtree(dir_, ignore_errors=True) + _shutil.rmtree(dir_, ignore_errors=True) os.makedirs(dir_) return dir_ diff --git a/setuptools/command/rotate.py b/setuptools/command/rotate.py index c10e8d5024..acdce07baa 100644 --- a/setuptools/command/rotate.py +++ b/setuptools/command/rotate.py @@ -1,10 +1,9 @@ from __future__ import annotations import os -import shutil from typing import ClassVar -from setuptools import Command +from .. import Command, _shutil from distutils import log from distutils.errors import DistutilsOptionError @@ -61,6 +60,6 @@ def run(self) -> None: log.info("Deleting %s", f) if not self.dry_run: if os.path.isdir(f): - shutil.rmtree(f) + _shutil.rmtree(f) else: os.unlink(f)