sdist.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. import pkg_resources
  9. _default_revctrl = list
  10. def walk_revctrl(dirname=''):
  11. """Find all files under revision control"""
  12. for ep in pkg_resources.iter_entry_points('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. ]
  27. negative_opt = {}
  28. README_EXTENSIONS = ['', '.rst', '.txt', '.md']
  29. READMES = tuple('README{0}'.format(ext) for ext in README_EXTENSIONS)
  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. self.make_distribution()
  40. dist_files = getattr(self.distribution, 'dist_files', [])
  41. for file in self.archive_files:
  42. data = ('sdist', '', file)
  43. if data not in dist_files:
  44. dist_files.append(data)
  45. def initialize_options(self):
  46. orig.sdist.initialize_options(self)
  47. self._default_to_gztar()
  48. def _default_to_gztar(self):
  49. # only needed on Python prior to 3.6.
  50. if sys.version_info >= (3, 6, 0, 'beta', 1):
  51. return
  52. self.formats = ['gztar']
  53. def make_distribution(self):
  54. """
  55. Workaround for #516
  56. """
  57. with self._remove_os_link():
  58. orig.sdist.make_distribution(self)
  59. @staticmethod
  60. @contextlib.contextmanager
  61. def _remove_os_link():
  62. """
  63. In a context, remove and restore os.link if it exists
  64. """
  65. class NoValue:
  66. pass
  67. orig_val = getattr(os, 'link', NoValue)
  68. try:
  69. del os.link
  70. except Exception:
  71. pass
  72. try:
  73. yield
  74. finally:
  75. if orig_val is not NoValue:
  76. setattr(os, 'link', orig_val)
  77. def _add_defaults_optional(self):
  78. super()._add_defaults_optional()
  79. if os.path.isfile('pyproject.toml'):
  80. self.filelist.append('pyproject.toml')
  81. def _add_defaults_python(self):
  82. """getting python files"""
  83. if self.distribution.has_pure_modules():
  84. build_py = self.get_finalized_command('build_py')
  85. self.filelist.extend(build_py.get_source_files())
  86. self._add_data_files(self._safe_data_files(build_py))
  87. def _safe_data_files(self, build_py):
  88. """
  89. Extracting data_files from build_py is known to cause
  90. infinite recursion errors when `include_package_data`
  91. is enabled, so suppress it in that case.
  92. """
  93. if self.distribution.include_package_data:
  94. return ()
  95. return build_py.data_files
  96. def _add_data_files(self, data_files):
  97. """
  98. Add data files as found in build_py.data_files.
  99. """
  100. self.filelist.extend(
  101. os.path.join(src_dir, name)
  102. for _, src_dir, _, filenames in data_files
  103. for name in filenames
  104. )
  105. def _add_defaults_data_files(self):
  106. try:
  107. super()._add_defaults_data_files()
  108. except TypeError:
  109. log.warn("data_files contains unexpected objects")
  110. def check_readme(self):
  111. for f in self.READMES:
  112. if os.path.exists(f):
  113. return
  114. else:
  115. self.warn(
  116. "standard file not found: should have one of " +
  117. ', '.join(self.READMES)
  118. )
  119. def make_release_tree(self, base_dir, files):
  120. orig.sdist.make_release_tree(self, base_dir, files)
  121. # Save any egg_info command line options used to create this sdist
  122. dest = os.path.join(base_dir, 'setup.cfg')
  123. if hasattr(os, 'link') and os.path.exists(dest):
  124. # unlink and re-copy, since it might be hard-linked, and
  125. # we don't want to change the source version
  126. os.unlink(dest)
  127. self.copy_file('setup.cfg', dest)
  128. self.get_finalized_command('egg_info').save_version_info(dest)
  129. def _manifest_is_not_generated(self):
  130. # check for special comment used in 2.7.1 and higher
  131. if not os.path.isfile(self.manifest):
  132. return False
  133. with io.open(self.manifest, 'rb') as fp:
  134. first_line = fp.readline()
  135. return (first_line !=
  136. '# file GENERATED by distutils, do NOT edit\n'.encode())
  137. def read_manifest(self):
  138. """Read the manifest file (named by 'self.manifest') and use it to
  139. fill in 'self.filelist', the list of files to include in the source
  140. distribution.
  141. """
  142. log.info("reading manifest file '%s'", self.manifest)
  143. manifest = open(self.manifest, 'rb')
  144. for line in manifest:
  145. # The manifest must contain UTF-8. See #303.
  146. try:
  147. line = line.decode('UTF-8')
  148. except UnicodeDecodeError:
  149. log.warn("%r not UTF-8 decodable -- skipping" % line)
  150. continue
  151. # ignore comments and blank lines
  152. line = line.strip()
  153. if line.startswith('#') or not line:
  154. continue
  155. self.filelist.append(line)
  156. manifest.close()