sdist.py 6.3 KB

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