sdist.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from glob import glob
  2. from distutils import log
  3. import distutils.command.sdist as orig
  4. import os
  5. import sys
  6. import io
  7. from setuptools.extern import six
  8. from setuptools.utils import cs_path_exists
  9. import pkg_resources
  10. READMES = 'README', 'README.rst', 'README.txt'
  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(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. 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 __read_template_hack(self):
  51. # This grody hack closes the template file (MANIFEST.in) if an
  52. # exception occurs during read_template.
  53. # Doing so prevents an error when easy_install attempts to delete the
  54. # file.
  55. try:
  56. orig.sdist.read_template(self)
  57. except:
  58. _, _, tb = sys.exc_info()
  59. tb.tb_next.tb_frame.f_locals['template'].close()
  60. raise
  61. # Beginning with Python 2.7.2, 3.1.4, and 3.2.1, this leaky file handle
  62. # has been fixed, so only override the method if we're using an earlier
  63. # Python.
  64. has_leaky_handle = (
  65. sys.version_info < (2, 7, 2)
  66. or (3, 0) <= sys.version_info < (3, 1, 4)
  67. or (3, 2) <= sys.version_info < (3, 2, 1)
  68. )
  69. if has_leaky_handle:
  70. read_template = __read_template_hack
  71. def add_defaults(self):
  72. standards = [READMES,
  73. self.distribution.script_name]
  74. for fn in standards:
  75. if isinstance(fn, tuple):
  76. alts = fn
  77. got_it = 0
  78. for fn in alts:
  79. if cs_path_exists(fn):
  80. got_it = 1
  81. self.filelist.append(fn)
  82. break
  83. if not got_it:
  84. self.warn("standard file not found: should have one of " +
  85. ', '.join(alts))
  86. else:
  87. if cs_path_exists(fn):
  88. self.filelist.append(fn)
  89. else:
  90. self.warn("standard file '%s' not found" % fn)
  91. optional = ['test/test*.py', 'setup.cfg']
  92. for pattern in optional:
  93. files = list(filter(cs_path_exists, glob(pattern)))
  94. if files:
  95. self.filelist.extend(files)
  96. # getting python files
  97. if self.distribution.has_pure_modules():
  98. build_py = self.get_finalized_command('build_py')
  99. self.filelist.extend(build_py.get_source_files())
  100. # This functionality is incompatible with include_package_data, and
  101. # will in fact create an infinite recursion if include_package_data
  102. # is True. Use of include_package_data will imply that
  103. # distutils-style automatic handling of package_data is disabled
  104. if not self.distribution.include_package_data:
  105. for _, src_dir, _, filenames in build_py.data_files:
  106. self.filelist.extend([os.path.join(src_dir, filename)
  107. for filename in filenames])
  108. if self.distribution.has_ext_modules():
  109. build_ext = self.get_finalized_command('build_ext')
  110. self.filelist.extend(build_ext.get_source_files())
  111. if self.distribution.has_c_libraries():
  112. build_clib = self.get_finalized_command('build_clib')
  113. self.filelist.extend(build_clib.get_source_files())
  114. if self.distribution.has_scripts():
  115. build_scripts = self.get_finalized_command('build_scripts')
  116. self.filelist.extend(build_scripts.get_source_files())
  117. def check_readme(self):
  118. for f in READMES:
  119. if os.path.exists(f):
  120. return
  121. else:
  122. self.warn(
  123. "standard file not found: should have one of " +
  124. ', '.join(READMES)
  125. )
  126. def make_release_tree(self, base_dir, files):
  127. orig.sdist.make_release_tree(self, base_dir, files)
  128. # Save any egg_info command line options used to create this sdist
  129. dest = os.path.join(base_dir, 'setup.cfg')
  130. if hasattr(os, 'link') and os.path.exists(dest):
  131. # unlink and re-copy, since it might be hard-linked, and
  132. # we don't want to change the source version
  133. os.unlink(dest)
  134. self.copy_file('setup.cfg', dest)
  135. self.get_finalized_command('egg_info').save_version_info(dest)
  136. def _manifest_is_not_generated(self):
  137. # check for special comment used in 2.7.1 and higher
  138. if not os.path.isfile(self.manifest):
  139. return False
  140. with io.open(self.manifest, 'rb') as fp:
  141. first_line = fp.readline()
  142. return (first_line !=
  143. '# file GENERATED by distutils, do NOT edit\n'.encode())
  144. def read_manifest(self):
  145. """Read the manifest file (named by 'self.manifest') and use it to
  146. fill in 'self.filelist', the list of files to include in the source
  147. distribution.
  148. """
  149. log.info("reading manifest file '%s'", self.manifest)
  150. manifest = open(self.manifest, 'rb')
  151. for line in manifest:
  152. # The manifest must contain UTF-8. See #303.
  153. if six.PY3:
  154. try:
  155. line = line.decode('UTF-8')
  156. except UnicodeDecodeError:
  157. log.warn("%r not UTF-8 decodable -- skipping" % line)
  158. continue
  159. # ignore comments and blank lines
  160. line = line.strip()
  161. if line.startswith('#') or not line:
  162. continue
  163. self.filelist.append(line)
  164. manifest.close()