Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

136 wiersze
5.9 KiB

  1. from __future__ import annotations
  2. from typing import Protocol
  3. from ..dist import Distribution
  4. from distutils.command.build import build as _build
  5. _ORIGINAL_SUBCOMMANDS = {"build_py", "build_clib", "build_ext", "build_scripts"}
  6. class build(_build):
  7. distribution: Distribution # override distutils.dist.Distribution with setuptools.dist.Distribution
  8. # copy to avoid sharing the object with parent class
  9. sub_commands = _build.sub_commands[:]
  10. class SubCommand(Protocol):
  11. """In order to support editable installations (see :pep:`660`) all
  12. build subcommands **SHOULD** implement this protocol. They also **MUST** inherit
  13. from ``setuptools.Command``.
  14. When creating an :pep:`editable wheel <660>`, ``setuptools`` will try to evaluate
  15. custom ``build`` subcommands using the following procedure:
  16. 1. ``setuptools`` will set the ``editable_mode`` attribute to ``True``
  17. 2. ``setuptools`` will execute the ``run()`` command.
  18. .. important::
  19. Subcommands **SHOULD** take advantage of ``editable_mode=True`` to adequate
  20. its behaviour or perform optimisations.
  21. For example, if a subcommand doesn't need to generate an extra file and
  22. all it does is to copy a source file into the build directory,
  23. ``run()`` **SHOULD** simply "early return".
  24. Similarly, if the subcommand creates files that would be placed alongside
  25. Python files in the final distribution, during an editable install
  26. the command **SHOULD** generate these files "in place" (i.e. write them to
  27. the original source directory, instead of using the build directory).
  28. Note that ``get_output_mapping()`` should reflect that and include mappings
  29. for "in place" builds accordingly.
  30. 3. ``setuptools`` use any knowledge it can derive from the return values of
  31. ``get_outputs()`` and ``get_output_mapping()`` to create an editable wheel.
  32. When relevant ``setuptools`` **MAY** attempt to use file links based on the value
  33. of ``get_output_mapping()``. Alternatively, ``setuptools`` **MAY** attempt to use
  34. :doc:`import hooks <python:reference/import>` to redirect any attempt to import
  35. to the directory with the original source code and other files built in place.
  36. Please note that custom sub-commands **SHOULD NOT** rely on ``run()`` being
  37. executed (or not) to provide correct return values for ``get_outputs()``,
  38. ``get_output_mapping()`` or ``get_source_files()``. The ``get_*`` methods should
  39. work independently of ``run()``.
  40. """
  41. editable_mode: bool = False
  42. """Boolean flag that will be set to ``True`` when setuptools is used for an
  43. editable installation (see :pep:`660`).
  44. Implementations **SHOULD** explicitly set the default value of this attribute to
  45. ``False``.
  46. When subcommands run, they can use this flag to perform optimizations or change
  47. their behaviour accordingly.
  48. """
  49. build_lib: str
  50. """String representing the directory where the build artifacts should be stored,
  51. e.g. ``build/lib``.
  52. For example, if a distribution wants to provide a Python module named ``pkg.mod``,
  53. then a corresponding file should be written to ``{build_lib}/package/module.py``.
  54. A way of thinking about this is that the files saved under ``build_lib``
  55. would be eventually copied to one of the directories in :obj:`site.PREFIXES`
  56. upon installation.
  57. A command that produces platform-independent files (e.g. compiling text templates
  58. into Python functions), **CAN** initialize ``build_lib`` by copying its value from
  59. the ``build_py`` command. On the other hand, a command that produces
  60. platform-specific files **CAN** initialize ``build_lib`` by copying its value from
  61. the ``build_ext`` command. In general this is done inside the ``finalize_options``
  62. method with the help of the ``set_undefined_options`` command::
  63. def finalize_options(self):
  64. self.set_undefined_options("build_py", ("build_lib", "build_lib"))
  65. ...
  66. """
  67. def initialize_options(self) -> None:
  68. """(Required by the original :class:`setuptools.Command` interface)"""
  69. ...
  70. def finalize_options(self) -> None:
  71. """(Required by the original :class:`setuptools.Command` interface)"""
  72. ...
  73. def run(self) -> None:
  74. """(Required by the original :class:`setuptools.Command` interface)"""
  75. ...
  76. def get_source_files(self) -> list[str]:
  77. """
  78. Return a list of all files that are used by the command to create the expected
  79. outputs.
  80. For example, if your build command transpiles Java files into Python, you should
  81. list here all the Java files.
  82. The primary purpose of this function is to help populating the ``sdist``
  83. with all the files necessary to build the distribution.
  84. All files should be strings relative to the project root directory.
  85. """
  86. ...
  87. def get_outputs(self) -> list[str]:
  88. """
  89. Return a list of files intended for distribution as they would have been
  90. produced by the build.
  91. These files should be strings in the form of
  92. ``"{build_lib}/destination/file/path"``.
  93. .. note::
  94. The return value of ``get_output()`` should include all files used as keys
  95. in ``get_output_mapping()`` plus files that are generated during the build
  96. and don't correspond to any source file already present in the project.
  97. """
  98. ...
  99. def get_output_mapping(self) -> dict[str, str]:
  100. """
  101. Return a mapping between destination files as they would be produced by the
  102. build (dict keys) into the respective existing (source) files (dict values).
  103. Existing (source) files should be represented as strings relative to the project
  104. root directory.
  105. Destination files should be strings in the form of
  106. ``"{build_lib}/destination/file/path"``.
  107. """
  108. ...