sdist.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. 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. READMES = 'README', 'README.rst', 'README.txt'
  30. def run(self):
  31. self.run_command('egg_info')
  32. ei_cmd = self.get_finalized_command('egg_info')
  33. self.filelist = ei_cmd.filelist
  34. self.filelist.append(os.path.join(ei_cmd.egg_info, 'SOURCES.txt'))
  35. self.check_readme()
  36. # Run sub commands
  37. for cmd_name in self.get_sub_commands():
  38. self.run_command(cmd_name)
  39. # Call check_metadata only if no 'check' command
  40. # (distutils <= 2.6)
  41. import distutils.command
  42. if 'check' not in distutils.command.__all__:
  43. self.check_metadata()
  44. self.make_distribution()
  45. dist_files = getattr(self.distribution, 'dist_files', [])
  46. for file in self.archive_files:
  47. data = ('sdist', '', file)
  48. if data not in dist_files:
  49. dist_files.append(data)
  50. def initialize_options(self):
  51. orig.sdist.initialize_options(self)
  52. self._default_to_gztar()
  53. def _default_to_gztar(self):
  54. # only needed on Python prior to 3.6.
  55. if sys.version_info >= (3, 6, 0, 'beta', 1):
  56. return
  57. self.formats = ['gztar']
  58. def make_distribution(self):
  59. """
  60. Workaround for #516
  61. """
  62. with self._remove_os_link():
  63. orig.sdist.make_distribution(self)
  64. @staticmethod
  65. @contextlib.contextmanager
  66. def _remove_os_link():
  67. """
  68. In a context, remove and restore os.link if it exists
  69. """
  70. class NoValue:
  71. pass
  72. orig_val = getattr(os, 'link', NoValue)
  73. try:
  74. del os.link
  75. except Exception:
  76. pass
  77. try:
  78. yield
  79. finally:
  80. if orig_val is not NoValue:
  81. setattr(os, 'link', orig_val)
  82. def __read_template_hack(self):
  83. # This grody hack closes the template file (MANIFEST.in) if an
  84. # exception occurs during read_template.
  85. # Doing so prevents an error when easy_install attempts to delete the
  86. # file.
  87. try:
  88. orig.sdist.read_template(self)
  89. except Exception:
  90. _, _, tb = sys.exc_info()
  91. tb.tb_next.tb_frame.f_locals['template'].close()
  92. raise
  93. # Beginning with Python 2.7.2, 3.1.4, and 3.2.1, this leaky file handle
  94. # has been fixed, so only override the method if we're using an earlier
  95. # Python.
  96. has_leaky_handle = (
  97. sys.version_info < (2, 7, 2)
  98. or (3, 0) <= sys.version_info < (3, 1, 4)
  99. or (3, 2) <= sys.version_info < (3, 2, 1)
  100. )
  101. if has_leaky_handle:
  102. read_template = __read_template_hack
  103. def _add_defaults_python(self):
  104. """getting python files"""
  105. if self.distribution.has_pure_modules():
  106. build_py = self.get_finalized_command('build_py')
  107. self.filelist.extend(build_py.get_source_files())
  108. # This functionality is incompatible with include_package_data, and
  109. # will in fact create an infinite recursion if include_package_data
  110. # is True. Use of include_package_data will imply that
  111. # distutils-style automatic handling of package_data is disabled
  112. if not self.distribution.include_package_data:
  113. for _, src_dir, _, filenames in build_py.data_files:
  114. self.filelist.extend([os.path.join(src_dir, filename)
  115. for filename in filenames])
  116. def _add_defaults_data_files(self):
  117. try:
  118. if six.PY2:
  119. sdist_add_defaults._add_defaults_data_files(self)
  120. else:
  121. super()._add_defaults_data_files()
  122. except TypeError:
  123. log.warn("data_files contains unexpected objects")
  124. def check_readme(self):
  125. for f in self.READMES:
  126. if os.path.exists(f):
  127. return
  128. else:
  129. self.warn(
  130. "standard file not found: should have one of " +
  131. ', '.join(self.READMES)
  132. )
  133. def make_release_tree(self, base_dir, files):
  134. orig.sdist.make_release_tree(self, base_dir, files)
  135. # Save any egg_info command line options used to create this sdist
  136. dest = os.path.join(base_dir, 'setup.cfg')
  137. if hasattr(os, 'link') and os.path.exists(dest):
  138. # unlink and re-copy, since it might be hard-linked, and
  139. # we don't want to change the source version
  140. os.unlink(dest)
  141. self.copy_file('setup.cfg', dest)
  142. self.get_finalized_command('egg_info').save_version_info(dest)
  143. def _manifest_is_not_generated(self):
  144. # check for special comment used in 2.7.1 and higher
  145. if not os.path.isfile(self.manifest):
  146. return False
  147. with io.open(self.manifest, 'rb') as fp:
  148. first_line = fp.readline()
  149. return (first_line !=
  150. '# file GENERATED by distutils, do NOT edit\n'.encode())
  151. def read_manifest(self):
  152. """Read the manifest file (named by 'self.manifest') and use it to
  153. fill in 'self.filelist', the list of files to include in the source
  154. distribution.
  155. """
  156. log.info("reading manifest file '%s'", self.manifest)
  157. manifest = open(self.manifest, 'rb')
  158. for line in manifest:
  159. # The manifest must contain UTF-8. See #303.
  160. if six.PY3:
  161. try:
  162. line = line.decode('UTF-8')
  163. except UnicodeDecodeError:
  164. log.warn("%r not UTF-8 decodable -- skipping" % line)
  165. continue
  166. # ignore comments and blank lines
  167. line = line.strip()
  168. if line.startswith('#') or not line:
  169. continue
  170. self.filelist.append(line)
  171. manifest.close()