You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sdist.py 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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
  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_python(self):
  100. """getting python files"""
  101. if self.distribution.has_pure_modules():
  102. build_py = self.get_finalized_command('build_py')
  103. self.filelist.extend(build_py.get_source_files())
  104. # This functionality is incompatible with include_package_data, and
  105. # will in fact create an infinite recursion if include_package_data
  106. # is True. Use of include_package_data will imply that
  107. # distutils-style automatic handling of package_data is disabled
  108. if not self.distribution.include_package_data:
  109. for _, src_dir, _, filenames in build_py.data_files:
  110. self.filelist.extend([os.path.join(src_dir, filename)
  111. for filename in filenames])
  112. def _add_defaults_data_files(self):
  113. try:
  114. if six.PY2:
  115. sdist_add_defaults._add_defaults_data_files(self)
  116. else:
  117. super()._add_defaults_data_files()
  118. except TypeError:
  119. log.warn("data_files contains unexpected objects")
  120. def check_readme(self):
  121. for f in self.READMES:
  122. if os.path.exists(f):
  123. return
  124. else:
  125. self.warn(
  126. "standard file not found: should have one of " +
  127. ', '.join(self.READMES)
  128. )
  129. def make_release_tree(self, base_dir, files):
  130. orig.sdist.make_release_tree(self, base_dir, files)
  131. # Save any egg_info command line options used to create this sdist
  132. dest = os.path.join(base_dir, 'setup.cfg')
  133. if hasattr(os, 'link') and os.path.exists(dest):
  134. # unlink and re-copy, since it might be hard-linked, and
  135. # we don't want to change the source version
  136. os.unlink(dest)
  137. self.copy_file('setup.cfg', dest)
  138. self.get_finalized_command('egg_info').save_version_info(dest)
  139. def _manifest_is_not_generated(self):
  140. # check for special comment used in 2.7.1 and higher
  141. if not os.path.isfile(self.manifest):
  142. return False
  143. with io.open(self.manifest, 'rb') as fp:
  144. first_line = fp.readline()
  145. return (first_line !=
  146. '# file GENERATED by distutils, do NOT edit\n'.encode())
  147. def read_manifest(self):
  148. """Read the manifest file (named by 'self.manifest') and use it to
  149. fill in 'self.filelist', the list of files to include in the source
  150. distribution.
  151. """
  152. log.info("reading manifest file '%s'", self.manifest)
  153. manifest = open(self.manifest, 'rb')
  154. for line in manifest:
  155. # The manifest must contain UTF-8. See #303.
  156. if six.PY3:
  157. try:
  158. line = line.decode('UTF-8')
  159. except UnicodeDecodeError:
  160. log.warn("%r not UTF-8 decodable -- skipping" % line)
  161. continue
  162. # ignore comments and blank lines
  163. line = line.strip()
  164. if line.startswith('#') or not line:
  165. continue
  166. self.filelist.append(line)
  167. manifest.close()
  168. def check_license(self):
  169. """Checks if license_file' is configured and adds it to
  170. 'self.filelist' if the value contains a valid path.
  171. """
  172. opts = self.distribution.get_option_dict('metadata')
  173. # ignore the source of the value
  174. _, license_file = opts.get('license_file', (None, None))
  175. if license_file is None:
  176. log.debug("'license_file' option was not specified")
  177. return
  178. if not os.path.exists(license_file):
  179. log.warn("warning: Failed to find the configured license file '%s'",
  180. license_file)
  181. return
  182. self.filelist.append(license_file)