dist.py 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. from __future__ import annotations
  2. import functools
  3. import io
  4. import itertools
  5. import numbers
  6. import os
  7. import re
  8. import sys
  9. from collections.abc import Iterable, Iterator, MutableMapping, Sequence
  10. from glob import glob
  11. from pathlib import Path
  12. from typing import TYPE_CHECKING, Any, Union
  13. from more_itertools import partition, unique_everseen
  14. from packaging.markers import InvalidMarker, Marker
  15. from packaging.specifiers import InvalidSpecifier, SpecifierSet
  16. from packaging.version import Version
  17. from . import (
  18. _entry_points,
  19. _reqs,
  20. _static,
  21. command as _, # noqa: F401 # imported for side-effects
  22. )
  23. from ._importlib import metadata
  24. from ._normalization import _canonicalize_license_expression
  25. from ._path import StrPath
  26. from ._reqs import _StrOrIter
  27. from .config import pyprojecttoml, setupcfg
  28. from .discovery import ConfigDiscovery
  29. from .errors import InvalidConfigError
  30. from .monkey import get_unpatched
  31. from .warnings import InformationOnly, SetuptoolsDeprecationWarning
  32. import distutils.cmd
  33. import distutils.command
  34. import distutils.core
  35. import distutils.dist
  36. import distutils.log
  37. from distutils.debug import DEBUG
  38. from distutils.errors import DistutilsOptionError, DistutilsSetupError
  39. from distutils.fancy_getopt import translate_longopt
  40. from distutils.util import strtobool
  41. if TYPE_CHECKING:
  42. from typing_extensions import TypeAlias
  43. __all__ = ['Distribution']
  44. _sequence = tuple, list
  45. """
  46. :meta private:
  47. Supported iterable types that are known to be:
  48. - ordered (which `set` isn't)
  49. - not match a str (which `Sequence[str]` does)
  50. - not imply a nested type (like `dict`)
  51. for use with `isinstance`.
  52. """
  53. _Sequence: TypeAlias = Union[tuple[str, ...], list[str]]
  54. # This is how stringifying _Sequence would look in Python 3.10
  55. _sequence_type_repr = "tuple[str, ...] | list[str]"
  56. _OrderedStrSequence: TypeAlias = Union[str, dict[str, Any], Sequence[str]]
  57. """
  58. :meta private:
  59. Avoid single-use iterable. Disallow sets.
  60. A poor approximation of an OrderedSequence (dict doesn't match a Sequence).
  61. """
  62. def __getattr__(name: str) -> Any: # pragma: no cover
  63. if name == "sequence":
  64. SetuptoolsDeprecationWarning.emit(
  65. "`setuptools.dist.sequence` is an internal implementation detail.",
  66. "Please define your own `sequence = tuple, list` instead.",
  67. due_date=(2025, 8, 28), # Originally added on 2024-08-27
  68. )
  69. return _sequence
  70. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
  71. def check_importable(dist, attr, value):
  72. try:
  73. ep = metadata.EntryPoint(value=value, name=None, group=None)
  74. assert not ep.extras
  75. except (TypeError, ValueError, AttributeError, AssertionError) as e:
  76. raise DistutilsSetupError(
  77. f"{attr!r} must be importable 'module:attrs' string (got {value!r})"
  78. ) from e
  79. def assert_string_list(dist, attr: str, value: _Sequence) -> None:
  80. """Verify that value is a string list"""
  81. try:
  82. # verify that value is a list or tuple to exclude unordered
  83. # or single-use iterables
  84. assert isinstance(value, _sequence)
  85. # verify that elements of value are strings
  86. assert ''.join(value) != value
  87. except (TypeError, ValueError, AttributeError, AssertionError) as e:
  88. raise DistutilsSetupError(
  89. f"{attr!r} must be of type <{_sequence_type_repr}> (got {value!r})"
  90. ) from e
  91. def check_nsp(dist, attr, value):
  92. """Verify that namespace packages are valid"""
  93. ns_packages = value
  94. assert_string_list(dist, attr, ns_packages)
  95. for nsp in ns_packages:
  96. if not dist.has_contents_for(nsp):
  97. raise DistutilsSetupError(
  98. f"Distribution contains no modules or packages for namespace package {nsp!r}"
  99. )
  100. parent, _sep, _child = nsp.rpartition('.')
  101. if parent and parent not in ns_packages:
  102. distutils.log.warn(
  103. "WARNING: %r is declared as a package namespace, but %r"
  104. " is not: please correct this in setup.py",
  105. nsp,
  106. parent,
  107. )
  108. SetuptoolsDeprecationWarning.emit(
  109. "The namespace_packages parameter is deprecated.",
  110. "Please replace its usage with implicit namespaces (PEP 420).",
  111. see_docs="references/keywords.html#keyword-namespace-packages",
  112. # TODO: define due_date, it may break old packages that are no longer
  113. # maintained (e.g. sphinxcontrib extensions) when installed from source.
  114. # Warning officially introduced in May 2022, however the deprecation
  115. # was mentioned much earlier in the docs (May 2020, see #2149).
  116. )
  117. def check_extras(dist, attr, value):
  118. """Verify that extras_require mapping is valid"""
  119. try:
  120. list(itertools.starmap(_check_extra, value.items()))
  121. except (TypeError, ValueError, AttributeError) as e:
  122. raise DistutilsSetupError(
  123. "'extras_require' must be a dictionary whose values are "
  124. "strings or lists of strings containing valid project/version "
  125. "requirement specifiers."
  126. ) from e
  127. def _check_extra(extra, reqs):
  128. _name, _sep, marker = extra.partition(':')
  129. try:
  130. _check_marker(marker)
  131. except InvalidMarker:
  132. msg = f"Invalid environment marker: {marker} ({extra!r})"
  133. raise DistutilsSetupError(msg) from None
  134. list(_reqs.parse(reqs))
  135. def _check_marker(marker):
  136. if not marker:
  137. return
  138. m = Marker(marker)
  139. m.evaluate()
  140. def assert_bool(dist, attr, value):
  141. """Verify that value is True, False, 0, or 1"""
  142. if bool(value) != value:
  143. raise DistutilsSetupError(f"{attr!r} must be a boolean value (got {value!r})")
  144. def invalid_unless_false(dist, attr, value):
  145. if not value:
  146. DistDeprecationWarning.emit(f"{attr} is ignored.")
  147. # TODO: should there be a `due_date` here?
  148. return
  149. raise DistutilsSetupError(f"{attr} is invalid.")
  150. def check_requirements(dist, attr: str, value: _OrderedStrSequence) -> None:
  151. """Verify that install_requires is a valid requirements list"""
  152. try:
  153. list(_reqs.parse(value))
  154. if isinstance(value, set):
  155. raise TypeError("Unordered types are not allowed")
  156. except (TypeError, ValueError) as error:
  157. msg = (
  158. f"{attr!r} must be a string or iterable of strings "
  159. f"containing valid project/version requirement specifiers; {error}"
  160. )
  161. raise DistutilsSetupError(msg) from error
  162. def check_specifier(dist, attr, value):
  163. """Verify that value is a valid version specifier"""
  164. try:
  165. SpecifierSet(value)
  166. except (InvalidSpecifier, AttributeError) as error:
  167. msg = f"{attr!r} must be a string containing valid version specifiers; {error}"
  168. raise DistutilsSetupError(msg) from error
  169. def check_entry_points(dist, attr, value):
  170. """Verify that entry_points map is parseable"""
  171. try:
  172. _entry_points.load(value)
  173. except Exception as e:
  174. raise DistutilsSetupError(e) from e
  175. def check_package_data(dist, attr, value):
  176. """Verify that value is a dictionary of package names to glob lists"""
  177. if not isinstance(value, dict):
  178. raise DistutilsSetupError(
  179. f"{attr!r} must be a dictionary mapping package names to lists of "
  180. "string wildcard patterns"
  181. )
  182. for k, v in value.items():
  183. if not isinstance(k, str):
  184. raise DistutilsSetupError(
  185. f"keys of {attr!r} dict must be strings (got {k!r})"
  186. )
  187. assert_string_list(dist, f'values of {attr!r} dict', v)
  188. def check_packages(dist, attr, value):
  189. for pkgname in value:
  190. if not re.match(r'\w+(\.\w+)*', pkgname):
  191. distutils.log.warn(
  192. "WARNING: %r not a valid package name; please use only "
  193. ".-separated package names in setup.py",
  194. pkgname,
  195. )
  196. if TYPE_CHECKING:
  197. # Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/10962
  198. from distutils.core import Distribution as _Distribution
  199. else:
  200. _Distribution = get_unpatched(distutils.core.Distribution)
  201. class Distribution(_Distribution):
  202. """Distribution with support for tests and package data
  203. This is an enhanced version of 'distutils.dist.Distribution' that
  204. effectively adds the following new optional keyword arguments to 'setup()':
  205. 'install_requires' -- a string or sequence of strings specifying project
  206. versions that the distribution requires when installed, in the format
  207. used by 'pkg_resources.require()'. They will be installed
  208. automatically when the package is installed. If you wish to use
  209. packages that are not available in PyPI, or want to give your users an
  210. alternate download location, you can add a 'find_links' option to the
  211. '[easy_install]' section of your project's 'setup.cfg' file, and then
  212. setuptools will scan the listed web pages for links that satisfy the
  213. requirements.
  214. 'extras_require' -- a dictionary mapping names of optional "extras" to the
  215. additional requirement(s) that using those extras incurs. For example,
  216. this::
  217. extras_require = dict(reST = ["docutils>=0.3", "reSTedit"])
  218. indicates that the distribution can optionally provide an extra
  219. capability called "reST", but it can only be used if docutils and
  220. reSTedit are installed. If the user installs your package using
  221. EasyInstall and requests one of your extras, the corresponding
  222. additional requirements will be installed if needed.
  223. 'package_data' -- a dictionary mapping package names to lists of filenames
  224. or globs to use to find data files contained in the named packages.
  225. If the dictionary has filenames or globs listed under '""' (the empty
  226. string), those names will be searched for in every package, in addition
  227. to any names for the specific package. Data files found using these
  228. names/globs will be installed along with the package, in the same
  229. location as the package. Note that globs are allowed to reference
  230. the contents of non-package subdirectories, as long as you use '/' as
  231. a path separator. (Globs are automatically converted to
  232. platform-specific paths at runtime.)
  233. In addition to these new keywords, this class also has several new methods
  234. for manipulating the distribution's contents. For example, the 'include()'
  235. and 'exclude()' methods can be thought of as in-place add and subtract
  236. commands that add or remove packages, modules, extensions, and so on from
  237. the distribution.
  238. """
  239. _DISTUTILS_UNSUPPORTED_METADATA = {
  240. 'long_description_content_type': lambda: None,
  241. 'project_urls': dict,
  242. 'provides_extras': dict, # behaves like an ordered set
  243. 'license_expression': lambda: None,
  244. 'license_file': lambda: None,
  245. 'license_files': lambda: None,
  246. 'install_requires': list,
  247. 'extras_require': dict,
  248. }
  249. # Used by build_py, editable_wheel and install_lib commands for legacy namespaces
  250. namespace_packages: list[str] #: :meta private: DEPRECATED
  251. # Any: Dynamic assignment results in Incompatible types in assignment
  252. def __init__(self, attrs: MutableMapping[str, Any] | None = None) -> None:
  253. have_package_data = hasattr(self, "package_data")
  254. if not have_package_data:
  255. self.package_data: dict[str, list[str]] = {}
  256. attrs = attrs or {}
  257. self.dist_files: list[tuple[str, str, str]] = []
  258. self.include_package_data: bool | None = None
  259. self.exclude_package_data: dict[str, list[str]] | None = None
  260. # Filter-out setuptools' specific options.
  261. self.src_root: str | None = attrs.pop("src_root", None)
  262. self.dependency_links: list[str] = attrs.pop('dependency_links', [])
  263. self.setup_requires: list[str] = attrs.pop('setup_requires', [])
  264. for ep in metadata.entry_points(group='distutils.setup_keywords'):
  265. vars(self).setdefault(ep.name, None)
  266. metadata_only = set(self._DISTUTILS_UNSUPPORTED_METADATA)
  267. metadata_only -= {"install_requires", "extras_require"}
  268. dist_attrs = {k: v for k, v in attrs.items() if k not in metadata_only}
  269. _Distribution.__init__(self, dist_attrs)
  270. # Private API (setuptools-use only, not restricted to Distribution)
  271. # Stores files that are referenced by the configuration and need to be in the
  272. # sdist (e.g. `version = file: VERSION.txt`)
  273. self._referenced_files = set[str]()
  274. self.set_defaults = ConfigDiscovery(self)
  275. self._set_metadata_defaults(attrs)
  276. self.metadata.version = self._normalize_version(self.metadata.version)
  277. self._finalize_requires()
  278. def _validate_metadata(self):
  279. required = {"name"}
  280. provided = {
  281. key
  282. for key in vars(self.metadata)
  283. if getattr(self.metadata, key, None) is not None
  284. }
  285. missing = required - provided
  286. if missing:
  287. msg = f"Required package metadata is missing: {missing}"
  288. raise DistutilsSetupError(msg)
  289. def _set_metadata_defaults(self, attrs):
  290. """
  291. Fill-in missing metadata fields not supported by distutils.
  292. Some fields may have been set by other tools (e.g. pbr).
  293. Those fields (vars(self.metadata)) take precedence to
  294. supplied attrs.
  295. """
  296. for option, default in self._DISTUTILS_UNSUPPORTED_METADATA.items():
  297. vars(self.metadata).setdefault(option, attrs.get(option, default()))
  298. @staticmethod
  299. def _normalize_version(version):
  300. from . import sic
  301. if isinstance(version, numbers.Number):
  302. # Some people apparently take "version number" too literally :)
  303. version = str(version)
  304. elif isinstance(version, sic) or version is None:
  305. return version
  306. normalized = str(Version(version))
  307. if version != normalized:
  308. InformationOnly.emit(f"Normalizing '{version}' to '{normalized}'")
  309. return normalized
  310. return version
  311. def _finalize_requires(self):
  312. """
  313. Set `metadata.python_requires` and fix environment markers
  314. in `install_requires` and `extras_require`.
  315. """
  316. if getattr(self, 'python_requires', None):
  317. self.metadata.python_requires = self.python_requires
  318. self._normalize_requires()
  319. self.metadata.install_requires = self.install_requires
  320. self.metadata.extras_require = self.extras_require
  321. if self.extras_require:
  322. for extra in self.extras_require.keys():
  323. # Setuptools allows a weird "<name>:<env markers> syntax for extras
  324. extra = extra.split(':')[0]
  325. if extra:
  326. self.metadata.provides_extras.setdefault(extra)
  327. def _normalize_requires(self):
  328. """Make sure requirement-related attributes exist and are normalized"""
  329. install_requires = getattr(self, "install_requires", None) or []
  330. extras_require = getattr(self, "extras_require", None) or {}
  331. # Preserve the "static"-ness of values parsed from config files
  332. list_ = _static.List if _static.is_static(install_requires) else list
  333. self.install_requires = list_(map(str, _reqs.parse(install_requires)))
  334. dict_ = _static.Dict if _static.is_static(extras_require) else dict
  335. self.extras_require = dict_(
  336. (k, list(map(str, _reqs.parse(v or [])))) for k, v in extras_require.items()
  337. )
  338. def _finalize_license_expression(self) -> None:
  339. """
  340. Normalize license and license_expression.
  341. >>> dist = Distribution({"license_expression": _static.Str("mit aNd gpl-3.0-OR-later")})
  342. >>> _static.is_static(dist.metadata.license_expression)
  343. True
  344. >>> dist._finalize_license_expression()
  345. >>> _static.is_static(dist.metadata.license_expression) # preserve "static-ness"
  346. True
  347. >>> print(dist.metadata.license_expression)
  348. MIT AND GPL-3.0-or-later
  349. """
  350. classifiers = self.metadata.get_classifiers()
  351. license_classifiers = [cl for cl in classifiers if cl.startswith("License :: ")]
  352. license_expr = self.metadata.license_expression
  353. if license_expr:
  354. str_ = _static.Str if _static.is_static(license_expr) else str
  355. normalized = str_(_canonicalize_license_expression(license_expr))
  356. if license_expr != normalized:
  357. InformationOnly.emit(f"Normalizing '{license_expr}' to '{normalized}'")
  358. self.metadata.license_expression = normalized
  359. if license_classifiers:
  360. raise InvalidConfigError(
  361. "License classifiers have been superseded by license expressions "
  362. "(see https://peps.python.org/pep-0639/). Please remove:\n\n"
  363. + "\n".join(license_classifiers),
  364. )
  365. elif license_classifiers:
  366. pypa_guides = "guides/writing-pyproject-toml/#license"
  367. SetuptoolsDeprecationWarning.emit(
  368. "License classifiers are deprecated.",
  369. "Please consider removing the following classifiers in favor of a "
  370. "SPDX license expression:\n\n" + "\n".join(license_classifiers),
  371. see_url=f"https://packaging.python.org/en/latest/{pypa_guides}",
  372. # Warning introduced on 2025-02-17
  373. # TODO: Should we add a due date? It may affect old/unmaintained
  374. # packages in the ecosystem and cause problems...
  375. )
  376. def _finalize_license_files(self) -> None:
  377. """Compute names of all license files which should be included."""
  378. license_files: list[str] | None = self.metadata.license_files
  379. patterns = license_files or []
  380. license_file: str | None = self.metadata.license_file
  381. if license_file and license_file not in patterns:
  382. patterns.append(license_file)
  383. if license_files is None and license_file is None:
  384. # Default patterns match the ones wheel uses
  385. # See https://wheel.readthedocs.io/en/stable/user_guide.html
  386. # -> 'Including license files in the generated wheel file'
  387. patterns = ['LICEN[CS]E*', 'COPYING*', 'NOTICE*', 'AUTHORS*']
  388. files = self._expand_patterns(patterns, enforce_match=False)
  389. else: # Patterns explicitly given by the user
  390. files = self._expand_patterns(patterns, enforce_match=True)
  391. self.metadata.license_files = list(unique_everseen(files))
  392. @classmethod
  393. def _expand_patterns(
  394. cls, patterns: list[str], enforce_match: bool = True
  395. ) -> Iterator[str]:
  396. """
  397. >>> getfixture('sample_project_cwd')
  398. >>> list(Distribution._expand_patterns(['LICENSE.txt']))
  399. ['LICENSE.txt']
  400. >>> list(Distribution._expand_patterns(['pyproject.toml', 'LIC*']))
  401. ['pyproject.toml', 'LICENSE.txt']
  402. >>> list(Distribution._expand_patterns(['src/**/*.dat']))
  403. ['src/sample/package_data.dat']
  404. """
  405. return (
  406. path.replace(os.sep, "/")
  407. for pattern in patterns
  408. for path in sorted(cls._find_pattern(pattern, enforce_match))
  409. if not path.endswith('~') and os.path.isfile(path)
  410. )
  411. @staticmethod
  412. def _find_pattern(pattern: str, enforce_match: bool = True) -> list[str]:
  413. r"""
  414. >>> getfixture('sample_project_cwd')
  415. >>> Distribution._find_pattern("LICENSE.txt")
  416. ['LICENSE.txt']
  417. >>> Distribution._find_pattern("/LICENSE.MIT")
  418. Traceback (most recent call last):
  419. ...
  420. setuptools.errors.InvalidConfigError: Pattern '/LICENSE.MIT' should be relative...
  421. >>> Distribution._find_pattern("../LICENSE.MIT")
  422. Traceback (most recent call last):
  423. ...
  424. setuptools.warnings.SetuptoolsDeprecationWarning: ...Pattern '../LICENSE.MIT' cannot contain '..'...
  425. >>> Distribution._find_pattern("LICEN{CSE*")
  426. Traceback (most recent call last):
  427. ...
  428. setuptools.warnings.SetuptoolsDeprecationWarning: ...Pattern 'LICEN{CSE*' contains invalid characters...
  429. """
  430. pypa_guides = "specifications/glob-patterns/"
  431. if ".." in pattern:
  432. SetuptoolsDeprecationWarning.emit(
  433. f"Pattern {pattern!r} cannot contain '..'",
  434. """
  435. Please ensure the files specified are contained by the root
  436. of the Python package (normally marked by `pyproject.toml`).
  437. """,
  438. see_url=f"https://packaging.python.org/en/latest/{pypa_guides}",
  439. due_date=(2027, 2, 18), # Introduced in 2025-03-20
  440. # Replace with InvalidConfigError after deprecation
  441. )
  442. if pattern.startswith((os.sep, "/")) or ":\\" in pattern:
  443. raise InvalidConfigError(
  444. f"Pattern {pattern!r} should be relative and must not start with '/'"
  445. )
  446. if re.match(r'^[\w\-\.\/\*\?\[\]]+$', pattern) is None:
  447. SetuptoolsDeprecationWarning.emit(
  448. "Please provide a valid glob pattern.",
  449. "Pattern {pattern!r} contains invalid characters.",
  450. pattern=pattern,
  451. see_url=f"https://packaging.python.org/en/latest/{pypa_guides}",
  452. due_date=(2027, 2, 18), # Introduced in 2025-02-20
  453. )
  454. found = glob(pattern, recursive=True)
  455. if enforce_match and not found:
  456. SetuptoolsDeprecationWarning.emit(
  457. "Cannot find any files for the given pattern.",
  458. "Pattern {pattern!r} did not match any files.",
  459. pattern=pattern,
  460. due_date=(2027, 2, 18), # Introduced in 2025-02-20
  461. # PEP 639 requires us to error, but as a transition period
  462. # we will only issue a warning to give people time to prepare.
  463. # After the transition, this should raise an InvalidConfigError.
  464. )
  465. return found
  466. # FIXME: 'Distribution._parse_config_files' is too complex (14)
  467. def _parse_config_files(self, filenames=None): # noqa: C901
  468. """
  469. Adapted from distutils.dist.Distribution.parse_config_files,
  470. this method provides the same functionality in subtly-improved
  471. ways.
  472. """
  473. from configparser import ConfigParser
  474. # Ignore install directory options if we have a venv
  475. ignore_options = (
  476. []
  477. if sys.prefix == sys.base_prefix
  478. else [
  479. 'install-base',
  480. 'install-platbase',
  481. 'install-lib',
  482. 'install-platlib',
  483. 'install-purelib',
  484. 'install-headers',
  485. 'install-scripts',
  486. 'install-data',
  487. 'prefix',
  488. 'exec-prefix',
  489. 'home',
  490. 'user',
  491. 'root',
  492. ]
  493. )
  494. ignore_options = frozenset(ignore_options)
  495. if filenames is None:
  496. filenames = self.find_config_files()
  497. if DEBUG:
  498. self.announce("Distribution.parse_config_files():")
  499. parser = ConfigParser()
  500. parser.optionxform = str
  501. for filename in filenames:
  502. with open(filename, encoding='utf-8') as reader:
  503. if DEBUG:
  504. self.announce(" reading {filename}".format(**locals()))
  505. parser.read_file(reader)
  506. for section in parser.sections():
  507. options = parser.options(section)
  508. opt_dict = self.get_option_dict(section)
  509. for opt in options:
  510. if opt == '__name__' or opt in ignore_options:
  511. continue
  512. val = parser.get(section, opt)
  513. opt = self._enforce_underscore(opt, section)
  514. opt = self._enforce_option_lowercase(opt, section)
  515. opt_dict[opt] = (filename, val)
  516. # Make the ConfigParser forget everything (so we retain
  517. # the original filenames that options come from)
  518. parser.__init__()
  519. if 'global' not in self.command_options:
  520. return
  521. # If there was a "global" section in the config file, use it
  522. # to set Distribution options.
  523. for opt, (src, val) in self.command_options['global'].items():
  524. alias = self.negative_opt.get(opt)
  525. if alias:
  526. val = not strtobool(val)
  527. elif opt == 'verbose':
  528. val = strtobool(val)
  529. try:
  530. setattr(self, alias or opt, val)
  531. except ValueError as e:
  532. raise DistutilsOptionError(e) from e
  533. def _enforce_underscore(self, opt: str, section: str) -> str:
  534. if "-" not in opt or self._skip_setupcfg_normalization(section):
  535. return opt
  536. underscore_opt = opt.replace('-', '_')
  537. affected = f"(Affected: {self.metadata.name})." if self.metadata.name else ""
  538. SetuptoolsDeprecationWarning.emit(
  539. f"Invalid dash-separated key {opt!r} in {section!r} (setup.cfg), "
  540. f"please use the underscore name {underscore_opt!r} instead.",
  541. f"""
  542. Usage of dash-separated {opt!r} will not be supported in future
  543. versions. Please use the underscore name {underscore_opt!r} instead.
  544. {affected}
  545. Available configuration options are listed in:
  546. https://setuptools.pypa.io/en/latest/userguide/declarative_config.html
  547. """,
  548. see_url="https://github.com/pypa/setuptools/discussions/5011",
  549. due_date=(2026, 3, 3),
  550. # Warning initially introduced in 3 Mar 2021
  551. )
  552. return underscore_opt
  553. def _enforce_option_lowercase(self, opt: str, section: str) -> str:
  554. if opt.islower() or self._skip_setupcfg_normalization(section):
  555. return opt
  556. lowercase_opt = opt.lower()
  557. affected = f"(Affected: {self.metadata.name})." if self.metadata.name else ""
  558. SetuptoolsDeprecationWarning.emit(
  559. f"Invalid uppercase key {opt!r} in {section!r} (setup.cfg), "
  560. f"please use lowercase {lowercase_opt!r} instead.",
  561. f"""
  562. Usage of uppercase key {opt!r} in {section!r} will not be supported in
  563. future versions. Please use lowercase {lowercase_opt!r} instead.
  564. {affected}
  565. Available configuration options are listed in:
  566. https://setuptools.pypa.io/en/latest/userguide/declarative_config.html
  567. """,
  568. see_url="https://github.com/pypa/setuptools/discussions/5011",
  569. due_date=(2026, 3, 3),
  570. # Warning initially introduced in 6 Mar 2021
  571. )
  572. return lowercase_opt
  573. def _skip_setupcfg_normalization(self, section: str) -> bool:
  574. skip = (
  575. 'options.extras_require',
  576. 'options.data_files',
  577. 'options.entry_points',
  578. 'options.package_data',
  579. 'options.exclude_package_data',
  580. )
  581. return section in skip or not self._is_setuptools_section(section)
  582. def _is_setuptools_section(self, section: str) -> bool:
  583. return (
  584. section == "metadata"
  585. or section.startswith("options")
  586. or section in _setuptools_commands()
  587. )
  588. # FIXME: 'Distribution._set_command_options' is too complex (14)
  589. def _set_command_options(self, command_obj, option_dict=None): # noqa: C901
  590. """
  591. Set the options for 'command_obj' from 'option_dict'. Basically
  592. this means copying elements of a dictionary ('option_dict') to
  593. attributes of an instance ('command').
  594. 'command_obj' must be a Command instance. If 'option_dict' is not
  595. supplied, uses the standard option dictionary for this command
  596. (from 'self.command_options').
  597. (Adopted from distutils.dist.Distribution._set_command_options)
  598. """
  599. command_name = command_obj.get_command_name()
  600. if option_dict is None:
  601. option_dict = self.get_option_dict(command_name)
  602. if DEBUG:
  603. self.announce(f" setting options for '{command_name}' command:")
  604. for option, (source, value) in option_dict.items():
  605. if DEBUG:
  606. self.announce(f" {option} = {value} (from {source})")
  607. try:
  608. bool_opts = [translate_longopt(o) for o in command_obj.boolean_options]
  609. except AttributeError:
  610. bool_opts = []
  611. try:
  612. neg_opt = command_obj.negative_opt
  613. except AttributeError:
  614. neg_opt = {}
  615. try:
  616. is_string = isinstance(value, str)
  617. if option in neg_opt and is_string:
  618. setattr(command_obj, neg_opt[option], not strtobool(value))
  619. elif option in bool_opts and is_string:
  620. setattr(command_obj, option, strtobool(value))
  621. elif hasattr(command_obj, option):
  622. setattr(command_obj, option, value)
  623. else:
  624. raise DistutilsOptionError(
  625. f"error in {source}: command '{command_name}' has no such option '{option}'"
  626. )
  627. except ValueError as e:
  628. raise DistutilsOptionError(e) from e
  629. def _get_project_config_files(self, filenames: Iterable[StrPath] | None):
  630. """Add default file and split between INI and TOML"""
  631. tomlfiles = []
  632. standard_project_metadata = Path(self.src_root or os.curdir, "pyproject.toml")
  633. if filenames is not None:
  634. parts = partition(lambda f: Path(f).suffix == ".toml", filenames)
  635. filenames = list(parts[0]) # 1st element => predicate is False
  636. tomlfiles = list(parts[1]) # 2nd element => predicate is True
  637. elif standard_project_metadata.exists():
  638. tomlfiles = [standard_project_metadata]
  639. return filenames, tomlfiles
  640. def parse_config_files(
  641. self,
  642. filenames: Iterable[StrPath] | None = None,
  643. ignore_option_errors: bool = False,
  644. ) -> None:
  645. """Parses configuration files from various levels
  646. and loads configuration.
  647. """
  648. inifiles, tomlfiles = self._get_project_config_files(filenames)
  649. self._parse_config_files(filenames=inifiles)
  650. setupcfg.parse_configuration(
  651. self, self.command_options, ignore_option_errors=ignore_option_errors
  652. )
  653. for filename in tomlfiles:
  654. pyprojecttoml.apply_configuration(self, filename, ignore_option_errors)
  655. self._finalize_requires()
  656. self._finalize_license_expression()
  657. self._finalize_license_files()
  658. def fetch_build_eggs(self, requires: _StrOrIter) -> list[metadata.Distribution]:
  659. """Resolve pre-setup requirements"""
  660. from .installer import _fetch_build_eggs
  661. return _fetch_build_eggs(self, requires)
  662. def finalize_options(self) -> None:
  663. """
  664. Allow plugins to apply arbitrary operations to the
  665. distribution. Each hook may optionally define a 'order'
  666. to influence the order of execution. Smaller numbers
  667. go first and the default is 0.
  668. """
  669. group = 'setuptools.finalize_distribution_options'
  670. def by_order(hook):
  671. return getattr(hook, 'order', 0)
  672. defined = metadata.entry_points(group=group)
  673. filtered = itertools.filterfalse(self._removed, defined)
  674. loaded = map(lambda e: e.load(), filtered)
  675. for ep in sorted(loaded, key=by_order):
  676. ep(self)
  677. @staticmethod
  678. def _removed(ep):
  679. """
  680. When removing an entry point, if metadata is loaded
  681. from an older version of Setuptools, that removed
  682. entry point will attempt to be loaded and will fail.
  683. See #2765 for more details.
  684. """
  685. removed = {
  686. # removed 2021-09-05
  687. '2to3_doctests',
  688. }
  689. return ep.name in removed
  690. def _finalize_setup_keywords(self):
  691. for ep in metadata.entry_points(group='distutils.setup_keywords'):
  692. value = getattr(self, ep.name, None)
  693. if value is not None:
  694. ep.load()(self, ep.name, value)
  695. def get_egg_cache_dir(self) -> str:
  696. from . import windows_support
  697. egg_cache_dir = os.path.join(os.curdir, '.eggs')
  698. if not os.path.exists(egg_cache_dir):
  699. os.mkdir(egg_cache_dir)
  700. windows_support.hide_file(egg_cache_dir)
  701. readme_txt_filename = os.path.join(egg_cache_dir, 'README.txt')
  702. with open(readme_txt_filename, 'w', encoding="utf-8") as f:
  703. f.write(
  704. 'This directory contains eggs that were downloaded '
  705. 'by setuptools to build, test, and run plug-ins.\n\n'
  706. )
  707. f.write(
  708. 'This directory caches those eggs to prevent '
  709. 'repeated downloads.\n\n'
  710. )
  711. f.write('However, it is safe to delete this directory.\n\n')
  712. return egg_cache_dir
  713. def fetch_build_egg(self, req):
  714. """Fetch an egg needed for building"""
  715. from .installer import fetch_build_egg
  716. return fetch_build_egg(self, req)
  717. def get_command_class(self, command: str) -> type[distutils.cmd.Command]: # type: ignore[override] # Not doing complex overrides yet
  718. """Pluggable version of get_command_class()"""
  719. if command in self.cmdclass:
  720. return self.cmdclass[command]
  721. # Special case bdist_wheel so it's never loaded from "wheel"
  722. if command == 'bdist_wheel':
  723. from .command.bdist_wheel import bdist_wheel
  724. return bdist_wheel
  725. eps = metadata.entry_points(group='distutils.commands', name=command)
  726. for ep in eps:
  727. self.cmdclass[command] = cmdclass = ep.load()
  728. return cmdclass
  729. else:
  730. return _Distribution.get_command_class(self, command)
  731. def print_commands(self):
  732. for ep in metadata.entry_points(group='distutils.commands'):
  733. if ep.name not in self.cmdclass:
  734. cmdclass = ep.load()
  735. self.cmdclass[ep.name] = cmdclass
  736. return _Distribution.print_commands(self)
  737. def get_command_list(self):
  738. for ep in metadata.entry_points(group='distutils.commands'):
  739. if ep.name not in self.cmdclass:
  740. cmdclass = ep.load()
  741. self.cmdclass[ep.name] = cmdclass
  742. return _Distribution.get_command_list(self)
  743. def include(self, **attrs) -> None:
  744. """Add items to distribution that are named in keyword arguments
  745. For example, 'dist.include(py_modules=["x"])' would add 'x' to
  746. the distribution's 'py_modules' attribute, if it was not already
  747. there.
  748. Currently, this method only supports inclusion for attributes that are
  749. lists or tuples. If you need to add support for adding to other
  750. attributes in this or a subclass, you can add an '_include_X' method,
  751. where 'X' is the name of the attribute. The method will be called with
  752. the value passed to 'include()'. So, 'dist.include(foo={"bar":"baz"})'
  753. will try to call 'dist._include_foo({"bar":"baz"})', which can then
  754. handle whatever special inclusion logic is needed.
  755. """
  756. for k, v in attrs.items():
  757. include = getattr(self, '_include_' + k, None)
  758. if include:
  759. include(v)
  760. else:
  761. self._include_misc(k, v)
  762. def exclude_package(self, package: str) -> None:
  763. """Remove packages, modules, and extensions in named package"""
  764. pfx = package + '.'
  765. if self.packages:
  766. self.packages = [
  767. p for p in self.packages if p != package and not p.startswith(pfx)
  768. ]
  769. if self.py_modules:
  770. self.py_modules = [
  771. p for p in self.py_modules if p != package and not p.startswith(pfx)
  772. ]
  773. if self.ext_modules:
  774. self.ext_modules = [
  775. p
  776. for p in self.ext_modules
  777. if p.name != package and not p.name.startswith(pfx)
  778. ]
  779. def has_contents_for(self, package: str) -> bool:
  780. """Return true if 'exclude_package(package)' would do something"""
  781. pfx = package + '.'
  782. for p in self.iter_distribution_names():
  783. if p == package or p.startswith(pfx):
  784. return True
  785. return False
  786. def _exclude_misc(self, name: str, value: _Sequence) -> None:
  787. """Handle 'exclude()' for list/tuple attrs without a special handler"""
  788. if not isinstance(value, _sequence):
  789. raise DistutilsSetupError(
  790. f"{name}: setting must be of type <{_sequence_type_repr}> (got {value!r})"
  791. )
  792. try:
  793. old = getattr(self, name)
  794. except AttributeError as e:
  795. raise DistutilsSetupError(f"{name}: No such distribution setting") from e
  796. if old is not None and not isinstance(old, _sequence):
  797. raise DistutilsSetupError(
  798. name + ": this setting cannot be changed via include/exclude"
  799. )
  800. elif old:
  801. setattr(self, name, [item for item in old if item not in value])
  802. def _include_misc(self, name: str, value: _Sequence) -> None:
  803. """Handle 'include()' for list/tuple attrs without a special handler"""
  804. if not isinstance(value, _sequence):
  805. raise DistutilsSetupError(
  806. f"{name}: setting must be of type <{_sequence_type_repr}> (got {value!r})"
  807. )
  808. try:
  809. old = getattr(self, name)
  810. except AttributeError as e:
  811. raise DistutilsSetupError(f"{name}: No such distribution setting") from e
  812. if old is None:
  813. setattr(self, name, value)
  814. elif not isinstance(old, _sequence):
  815. raise DistutilsSetupError(
  816. name + ": this setting cannot be changed via include/exclude"
  817. )
  818. else:
  819. new = [item for item in value if item not in old]
  820. setattr(self, name, list(old) + new)
  821. def exclude(self, **attrs) -> None:
  822. """Remove items from distribution that are named in keyword arguments
  823. For example, 'dist.exclude(py_modules=["x"])' would remove 'x' from
  824. the distribution's 'py_modules' attribute. Excluding packages uses
  825. the 'exclude_package()' method, so all of the package's contained
  826. packages, modules, and extensions are also excluded.
  827. Currently, this method only supports exclusion from attributes that are
  828. lists or tuples. If you need to add support for excluding from other
  829. attributes in this or a subclass, you can add an '_exclude_X' method,
  830. where 'X' is the name of the attribute. The method will be called with
  831. the value passed to 'exclude()'. So, 'dist.exclude(foo={"bar":"baz"})'
  832. will try to call 'dist._exclude_foo({"bar":"baz"})', which can then
  833. handle whatever special exclusion logic is needed.
  834. """
  835. for k, v in attrs.items():
  836. exclude = getattr(self, '_exclude_' + k, None)
  837. if exclude:
  838. exclude(v)
  839. else:
  840. self._exclude_misc(k, v)
  841. def _exclude_packages(self, packages: _Sequence) -> None:
  842. if not isinstance(packages, _sequence):
  843. raise DistutilsSetupError(
  844. f"packages: setting must be of type <{_sequence_type_repr}> (got {packages!r})"
  845. )
  846. list(map(self.exclude_package, packages))
  847. def _parse_command_opts(self, parser, args):
  848. # Remove --with-X/--without-X options when processing command args
  849. self.global_options = self.__class__.global_options
  850. self.negative_opt = self.__class__.negative_opt
  851. # First, expand any aliases
  852. command = args[0]
  853. aliases = self.get_option_dict('aliases')
  854. while command in aliases:
  855. _src, alias = aliases[command]
  856. del aliases[command] # ensure each alias can expand only once!
  857. import shlex
  858. args[:1] = shlex.split(alias, True)
  859. command = args[0]
  860. nargs = _Distribution._parse_command_opts(self, parser, args)
  861. # Handle commands that want to consume all remaining arguments
  862. cmd_class = self.get_command_class(command)
  863. if getattr(cmd_class, 'command_consumes_arguments', None):
  864. self.get_option_dict(command)['args'] = ("command line", nargs)
  865. if nargs is not None:
  866. return []
  867. return nargs
  868. def get_cmdline_options(self) -> dict[str, dict[str, str | None]]:
  869. """Return a '{cmd: {opt:val}}' map of all command-line options
  870. Option names are all long, but do not include the leading '--', and
  871. contain dashes rather than underscores. If the option doesn't take
  872. an argument (e.g. '--quiet'), the 'val' is 'None'.
  873. Note that options provided by config files are intentionally excluded.
  874. """
  875. d: dict[str, dict[str, str | None]] = {}
  876. for cmd, opts in self.command_options.items():
  877. val: str | None
  878. for opt, (src, val) in opts.items():
  879. if src != "command line":
  880. continue
  881. opt = opt.replace('_', '-')
  882. if val == 0:
  883. cmdobj = self.get_command_obj(cmd)
  884. neg_opt = self.negative_opt.copy()
  885. neg_opt.update(getattr(cmdobj, 'negative_opt', {}))
  886. for neg, pos in neg_opt.items():
  887. if pos == opt:
  888. opt = neg
  889. val = None
  890. break
  891. else:
  892. raise AssertionError("Shouldn't be able to get here")
  893. elif val == 1:
  894. val = None
  895. d.setdefault(cmd, {})[opt] = val
  896. return d
  897. def iter_distribution_names(self) -> Iterator[str]:
  898. """Yield all packages, modules, and extension names in distribution"""
  899. yield from self.packages or ()
  900. yield from self.py_modules or ()
  901. for ext in self.ext_modules or ():
  902. if isinstance(ext, tuple):
  903. name, _buildinfo = ext
  904. else:
  905. name = ext.name
  906. name = name.removesuffix('module')
  907. yield name
  908. def handle_display_options(self, option_order):
  909. """If there were any non-global "display-only" options
  910. (--help-commands or the metadata display options) on the command
  911. line, display the requested info and return true; else return
  912. false.
  913. """
  914. import sys
  915. if self.help_commands:
  916. return _Distribution.handle_display_options(self, option_order)
  917. # Stdout may be StringIO (e.g. in tests)
  918. if not isinstance(sys.stdout, io.TextIOWrapper):
  919. return _Distribution.handle_display_options(self, option_order)
  920. # Don't wrap stdout if utf-8 is already the encoding. Provides
  921. # workaround for #334.
  922. if sys.stdout.encoding.lower() in ('utf-8', 'utf8'):
  923. return _Distribution.handle_display_options(self, option_order)
  924. # Print metadata in UTF-8 no matter the platform
  925. encoding = sys.stdout.encoding
  926. sys.stdout.reconfigure(encoding='utf-8')
  927. try:
  928. return _Distribution.handle_display_options(self, option_order)
  929. finally:
  930. sys.stdout.reconfigure(encoding=encoding)
  931. def run_command(self, command) -> None:
  932. self.set_defaults()
  933. # Postpone defaults until all explicit configuration is considered
  934. # (setup() args, config files, command line and plugins)
  935. super().run_command(command)
  936. @functools.cache
  937. def _setuptools_commands() -> set[str]:
  938. try:
  939. # Use older API for importlib.metadata compatibility
  940. entry_points = metadata.distribution('setuptools').entry_points
  941. eps: Iterable[str] = (ep.name for ep in entry_points)
  942. except metadata.PackageNotFoundError:
  943. # during bootstrapping, distribution doesn't exist
  944. eps = []
  945. return {*distutils.command.__all__, *eps}
  946. class DistDeprecationWarning(SetuptoolsDeprecationWarning):
  947. """Class for warning about deprecations in dist in
  948. setuptools. Not ignored by default, unlike DeprecationWarning."""