Skip to content

Commit

Permalink
Refactor usage of shutil.rmtree in other parts of setuptools
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Nov 11, 2024
1 parent 6ddac39 commit 8272bc3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 33 deletions.
33 changes: 5 additions & 28 deletions setuptools/command/bdist_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import os
import re
import shutil
import stat
import struct
import sys
import sysconfig
Expand All @@ -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
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand All @@ -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__})"
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions setuptools/command/editable_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_

Expand Down
5 changes: 2 additions & 3 deletions setuptools/command/rotate.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 8272bc3

Please sign in to comment.