build.py 6.4 KB

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