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 Oct 16, 2024
1 parent 5f0e608 commit 43822f1
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,31 +9,26 @@
import os
import re
import shutil
import stat
import struct
import sys
import sysconfig
import warnings
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, Iterable, Literal, Sequence, cast
from typing import Iterable, Literal, Sequence, 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 @@ -152,21 +147,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 @@ -462,7 +442,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 @@ -487,10 +467,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 @@ -574,7 +551,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 @@ -26,7 +26,7 @@
from types import TracebackType
from typing import TYPE_CHECKING, Iterable, Iterator, Mapping, 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 @@ -775,7 +775,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,9 +1,8 @@
from __future__ import annotations

import os
import shutil

from setuptools import Command
from .. import Command, _shutil

from distutils import log
from distutils.errors import DistutilsOptionError
Expand Down Expand Up @@ -60,6 +59,6 @@ def run(self):
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 43822f1

Please sign in to comment.