sdist.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from distutils import log
  2. import distutils.command.sdist as orig
  3. import os
  4. import sys
  5. import io
  6. import contextlib
  7. from itertools import chain
  8. from .._importlib import metadata
  9. from .build import _ORIGINAL_SUBCOMMANDS
  10. _default_revctrl = list
  11. def walk_revctrl(dirname=''):
  12. """Find all files under revision control"""
  13. for ep in metadata.entry_points(group='setuptools.file_finders'):
  14. for item in ep.load()(dirname):
  15. yield item
  16. class sdist(orig.sdist):
  17. """Smart sdist that finds anything supported by revision control"""
  18. user_options = [
  19. ('formats=', None, "formats for source distribution (comma-separated list)"),
  20. (
  21. 'keep-temp',
  22. 'k',
  23. "keep the distribution tree around after creating " + "archive file(s)",
  24. ),
  25. (
  26. 'dist-dir=',
  27. 'd',
  28. "directory to put the source distribution archive(s) in " "[default: dist]",
  29. ),
  30. (
  31. 'owner=',
  32. 'u',
  33. "Owner name used when creating a tar file [default: current user]",
  34. ),
  35. (
  36. 'group=',
  37. 'g',
  38. "Group name used when creating a tar file [default: current group]",
  39. ),
  40. ]
  41. negative_opt = {}
  42. README_EXTENSIONS = ['', '.rst', '.txt', '.md']
  43. READMES = tuple('README{0}'.format(ext) for ext in README_EXTENSIONS)
  44. def run(self):
  45. self.run_command('egg_info')
  46. ei_cmd = self.get_finalized_command('egg_info')
  47. self.filelist = ei_cmd.filelist
  48. self.filelist.append(os.path.join(ei_cmd.egg_info, 'SOURCES.txt'))
  49. self.check_readme()
  50. # Run sub commands
  51. for cmd_name in self.get_sub_commands():
  52. self.run_command(cmd_name)
  53. self.make_distribution()
  54. dist_files = getattr(self.distribution, 'dist_files', [])
  55. for file in self.archive_files:
  56. data = ('sdist', '', file)
  57. if data not in dist_files:
  58. dist_files.append(data)
  59. def initialize_options(self):
  60. orig.sdist.initialize_options(self)
  61. self._default_to_gztar()
  62. def _default_to_gztar(self):
  63. # only needed on Python prior to 3.6.
  64. if sys.version_info >= (3, 6, 0, 'beta', 1):
  65. return
  66. self.formats = ['gztar']
  67. def make_distribution(self):
  68. """
  69. Workaround for #516
  70. """
  71. with self._remove_os_link():
  72. orig.sdist.make_distribution(self)
  73. @staticmethod
  74. @contextlib.contextmanager
  75. def _remove_os_link():
  76. """
  77. In a context, remove and restore os.link if it exists
  78. """
  79. class NoValue:
  80. pass
  81. orig_val = getattr(os, 'link', NoValue)
  82. try:
  83. del os.link
  84. except Exception:
  85. pass
  86. try:
  87. yield
  88. finally:
  89. if orig_val is not NoValue:
  90. setattr(os, 'link', orig_val)
  91. def add_defaults(self):
  92. super().add_defaults()
  93. self._add_defaults_build_sub_commands()
  94. def _add_defaults_optional(self):
  95. super()._add_defaults_optional()
  96. if os.path.isfile('pyproject.toml'):
  97. self.filelist.append('pyproject.toml')
  98. def _add_defaults_python(self):
  99. """getting python files"""
  100. if self.distribution.has_pure_modules():
  101. build_py = self.get_finalized_command('build_py')
  102. self.filelist.extend(build_py.get_source_files())
  103. self._add_data_files(self._safe_data_files(build_py))
  104. def _add_defaults_build_sub_commands(self):
  105. build = self.get_finalized_command("build")
  106. missing_cmds = set(build.get_sub_commands()) - _ORIGINAL_SUBCOMMANDS
  107. # ^-- the original built-in sub-commands are already handled by default.
  108. cmds = (self.get_finalized_command(c) for c in missing_cmds)
  109. files = (c.get_source_files() for c in cmds if hasattr(c, "get_source_files"))
  110. self.filelist.extend(chain.from_iterable(files))
  111. def _safe_data_files(self, build_py):
  112. """
  113. Since the ``sdist`` class is also used to compute the MANIFEST
  114. (via :obj:`setuptools.command.egg_info.manifest_maker`),
  115. there might be recursion problems when trying to obtain the list of
  116. data_files and ``include_package_data=True`` (which in turn depends on
  117. the files included in the MANIFEST).
  118. To avoid that, ``manifest_maker`` should be able to overwrite this
  119. method and avoid recursive attempts to build/analyze the MANIFEST.
  120. """
  121. return build_py.data_files
  122. def _add_data_files(self, data_files):
  123. """
  124. Add data files as found in build_py.data_files.
  125. """
  126. self.filelist.extend(
  127. os.path.join(src_dir, name)
  128. for _, src_dir, _, filenames in data_files
  129. for name in filenames
  130. )
  131. def _add_defaults_data_files(self):
  132. try:
  133. super()._add_defaults_data_files()
  134. except TypeError:
  135. log.warn("data_files contains unexpected objects")
  136. def check_readme(self):
  137. for f in self.READMES:
  138. if os.path.exists(f):
  139. return
  140. else:
  141. self.warn(
  142. "standard file not found: should have one of " + ', '.join(self.READMES)
  143. )
  144. def make_release_tree(self, base_dir, files):
  145. orig.sdist.make_release_tree(self, base_dir, files)
  146. # Save any egg_info command line options used to create this sdist
  147. dest = os.path.join(base_dir, 'setup.cfg')
  148. if hasattr(os, 'link') and os.path.exists(dest):
  149. # unlink and re-copy, since it might be hard-linked, and
  150. # we don't want to change the source version
  151. os.unlink(dest)
  152. self.copy_file('setup.cfg', dest)
  153. self.get_finalized_command('egg_info').save_version_info(dest)
  154. def _manifest_is_not_generated(self):
  155. # check for special comment used in 2.7.1 and higher
  156. if not os.path.isfile(self.manifest):
  157. return False
  158. with io.open(self.manifest, 'rb') as fp:
  159. first_line = fp.readline()
  160. return first_line != '# file GENERATED by distutils, do NOT edit\n'.encode()
  161. def read_manifest(self):
  162. """Read the manifest file (named by 'self.manifest') and use it to
  163. fill in 'self.filelist', the list of files to include in the source
  164. distribution.
  165. """
  166. log.info("reading manifest file '%s'", self.manifest)
  167. manifest = open(self.manifest, 'rb')
  168. for line in manifest:
  169. # The manifest must contain UTF-8. See #303.
  170. try:
  171. line = line.decode('UTF-8')
  172. except UnicodeDecodeError:
  173. log.warn("%r not UTF-8 decodable -- skipping" % line)
  174. continue
  175. # ignore comments and blank lines
  176. line = line.strip()
  177. if line.startswith('#') or not line:
  178. continue
  179. self.filelist.append(line)
  180. manifest.close()