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.

368 lines
14 KiB

4 years ago
  1. """
  2. Create a wheel (.whl) distribution.
  3. A wheel is a built archive format.
  4. """
  5. import os
  6. import shutil
  7. import sys
  8. import re
  9. from email.generator import Generator
  10. from distutils.core import Command
  11. from distutils.sysconfig import get_python_version
  12. from distutils import log as logger
  13. from glob import iglob
  14. from shutil import rmtree
  15. from warnings import warn
  16. import pkg_resources
  17. from .pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag, get_platform
  18. from .pkginfo import write_pkg_info
  19. from .metadata import pkginfo_to_metadata
  20. from .wheelfile import WheelFile
  21. from . import pep425tags
  22. from . import __version__ as wheel_version
  23. safe_name = pkg_resources.safe_name
  24. safe_version = pkg_resources.safe_version
  25. PY_LIMITED_API_PATTERN = r'cp3\d'
  26. def safer_name(name):
  27. return safe_name(name).replace('-', '_')
  28. def safer_version(version):
  29. return safe_version(version).replace('-', '_')
  30. class bdist_wheel(Command):
  31. description = 'create a wheel distribution'
  32. user_options = [('bdist-dir=', 'b',
  33. "temporary directory for creating the distribution"),
  34. ('plat-name=', 'p',
  35. "platform name to embed in generated filenames "
  36. "(default: %s)" % get_platform()),
  37. ('keep-temp', 'k',
  38. "keep the pseudo-installation tree around after " +
  39. "creating the distribution archive"),
  40. ('dist-dir=', 'd',
  41. "directory to put final built distributions in"),
  42. ('skip-build', None,
  43. "skip rebuilding everything (for testing/debugging)"),
  44. ('relative', None,
  45. "build the archive using relative paths"
  46. "(default: false)"),
  47. ('owner=', 'u',
  48. "Owner name used when creating a tar file"
  49. " [default: current user]"),
  50. ('group=', 'g',
  51. "Group name used when creating a tar file"
  52. " [default: current group]"),
  53. ('universal', None,
  54. "make a universal wheel"
  55. " (default: false)"),
  56. ('python-tag=', None,
  57. "Python implementation compatibility tag"
  58. " (default: py%s)" % get_impl_ver()[0]),
  59. ('build-number=', None,
  60. "Build number for this particular version. "
  61. "As specified in PEP-0427, this must start with a digit. "
  62. "[default: None]"),
  63. ('py-limited-api=', None,
  64. "Python tag (cp32|cp33|cpNN) for abi3 wheel tag"
  65. " (default: false)"),
  66. ]
  67. boolean_options = ['keep-temp', 'skip-build', 'relative', 'universal']
  68. def initialize_options(self):
  69. self.bdist_dir = None
  70. self.data_dir = None
  71. self.plat_name = None
  72. self.plat_tag = None
  73. self.format = 'zip'
  74. self.keep_temp = False
  75. self.dist_dir = None
  76. self.egginfo_dir = None
  77. self.root_is_pure = None
  78. self.skip_build = None
  79. self.relative = False
  80. self.owner = None
  81. self.group = None
  82. self.universal = False
  83. self.python_tag = 'py' + get_impl_ver()[0]
  84. self.build_number = None
  85. self.py_limited_api = False
  86. self.plat_name_supplied = False
  87. def finalize_options(self):
  88. if self.bdist_dir is None:
  89. bdist_base = self.get_finalized_command('bdist').bdist_base
  90. self.bdist_dir = os.path.join(bdist_base, 'wheel')
  91. self.data_dir = self.wheel_dist_name + '.data'
  92. self.plat_name_supplied = self.plat_name is not None
  93. need_options = ('dist_dir', 'plat_name', 'skip_build')
  94. self.set_undefined_options('bdist',
  95. *zip(need_options, need_options))
  96. self.root_is_pure = not (self.distribution.has_ext_modules()
  97. or self.distribution.has_c_libraries())
  98. if self.py_limited_api and not re.match(PY_LIMITED_API_PATTERN, self.py_limited_api):
  99. raise ValueError("py-limited-api must match '%s'" % PY_LIMITED_API_PATTERN)
  100. # Support legacy [wheel] section for setting universal
  101. wheel = self.distribution.get_option_dict('wheel')
  102. if 'universal' in wheel:
  103. # please don't define this in your global configs
  104. logger.warn('The [wheel] section is deprecated. Use [bdist_wheel] instead.')
  105. val = wheel['universal'][1].strip()
  106. if val.lower() in ('1', 'true', 'yes'):
  107. self.universal = True
  108. if self.build_number is not None and not self.build_number[:1].isdigit():
  109. raise ValueError("Build tag (build-number) must start with a digit.")
  110. @property
  111. def wheel_dist_name(self):
  112. """Return distribution full name with - replaced with _"""
  113. components = (safer_name(self.distribution.get_name()),
  114. safer_version(self.distribution.get_version()))
  115. if self.build_number:
  116. components += (self.build_number,)
  117. return '-'.join(components)
  118. def get_tag(self):
  119. # bdist sets self.plat_name if unset, we should only use it for purepy
  120. # wheels if the user supplied it.
  121. if self.plat_name_supplied:
  122. plat_name = self.plat_name
  123. elif self.root_is_pure:
  124. plat_name = 'any'
  125. else:
  126. plat_name = self.plat_name or get_platform()
  127. if plat_name in ('linux-x86_64', 'linux_x86_64') and sys.maxsize == 2147483647:
  128. plat_name = 'linux_i686'
  129. plat_name = plat_name.replace('-', '_').replace('.', '_')
  130. if self.root_is_pure:
  131. if self.universal:
  132. impl = 'py2.py3'
  133. else:
  134. impl = self.python_tag
  135. tag = (impl, 'none', plat_name)
  136. else:
  137. impl_name = get_abbr_impl()
  138. impl_ver = get_impl_ver()
  139. impl = impl_name + impl_ver
  140. # We don't work on CPython 3.1, 3.0.
  141. if self.py_limited_api and (impl_name + impl_ver).startswith('cp3'):
  142. impl = self.py_limited_api
  143. abi_tag = 'abi3'
  144. else:
  145. abi_tag = str(get_abi_tag()).lower()
  146. tag = (impl, abi_tag, plat_name)
  147. supported_tags = pep425tags.get_supported(
  148. supplied_platform=plat_name if self.plat_name_supplied else None)
  149. # XXX switch to this alternate implementation for non-pure:
  150. if not self.py_limited_api:
  151. assert tag == supported_tags[0], "%s != %s" % (tag, supported_tags[0])
  152. assert tag in supported_tags, "would build wheel with unsupported tag {}".format(tag)
  153. return tag
  154. def run(self):
  155. build_scripts = self.reinitialize_command('build_scripts')
  156. build_scripts.executable = 'python'
  157. if not self.skip_build:
  158. self.run_command('build')
  159. install = self.reinitialize_command('install',
  160. reinit_subcommands=True)
  161. install.root = self.bdist_dir
  162. install.compile = False
  163. install.skip_build = self.skip_build
  164. install.warn_dir = False
  165. # A wheel without setuptools scripts is more cross-platform.
  166. # Use the (undocumented) `no_ep` option to setuptools'
  167. # install_scripts command to avoid creating entry point scripts.
  168. install_scripts = self.reinitialize_command('install_scripts')
  169. install_scripts.no_ep = True
  170. # Use a custom scheme for the archive, because we have to decide
  171. # at installation time which scheme to use.
  172. for key in ('headers', 'scripts', 'data', 'purelib', 'platlib'):
  173. setattr(install,
  174. 'install_' + key,
  175. os.path.join(self.data_dir, key))
  176. basedir_observed = ''
  177. if os.name == 'nt':
  178. # win32 barfs if any of these are ''; could be '.'?
  179. # (distutils.command.install:change_roots bug)
  180. basedir_observed = os.path.normpath(os.path.join(self.data_dir, '..'))
  181. self.install_libbase = self.install_lib = basedir_observed
  182. setattr(install,
  183. 'install_purelib' if self.root_is_pure else 'install_platlib',
  184. basedir_observed)
  185. logger.info("installing to %s", self.bdist_dir)
  186. self.run_command('install')
  187. impl_tag, abi_tag, plat_tag = self.get_tag()
  188. archive_basename = "{}-{}-{}-{}".format(self.wheel_dist_name, impl_tag, abi_tag, plat_tag)
  189. if not self.relative:
  190. archive_root = self.bdist_dir
  191. else:
  192. archive_root = os.path.join(
  193. self.bdist_dir,
  194. self._ensure_relative(install.install_base))
  195. self.set_undefined_options('install_egg_info', ('target', 'egginfo_dir'))
  196. distinfo_dirname = '{}-{}.dist-info'.format(
  197. safer_name(self.distribution.get_name()),
  198. safer_version(self.distribution.get_version()))
  199. distinfo_dir = os.path.join(self.bdist_dir, distinfo_dirname)
  200. self.egg2dist(self.egginfo_dir, distinfo_dir)
  201. self.write_wheelfile(distinfo_dir)
  202. # Make the archive
  203. if not os.path.exists(self.dist_dir):
  204. os.makedirs(self.dist_dir)
  205. wheel_path = os.path.join(self.dist_dir, archive_basename + '.whl')
  206. with WheelFile(wheel_path, 'w') as wf:
  207. wf.write_files(archive_root)
  208. # Add to 'Distribution.dist_files' so that the "upload" command works
  209. getattr(self.distribution, 'dist_files', []).append(
  210. ('bdist_wheel', get_python_version(), wheel_path))
  211. if not self.keep_temp:
  212. logger.info('removing %s', self.bdist_dir)
  213. if not self.dry_run:
  214. rmtree(self.bdist_dir)
  215. def write_wheelfile(self, wheelfile_base, generator='bdist_wheel (' + wheel_version + ')'):
  216. from email.message import Message
  217. msg = Message()
  218. msg['Wheel-Version'] = '1.0' # of the spec
  219. msg['Generator'] = generator
  220. msg['Root-Is-Purelib'] = str(self.root_is_pure).lower()
  221. if self.build_number is not None:
  222. msg['Build'] = self.build_number
  223. # Doesn't work for bdist_wininst
  224. impl_tag, abi_tag, plat_tag = self.get_tag()
  225. for impl in impl_tag.split('.'):
  226. for abi in abi_tag.split('.'):
  227. for plat in plat_tag.split('.'):
  228. msg['Tag'] = '-'.join((impl, abi, plat))
  229. wheelfile_path = os.path.join(wheelfile_base, 'WHEEL')
  230. logger.info('creating %s', wheelfile_path)
  231. with open(wheelfile_path, 'w') as f:
  232. Generator(f, maxheaderlen=0).flatten(msg)
  233. def _ensure_relative(self, path):
  234. # copied from dir_util, deleted
  235. drive, path = os.path.splitdrive(path)
  236. if path[0:1] == os.sep:
  237. path = drive + path[1:]
  238. return path
  239. @property
  240. def license_paths(self):
  241. metadata = self.distribution.get_option_dict('metadata')
  242. files = set()
  243. patterns = sorted({
  244. option for option in metadata.get('license_files', ('', ''))[1].split()
  245. })
  246. if 'license_file' in metadata:
  247. warn('The "license_file" option is deprecated. Use "license_files" instead.',
  248. DeprecationWarning)
  249. files.add(metadata['license_file'][1])
  250. if 'license_file' not in metadata and 'license_files' not in metadata:
  251. patterns = ('LICEN[CS]E*', 'COPYING*', 'NOTICE*', 'AUTHORS*')
  252. for pattern in patterns:
  253. for path in iglob(pattern):
  254. if path not in files and os.path.isfile(path):
  255. logger.info('adding license file "%s" (matched pattern "%s")', path, pattern)
  256. files.add(path)
  257. return files
  258. def egg2dist(self, egginfo_path, distinfo_path):
  259. """Convert an .egg-info directory into a .dist-info directory"""
  260. def adios(p):
  261. """Appropriately delete directory, file or link."""
  262. if os.path.exists(p) and not os.path.islink(p) and os.path.isdir(p):
  263. shutil.rmtree(p)
  264. elif os.path.exists(p):
  265. os.unlink(p)
  266. adios(distinfo_path)
  267. if not os.path.exists(egginfo_path):
  268. # There is no egg-info. This is probably because the egg-info
  269. # file/directory is not named matching the distribution name used
  270. # to name the archive file. Check for this case and report
  271. # accordingly.
  272. import glob
  273. pat = os.path.join(os.path.dirname(egginfo_path), '*.egg-info')
  274. possible = glob.glob(pat)
  275. err = "Egg metadata expected at %s but not found" % (egginfo_path,)
  276. if possible:
  277. alt = os.path.basename(possible[0])
  278. err += " (%s found - possible misnamed archive file?)" % (alt,)
  279. raise ValueError(err)
  280. if os.path.isfile(egginfo_path):
  281. # .egg-info is a single file
  282. pkginfo_path = egginfo_path
  283. pkg_info = pkginfo_to_metadata(egginfo_path, egginfo_path)
  284. os.mkdir(distinfo_path)
  285. else:
  286. # .egg-info is a directory
  287. pkginfo_path = os.path.join(egginfo_path, 'PKG-INFO')
  288. pkg_info = pkginfo_to_metadata(egginfo_path, pkginfo_path)
  289. # ignore common egg metadata that is useless to wheel
  290. shutil.copytree(egginfo_path, distinfo_path,
  291. ignore=lambda x, y: {'PKG-INFO', 'requires.txt', 'SOURCES.txt',
  292. 'not-zip-safe'}
  293. )
  294. # delete dependency_links if it is only whitespace
  295. dependency_links_path = os.path.join(distinfo_path, 'dependency_links.txt')
  296. with open(dependency_links_path, 'r') as dependency_links_file:
  297. dependency_links = dependency_links_file.read().strip()
  298. if not dependency_links:
  299. adios(dependency_links_path)
  300. write_pkg_info(os.path.join(distinfo_path, 'METADATA'), pkg_info)
  301. for license_path in self.license_paths:
  302. filename = os.path.basename(license_path)
  303. shutil.copy(license_path, os.path.join(distinfo_path, filename))
  304. adios(egginfo_path)