pyprojecttoml.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. """
  2. Load setuptools configuration from ``pyproject.toml`` files.
  3. **PRIVATE MODULE**: API reserved for setuptools internal usage only.
  4. """
  5. import logging
  6. import os
  7. import warnings
  8. from contextlib import contextmanager
  9. from functools import partial
  10. from typing import TYPE_CHECKING, Callable, Dict, Optional, Mapping, Union
  11. from setuptools.errors import FileError, OptionError
  12. from . import expand as _expand
  13. from ._apply_pyprojecttoml import apply as _apply
  14. from ._apply_pyprojecttoml import _PREVIOUSLY_DEFINED, _WouldIgnoreField
  15. if TYPE_CHECKING:
  16. from setuptools.dist import Distribution # noqa
  17. _Path = Union[str, os.PathLike]
  18. _logger = logging.getLogger(__name__)
  19. def load_file(filepath: _Path) -> dict:
  20. from setuptools.extern import tomli # type: ignore
  21. with open(filepath, "rb") as file:
  22. return tomli.load(file)
  23. def validate(config: dict, filepath: _Path) -> bool:
  24. from . import _validate_pyproject as validator
  25. trove_classifier = validator.FORMAT_FUNCTIONS.get("trove-classifier")
  26. if hasattr(trove_classifier, "_disable_download"):
  27. # Improve reproducibility by default. See issue 31 for validate-pyproject.
  28. trove_classifier._disable_download() # type: ignore
  29. try:
  30. return validator.validate(config)
  31. except validator.ValidationError as ex:
  32. summary = f"configuration error: {ex.summary}"
  33. if ex.name.strip("`") != "project":
  34. # Probably it is just a field missing/misnamed, not worthy the verbosity...
  35. _logger.debug(summary)
  36. _logger.debug(ex.details)
  37. error = f"invalid pyproject.toml config: {ex.name}."
  38. raise ValueError(f"{error}\n{summary}") from None
  39. def apply_configuration(
  40. dist: "Distribution",
  41. filepath: _Path,
  42. ignore_option_errors=False,
  43. ) -> "Distribution":
  44. """Apply the configuration from a ``pyproject.toml`` file into an existing
  45. distribution object.
  46. """
  47. config = read_configuration(filepath, True, ignore_option_errors, dist)
  48. return _apply(dist, config, filepath)
  49. def read_configuration(
  50. filepath: _Path,
  51. expand=True,
  52. ignore_option_errors=False,
  53. dist: Optional["Distribution"] = None,
  54. ):
  55. """Read given configuration file and returns options from it as a dict.
  56. :param str|unicode filepath: Path to configuration file in the ``pyproject.toml``
  57. format.
  58. :param bool expand: Whether to expand directives and other computed values
  59. (i.e. post-process the given configuration)
  60. :param bool ignore_option_errors: Whether to silently ignore
  61. options, values of which could not be resolved (e.g. due to exceptions
  62. in directives such as file:, attr:, etc.).
  63. If False exceptions are propagated as expected.
  64. :param Distribution|None: Distribution object to which the configuration refers.
  65. If not given a dummy object will be created and discarded after the
  66. configuration is read. This is used for auto-discovery of packages in the case
  67. a dynamic configuration (e.g. ``attr`` or ``cmdclass``) is expanded.
  68. When ``expand=False`` this object is simply ignored.
  69. :rtype: dict
  70. """
  71. filepath = os.path.abspath(filepath)
  72. if not os.path.isfile(filepath):
  73. raise FileError(f"Configuration file {filepath!r} does not exist.")
  74. asdict = load_file(filepath) or {}
  75. project_table = asdict.get("project", {})
  76. tool_table = asdict.get("tool", {})
  77. setuptools_table = tool_table.get("setuptools", {})
  78. if not asdict or not (project_table or setuptools_table):
  79. return {} # User is not using pyproject to configure setuptools
  80. if setuptools_table:
  81. # TODO: Remove the following once the feature stabilizes:
  82. msg = "Support for `[tool.setuptools]` in `pyproject.toml` is still *beta*."
  83. warnings.warn(msg, _BetaConfiguration)
  84. # There is an overall sense in the community that making include_package_data=True
  85. # the default would be an improvement.
  86. # `ini2toml` backfills include_package_data=False when nothing is explicitly given,
  87. # therefore setting a default here is backwards compatible.
  88. orig_setuptools_table = setuptools_table.copy()
  89. if dist and getattr(dist, "include_package_data") is not None:
  90. setuptools_table.setdefault("include-package-data", dist.include_package_data)
  91. else:
  92. setuptools_table.setdefault("include-package-data", True)
  93. # Persist changes:
  94. asdict["tool"] = tool_table
  95. tool_table["setuptools"] = setuptools_table
  96. try:
  97. # Don't complain about unrelated errors (e.g. tools not using the "tool" table)
  98. subset = {"project": project_table, "tool": {"setuptools": setuptools_table}}
  99. validate(subset, filepath)
  100. except Exception as ex:
  101. # TODO: Remove the following once the feature stabilizes:
  102. if _skip_bad_config(project_table, orig_setuptools_table, dist):
  103. return {}
  104. # TODO: After the previous statement is removed the try/except can be replaced
  105. # by the _ignore_errors context manager.
  106. if ignore_option_errors:
  107. _logger.debug(f"ignored error: {ex.__class__.__name__} - {ex}")
  108. else:
  109. raise # re-raise exception
  110. if expand:
  111. root_dir = os.path.dirname(filepath)
  112. return expand_configuration(asdict, root_dir, ignore_option_errors, dist)
  113. return asdict
  114. def _skip_bad_config(
  115. project_cfg: dict, setuptools_cfg: dict, dist: Optional["Distribution"]
  116. ) -> bool:
  117. """Be temporarily forgiving with invalid ``pyproject.toml``"""
  118. # See pypa/setuptools#3199 and pypa/cibuildwheel#1064
  119. if dist is None or (
  120. dist.metadata.name is None
  121. and dist.metadata.version is None
  122. and dist.install_requires is None
  123. ):
  124. # It seems that the build is not getting any configuration from other places
  125. return False
  126. if setuptools_cfg:
  127. # If `[tool.setuptools]` is set, then `pyproject.toml` config is intentional
  128. return False
  129. given_config = set(project_cfg.keys())
  130. popular_subset = {"name", "version", "python_requires", "requires-python"}
  131. if given_config <= popular_subset:
  132. # It seems that the docs in cibuildtool has been inadvertently encouraging users
  133. # to create `pyproject.toml` files that are not compliant with the standards.
  134. # Let's be forgiving for the time being.
  135. warnings.warn(_InvalidFile.message(), _InvalidFile, stacklevel=2)
  136. return True
  137. return False
  138. def expand_configuration(
  139. config: dict,
  140. root_dir: Optional[_Path] = None,
  141. ignore_option_errors: bool = False,
  142. dist: Optional["Distribution"] = None,
  143. ) -> dict:
  144. """Given a configuration with unresolved fields (e.g. dynamic, cmdclass, ...)
  145. find their final values.
  146. :param dict config: Dict containing the configuration for the distribution
  147. :param str root_dir: Top-level directory for the distribution/project
  148. (the same directory where ``pyproject.toml`` is place)
  149. :param bool ignore_option_errors: see :func:`read_configuration`
  150. :param Distribution|None: Distribution object to which the configuration refers.
  151. If not given a dummy object will be created and discarded after the
  152. configuration is read. Used in the case a dynamic configuration
  153. (e.g. ``attr`` or ``cmdclass``).
  154. :rtype: dict
  155. """
  156. return _ConfigExpander(config, root_dir, ignore_option_errors, dist).expand()
  157. class _ConfigExpander:
  158. def __init__(
  159. self,
  160. config: dict,
  161. root_dir: Optional[_Path] = None,
  162. ignore_option_errors: bool = False,
  163. dist: Optional["Distribution"] = None,
  164. ):
  165. self.config = config
  166. self.root_dir = root_dir or os.getcwd()
  167. self.project_cfg = config.get("project", {})
  168. self.dynamic = self.project_cfg.get("dynamic", [])
  169. self.setuptools_cfg = config.get("tool", {}).get("setuptools", {})
  170. self.dynamic_cfg = self.setuptools_cfg.get("dynamic", {})
  171. self.ignore_option_errors = ignore_option_errors
  172. self._dist = dist
  173. def _ensure_dist(self) -> "Distribution":
  174. from setuptools.dist import Distribution
  175. attrs = {"src_root": self.root_dir, "name": self.project_cfg.get("name", None)}
  176. return self._dist or Distribution(attrs)
  177. def _process_field(self, container: dict, field: str, fn: Callable):
  178. if field in container:
  179. with _ignore_errors(self.ignore_option_errors):
  180. container[field] = fn(container[field])
  181. def _canonic_package_data(self, field="package-data"):
  182. package_data = self.setuptools_cfg.get(field, {})
  183. return _expand.canonic_package_data(package_data)
  184. def expand(self):
  185. self._expand_packages()
  186. self._canonic_package_data()
  187. self._canonic_package_data("exclude-package-data")
  188. # A distribution object is required for discovering the correct package_dir
  189. dist = self._ensure_dist()
  190. ctx = _EnsurePackagesDiscovered(dist, self.project_cfg, self.setuptools_cfg)
  191. with ctx as ensure_discovered:
  192. package_dir = ensure_discovered.package_dir
  193. self._expand_data_files()
  194. self._expand_cmdclass(package_dir)
  195. self._expand_all_dynamic(dist, package_dir)
  196. return self.config
  197. def _expand_packages(self):
  198. packages = self.setuptools_cfg.get("packages")
  199. if packages is None or isinstance(packages, (list, tuple)):
  200. return
  201. find = packages.get("find")
  202. if isinstance(find, dict):
  203. find["root_dir"] = self.root_dir
  204. find["fill_package_dir"] = self.setuptools_cfg.setdefault("package-dir", {})
  205. with _ignore_errors(self.ignore_option_errors):
  206. self.setuptools_cfg["packages"] = _expand.find_packages(**find)
  207. def _expand_data_files(self):
  208. data_files = partial(_expand.canonic_data_files, root_dir=self.root_dir)
  209. self._process_field(self.setuptools_cfg, "data-files", data_files)
  210. def _expand_cmdclass(self, package_dir: Mapping[str, str]):
  211. root_dir = self.root_dir
  212. cmdclass = partial(_expand.cmdclass, package_dir=package_dir, root_dir=root_dir)
  213. self._process_field(self.setuptools_cfg, "cmdclass", cmdclass)
  214. def _expand_all_dynamic(self, dist: "Distribution", package_dir: Mapping[str, str]):
  215. special = ( # need special handling
  216. "version",
  217. "readme",
  218. "entry-points",
  219. "scripts",
  220. "gui-scripts",
  221. "classifiers",
  222. "dependencies",
  223. "optional-dependencies",
  224. )
  225. # `_obtain` functions are assumed to raise appropriate exceptions/warnings.
  226. obtained_dynamic = {
  227. field: self._obtain(dist, field, package_dir)
  228. for field in self.dynamic
  229. if field not in special
  230. }
  231. obtained_dynamic.update(
  232. self._obtain_entry_points(dist, package_dir) or {},
  233. version=self._obtain_version(dist, package_dir),
  234. readme=self._obtain_readme(dist),
  235. classifiers=self._obtain_classifiers(dist),
  236. dependencies=self._obtain_dependencies(dist),
  237. optional_dependencies=self._obtain_optional_dependencies(dist),
  238. )
  239. # `None` indicates there is nothing in `tool.setuptools.dynamic` but the value
  240. # might have already been set by setup.py/extensions, so avoid overwriting.
  241. updates = {k: v for k, v in obtained_dynamic.items() if v is not None}
  242. self.project_cfg.update(updates)
  243. def _ensure_previously_set(self, dist: "Distribution", field: str):
  244. previous = _PREVIOUSLY_DEFINED[field](dist)
  245. if previous is None and not self.ignore_option_errors:
  246. msg = (
  247. f"No configuration found for dynamic {field!r}.\n"
  248. "Some dynamic fields need to be specified via `tool.setuptools.dynamic`"
  249. "\nothers must be specified via the equivalent attribute in `setup.py`."
  250. )
  251. raise OptionError(msg)
  252. def _expand_directive(
  253. self, specifier: str, directive, package_dir: Mapping[str, str]
  254. ):
  255. with _ignore_errors(self.ignore_option_errors):
  256. root_dir = self.root_dir
  257. if "file" in directive:
  258. return _expand.read_files(directive["file"], root_dir)
  259. if "attr" in directive:
  260. return _expand.read_attr(directive["attr"], package_dir, root_dir)
  261. raise ValueError(f"invalid `{specifier}`: {directive!r}")
  262. return None
  263. def _obtain(self, dist: "Distribution", field: str, package_dir: Mapping[str, str]):
  264. if field in self.dynamic_cfg:
  265. return self._expand_directive(
  266. f"tool.setuptools.dynamic.{field}",
  267. self.dynamic_cfg[field],
  268. package_dir,
  269. )
  270. self._ensure_previously_set(dist, field)
  271. return None
  272. def _obtain_version(self, dist: "Distribution", package_dir: Mapping[str, str]):
  273. # Since plugins can set version, let's silently skip if it cannot be obtained
  274. if "version" in self.dynamic and "version" in self.dynamic_cfg:
  275. return _expand.version(self._obtain(dist, "version", package_dir))
  276. return None
  277. def _obtain_readme(self, dist: "Distribution") -> Optional[Dict[str, str]]:
  278. if "readme" not in self.dynamic:
  279. return None
  280. dynamic_cfg = self.dynamic_cfg
  281. if "readme" in dynamic_cfg:
  282. return {
  283. "text": self._obtain(dist, "readme", {}),
  284. "content-type": dynamic_cfg["readme"].get("content-type", "text/x-rst"),
  285. }
  286. self._ensure_previously_set(dist, "readme")
  287. return None
  288. def _obtain_entry_points(
  289. self, dist: "Distribution", package_dir: Mapping[str, str]
  290. ) -> Optional[Dict[str, dict]]:
  291. fields = ("entry-points", "scripts", "gui-scripts")
  292. if not any(field in self.dynamic for field in fields):
  293. return None
  294. text = self._obtain(dist, "entry-points", package_dir)
  295. if text is None:
  296. return None
  297. groups = _expand.entry_points(text)
  298. expanded = {"entry-points": groups}
  299. def _set_scripts(field: str, group: str):
  300. if group in groups:
  301. value = groups.pop(group)
  302. if field not in self.dynamic:
  303. msg = _WouldIgnoreField.message(field, value)
  304. warnings.warn(msg, _WouldIgnoreField)
  305. # TODO: Don't set field when support for pyproject.toml stabilizes
  306. # instead raise an error as specified in PEP 621
  307. expanded[field] = value
  308. _set_scripts("scripts", "console_scripts")
  309. _set_scripts("gui-scripts", "gui_scripts")
  310. return expanded
  311. def _obtain_classifiers(self, dist: "Distribution"):
  312. if "classifiers" in self.dynamic:
  313. value = self._obtain(dist, "classifiers", {})
  314. if value:
  315. return value.splitlines()
  316. return None
  317. def _obtain_dependencies(self, dist: "Distribution"):
  318. if "dependencies" in self.dynamic:
  319. value = self._obtain(dist, "dependencies", {})
  320. if value:
  321. return _parse_requirements_list(value)
  322. return None
  323. def _obtain_optional_dependencies(self, dist: "Distribution"):
  324. if "optional-dependencies" not in self.dynamic:
  325. return None
  326. if "optional-dependencies" in self.dynamic_cfg:
  327. optional_dependencies_map = self.dynamic_cfg["optional-dependencies"]
  328. assert isinstance(optional_dependencies_map, dict)
  329. return {
  330. group: _parse_requirements_list(self._expand_directive(
  331. f"tool.setuptools.dynamic.optional-dependencies.{group}",
  332. directive,
  333. {},
  334. ))
  335. for group, directive in optional_dependencies_map.items()
  336. }
  337. self._ensure_previously_set(dist, "optional-dependencies")
  338. return None
  339. def _parse_requirements_list(value):
  340. return [
  341. line
  342. for line in value.splitlines()
  343. if line.strip() and not line.strip().startswith("#")
  344. ]
  345. @contextmanager
  346. def _ignore_errors(ignore_option_errors: bool):
  347. if not ignore_option_errors:
  348. yield
  349. return
  350. try:
  351. yield
  352. except Exception as ex:
  353. _logger.debug(f"ignored error: {ex.__class__.__name__} - {ex}")
  354. class _EnsurePackagesDiscovered(_expand.EnsurePackagesDiscovered):
  355. def __init__(
  356. self, distribution: "Distribution", project_cfg: dict, setuptools_cfg: dict
  357. ):
  358. super().__init__(distribution)
  359. self._project_cfg = project_cfg
  360. self._setuptools_cfg = setuptools_cfg
  361. def __enter__(self):
  362. """When entering the context, the values of ``packages``, ``py_modules`` and
  363. ``package_dir`` that are missing in ``dist`` are copied from ``setuptools_cfg``.
  364. """
  365. dist, cfg = self._dist, self._setuptools_cfg
  366. package_dir: Dict[str, str] = cfg.setdefault("package-dir", {})
  367. package_dir.update(dist.package_dir or {})
  368. dist.package_dir = package_dir # needs to be the same object
  369. dist.set_defaults._ignore_ext_modules() # pyproject.toml-specific behaviour
  370. # Set `name`, `py_modules` and `packages` in dist to short-circuit
  371. # auto-discovery, but avoid overwriting empty lists purposefully set by users.
  372. if dist.metadata.name is None:
  373. dist.metadata.name = self._project_cfg.get("name")
  374. if dist.py_modules is None:
  375. dist.py_modules = cfg.get("py-modules")
  376. if dist.packages is None:
  377. dist.packages = cfg.get("packages")
  378. return super().__enter__()
  379. def __exit__(self, exc_type, exc_value, traceback):
  380. """When exiting the context, if values of ``packages``, ``py_modules`` and
  381. ``package_dir`` are missing in ``setuptools_cfg``, copy from ``dist``.
  382. """
  383. # If anything was discovered set them back, so they count in the final config.
  384. self._setuptools_cfg.setdefault("packages", self._dist.packages)
  385. self._setuptools_cfg.setdefault("py-modules", self._dist.py_modules)
  386. return super().__exit__(exc_type, exc_value, traceback)
  387. class _BetaConfiguration(UserWarning):
  388. """Explicitly inform users that some `pyproject.toml` configuration is *beta*"""
  389. class _InvalidFile(UserWarning):
  390. """The given `pyproject.toml` file is invalid and would be ignored.
  391. !!\n\n
  392. ############################
  393. # Invalid `pyproject.toml` #
  394. ############################
  395. Any configurations in `pyproject.toml` will be ignored.
  396. Please note that future releases of setuptools will halt the build process
  397. if an invalid file is given.
  398. To prevent setuptools from considering `pyproject.toml` please
  399. DO NOT include the `[project]` or `[tool.setuptools]` tables in your file.
  400. \n\n!!
  401. """
  402. @classmethod
  403. def message(cls):
  404. from inspect import cleandoc
  405. return cleandoc(cls.__doc__)