sdist.py 7.6 KB

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