codekingpro/portable-devtools
114k
1"""Extensions to the 'distutils' for large or complex distributions"""2# mypy: disable_error_code=override3# Command.reinitialize_command has an extra **kw param that distutils doesn't have4# Can't disable on the exact line because distutils doesn't exists on Python 3.125# and mypy isn't aware of distutils_hack, causing distutils.core.Command to be Any,6# and a [unused-ignore] to be raised on 3.12+7 8from __future__ import annotations9 10import functools11import os12import sys13from abc import abstractmethod14from collections.abc import Mapping15from typing import TYPE_CHECKING, TypeVar, overload16 17sys.path.extend(((vendor_path := os.path.join(os.path.dirname(os.path.dirname(__file__)), 'setuptools', '_vendor')) not in sys.path) * [vendor_path]) # fmt: skip18# workaround for #447619sys.modules.pop('backports', None)20 21import _distutils_hack.override # noqa: F40122 23from . import logging, monkey24from .depends import Require25from .discovery import PackageFinder, PEP420PackageFinder26from .dist import Distribution27from .extension import Extension28from .version import __version__ as __version__29from .warnings import SetuptoolsDeprecationWarning30 31import distutils.core32 33__all__ = [34 'setup',35 'Distribution',36 'Command',37 'Extension',38 'Require',39 'SetuptoolsDeprecationWarning',40 'find_packages',41 'find_namespace_packages',42]43 44_CommandT = TypeVar("_CommandT", bound="_Command")45 46bootstrap_install_from = None47 48find_packages = PackageFinder.find49find_namespace_packages = PEP420PackageFinder.find50 51 52def _install_setup_requires(attrs):53 # Note: do not use `setuptools.Distribution` directly, as54 # our PEP 517 backend patch `distutils.core.Distribution`.55 class MinimalDistribution(distutils.core.Distribution):56 """57 A minimal version of a distribution for supporting the58 fetch_build_eggs interface.59 """60 61 def __init__(self, attrs: Mapping[str, object]) -> None:62 _incl = 'dependency_links', 'setup_requires'63 filtered = {k: attrs[k] for k in set(_incl) & set(attrs)}64 super().__init__(filtered)65 # Prevent accidentally triggering discovery with incomplete set of attrs66 self.set_defaults._disable()67 68 def _get_project_config_files(self, filenames=None):69 """Ignore ``pyproject.toml``, they are not related to setup_requires"""70 try:71 cfg, _toml = super()._split_standard_project_metadata(filenames)72 except Exception:73 return filenames, ()74 return cfg, ()75 76 def finalize_options(self):77 """78 Disable finalize_options to avoid building the working set.79 Ref #2158.80 """81 82 dist = MinimalDistribution(attrs)83 84 # Honor setup.cfg's options.85 dist.parse_config_files(ignore_option_errors=True)86 if dist.setup_requires:87 _fetch_build_eggs(dist)88 89 90def _fetch_build_eggs(dist: Distribution):91 try:92 dist.fetch_build_eggs(dist.setup_requires)93 except Exception as ex:94 msg = """95 It is possible a package already installed in your system96 contains an version that is invalid according to PEP 440.97 You can try `pip install --use-pep517` as a workaround for this problem,98 or rely on a new virtual environment.99 100 If the problem refers to a package that is not installed yet,101 please contact that package's maintainers or distributors.102 """103 if "InvalidVersion" in ex.__class__.__name__:104 if hasattr(ex, "add_note"):105 ex.add_note(msg) # PEP 678106 else:107 dist.announce(f"\n{msg}\n")108 raise109 110 111def setup(**attrs) -> Distribution:112 logging.configure()113 # Make sure we have any requirements needed to interpret 'attrs'.114 _install_setup_requires(attrs)115 # Override return type of distutils.core.Distribution with setuptools.dist.Distribution116 # (implicitly implemented via `setuptools.monkey.patch_all`).117 return distutils.core.setup(**attrs) # type: ignore[return-value]118 119 120setup.__doc__ = distutils.core.setup.__doc__121 122if TYPE_CHECKING:123 # Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/10962124 from distutils.core import Command as _Command125else:126 _Command = monkey.get_unpatched(distutils.core.Command)127 128 129class Command(_Command):130 """131 Setuptools internal actions are organized using a *command design pattern*.132 This means that each action (or group of closely related actions) executed during133 the build should be implemented as a ``Command`` subclass.134 135 These commands are abstractions and do not necessarily correspond to a command that136 can (or should) be executed via a terminal, in a CLI fashion (although historically137 they would).138 139 When creating a new command from scratch, custom defined classes **SHOULD** inherit140 from ``setuptools.Command`` and implement a few mandatory methods.141 Between these mandatory methods, are listed:142 :meth:`initialize_options`, :meth:`finalize_options` and :meth:`run`.143 144 A useful analogy for command classes is to think of them as subroutines with local145 variables called "options". The options are "declared" in :meth:`initialize_options`146 and "defined" (given their final values, aka "finalized") in :meth:`finalize_options`,147 both of which must be defined by every command class. The "body" of the subroutine,148 (where it does all the work) is the :meth:`run` method.149 Between :meth:`initialize_options` and :meth:`finalize_options`, ``setuptools`` may set150 the values for options/attributes based on user's input (or circumstance),151 which means that the implementation should be careful to not overwrite values in152 :meth:`finalize_options` unless necessary.153 154 Please note that other commands (or other parts of setuptools) may also overwrite155 the values of the command's options/attributes multiple times during the build156 process.157 Therefore it is important to consistently implement :meth:`initialize_options` and158 :meth:`finalize_options`. For example, all derived attributes (or attributes that159 depend on the value of other attributes) **SHOULD** be recomputed in160 :meth:`finalize_options`.161 162 When overwriting existing commands, custom defined classes **MUST** abide by the163 same APIs implemented by the original class. They also **SHOULD** inherit from the164 original class.165 """166 167 command_consumes_arguments = False168 distribution: Distribution # override distutils.dist.Distribution with setuptools.dist.Distribution169 170 dry_run = False # type: ignore[assignment] # pyright: ignore[reportAssignmentType] (until #4689; see #4872)171 """172 For compatibility with vendored bdist_wheel.173 https://github.com/pypa/setuptools/pull/4872/files#r1986395142174 """175 176 def __init__(self, dist: Distribution, **kw) -> None:177 """178 Construct the command for dist, updating179 vars(self) with any keyword parameters.180 """181 super().__init__(dist)182 vars(self).update(kw)183 184 @overload185 def reinitialize_command(186 self, command: str, reinit_subcommands: bool = False, **kw187 ) -> Command: ... # override distutils.cmd.Command with setuptools.Command188 @overload189 def reinitialize_command(190 self, command: _CommandT, reinit_subcommands: bool = False, **kw191 ) -> _CommandT: ...192 def reinitialize_command(193 self, command: str | _Command, reinit_subcommands: bool = False, **kw194 ) -> Command | _Command:195 cmd = _Command.reinitialize_command(self, command, reinit_subcommands)196 vars(cmd).update(kw)197 return cmd # pyright: ignore[reportReturnType] # pypa/distutils#307198 199 @abstractmethod200 def initialize_options(self) -> None:201 """202 Set or (reset) all options/attributes/caches used by the command203 to their default values. Note that these values may be overwritten during204 the build.205 """206 raise NotImplementedError207 208 @abstractmethod209 def finalize_options(self) -> None:210 """211 Set final values for all options/attributes used by the command.212 Most of the time, each option/attribute/cache should only be set if it does not213 have any value yet (e.g. ``if self.attr is None: self.attr = val``).214 """215 raise NotImplementedError216 217 @abstractmethod218 def run(self) -> None:219 """220 Execute the actions intended by the command.221 (Side effects **SHOULD** only take place when :meth:`run` is executed,222 for example, creating new files or writing to the terminal output).223 """224 raise NotImplementedError225 226 227def _find_all_simple(path):228 """229 Find all files under 'path'230 """231 results = (232 os.path.join(base, file)233 for base, dirs, files in os.walk(path, followlinks=True)234 for file in files235 )236 return filter(os.path.isfile, results)237 238 239def findall(dir=os.curdir):240 """241 Find all files under 'dir' and return the list of full filenames.242 Unless dir is '.', return full filenames with dir prepended.243 """244 files = _find_all_simple(dir)245 if dir == os.curdir:246 make_rel = functools.partial(os.path.relpath, start=dir)247 files = map(make_rel, files)248 return list(files)249 250 251class sic(str):252 """Treat this string as-is (https://en.wikipedia.org/wiki/Sic)"""253 254 255# Apply monkey patches256monkey.patch_all()257 