sdist.py 6.8 KB

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