sdist.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 six, 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 __read_template_hack(self):
  79. # This grody hack closes the template file (MANIFEST.in) if an
  80. # exception occurs during read_template.
  81. # Doing so prevents an error when easy_install attempts to delete the
  82. # file.
  83. try:
  84. orig.sdist.read_template(self)
  85. except Exception:
  86. _, _, tb = sys.exc_info()
  87. tb.tb_next.tb_frame.f_locals['template'].close()
  88. raise
  89. # Beginning with Python 2.7.2, 3.1.4, and 3.2.1, this leaky file handle
  90. # has been fixed, so only override the method if we're using an earlier
  91. # Python.
  92. has_leaky_handle = (
  93. sys.version_info < (2, 7, 2)
  94. or (3, 0) <= sys.version_info < (3, 1, 4)
  95. or (3, 2) <= sys.version_info < (3, 2, 1)
  96. )
  97. if has_leaky_handle:
  98. read_template = __read_template_hack
  99. def _add_defaults_optional(self):
  100. if six.PY2:
  101. sdist_add_defaults._add_defaults_optional(self)
  102. else:
  103. super()._add_defaults_optional()
  104. if os.path.isfile('pyproject.toml'):
  105. self.filelist.append('pyproject.toml')
  106. def _add_defaults_python(self):
  107. """getting python files"""
  108. if self.distribution.has_pure_modules():
  109. build_py = self.get_finalized_command('build_py')
  110. self.filelist.extend(build_py.get_source_files())
  111. self._add_data_files(self._safe_data_files(build_py))
  112. def _safe_data_files(self, build_py):
  113. """
  114. Extracting data_files from build_py is known to cause
  115. infinite recursion errors when `include_package_data`
  116. is enabled, so suppress it in that case.
  117. """
  118. if self.distribution.include_package_data:
  119. return ()
  120. return build_py.data_files
  121. def _add_data_files(self, data_files):
  122. """
  123. Add data files as found in build_py.data_files.
  124. """
  125. self.filelist.extend(
  126. os.path.join(src_dir, name)
  127. for _, src_dir, _, filenames in data_files
  128. for name in filenames
  129. )
  130. def _add_defaults_data_files(self):
  131. try:
  132. if six.PY2:
  133. sdist_add_defaults._add_defaults_data_files(self)
  134. else:
  135. super()._add_defaults_data_files()
  136. except TypeError:
  137. log.warn("data_files contains unexpected objects")
  138. def check_readme(self):
  139. for f in self.READMES:
  140. if os.path.exists(f):
  141. return
  142. else:
  143. self.warn(
  144. "standard file not found: should have one of " +
  145. ', '.join(self.READMES)
  146. )
  147. def make_release_tree(self, base_dir, files):
  148. orig.sdist.make_release_tree(self, base_dir, files)
  149. # Save any egg_info command line options used to create this sdist
  150. dest = os.path.join(base_dir, 'setup.cfg')
  151. if hasattr(os, 'link') and os.path.exists(dest):
  152. # unlink and re-copy, since it might be hard-linked, and
  153. # we don't want to change the source version
  154. os.unlink(dest)
  155. self.copy_file('setup.cfg', dest)
  156. self.get_finalized_command('egg_info').save_version_info(dest)
  157. def _manifest_is_not_generated(self):
  158. # check for special comment used in 2.7.1 and higher
  159. if not os.path.isfile(self.manifest):
  160. return False
  161. with io.open(self.manifest, 'rb') as fp:
  162. first_line = fp.readline()
  163. return (first_line !=
  164. '# file GENERATED by distutils, do NOT edit\n'.encode())
  165. def read_manifest(self):
  166. """Read the manifest file (named by 'self.manifest') and use it to
  167. fill in 'self.filelist', the list of files to include in the source
  168. distribution.
  169. """
  170. log.info("reading manifest file '%s'", self.manifest)
  171. manifest = open(self.manifest, 'rb')
  172. for line in manifest:
  173. # The manifest must contain UTF-8. See #303.
  174. if not six.PY2:
  175. try:
  176. line = line.decode('UTF-8')
  177. except UnicodeDecodeError:
  178. log.warn("%r not UTF-8 decodable -- skipping" % line)
  179. continue
  180. # ignore comments and blank lines
  181. line = line.strip()
  182. if line.startswith('#') or not line:
  183. continue
  184. self.filelist.append(line)
  185. manifest.close()
  186. def check_license(self):
  187. """Checks if license_file' or 'license_files' is configured and adds any
  188. valid paths to 'self.filelist'.
  189. """
  190. files = ordered_set.OrderedSet()
  191. opts = self.distribution.get_option_dict('metadata')
  192. # ignore the source of the value
  193. _, license_file = opts.get('license_file', (None, None))
  194. if license_file is None:
  195. log.debug("'license_file' option was not specified")
  196. else:
  197. files.add(license_file)
  198. try:
  199. files.update(self.distribution.metadata.license_files)
  200. except TypeError:
  201. log.warn("warning: 'license_files' option is malformed")
  202. for f in files:
  203. if not os.path.exists(f):
  204. log.warn(
  205. "warning: Failed to find the configured license file '%s'",
  206. f)
  207. files.remove(f)
  208. self.filelist.extend(files)