bdist_wheel.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. """
  2. Create a wheel (.whl) distribution.
  3. A wheel is a built archive format.
  4. """
  5. import csv
  6. import hashlib
  7. import os
  8. import subprocess
  9. import shutil
  10. import sys
  11. import re
  12. from email.generator import Generator
  13. from distutils.core import Command
  14. from distutils.sysconfig import get_python_version
  15. from distutils import log as logger
  16. from shutil import rmtree
  17. import pkg_resources
  18. from .pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag, get_platform
  19. from .util import native, open_for_csv
  20. from .archive import archive_wheelfile
  21. from .pkginfo import write_pkg_info
  22. from .metadata import pkginfo_to_metadata
  23. from . import pep425tags
  24. from . import __version__ as wheel_version
  25. safe_name = pkg_resources.safe_name
  26. safe_version = pkg_resources.safe_version
  27. PY_LIMITED_API_PATTERN = r'cp3\d'
  28. def safer_name(name):
  29. return safe_name(name).replace('-', '_')
  30. def safer_version(version):
  31. return safe_version(version).replace('-', '_')
  32. class bdist_wheel(Command):
  33. description = 'create a wheel distribution'
  34. user_options = [('bdist-dir=', 'b',
  35. "temporary directory for creating the distribution"),
  36. ('plat-name=', 'p',
  37. "platform name to embed in generated filenames "
  38. "(default: %s)" % get_platform()),
  39. ('keep-temp', 'k',
  40. "keep the pseudo-installation tree around after " +
  41. "creating the distribution archive"),
  42. ('dist-dir=', 'd',
  43. "directory to put final built distributions in"),
  44. ('skip-build', None,
  45. "skip rebuilding everything (for testing/debugging)"),
  46. ('relative', None,
  47. "build the archive using relative paths"
  48. "(default: false)"),
  49. ('owner=', 'u',
  50. "Owner name used when creating a tar file"
  51. " [default: current user]"),
  52. ('group=', 'g',
  53. "Group name used when creating a tar file"
  54. " [default: current group]"),
  55. ('universal', None,
  56. "make a universal wheel"
  57. " (default: false)"),
  58. ('python-tag=', None,
  59. "Python implementation compatibility tag"
  60. " (default: py%s)" % get_impl_ver()[0]),
  61. ('build-number=', None,
  62. "Build number for this particular version. "
  63. "As specified in PEP-0427, this must start with a digit. "
  64. "[default: None]"),
  65. ('py-limited-api=', None,
  66. "Python tag (cp32|cp33|cpNN) for abi3 wheel tag"
  67. " (default: false)"),
  68. ]
  69. boolean_options = ['keep-temp', 'skip-build', 'relative', 'universal']
  70. def initialize_options(self):
  71. self.bdist_dir = None
  72. self.data_dir = None
  73. self.plat_name = None
  74. self.plat_tag = None
  75. self.format = 'zip'
  76. self.keep_temp = False
  77. self.dist_dir = None
  78. self.distinfo_dir = None
  79. self.egginfo_dir = None
  80. self.root_is_pure = None
  81. self.skip_build = None
  82. self.relative = False
  83. self.owner = None
  84. self.group = None
  85. self.universal = False
  86. self.python_tag = 'py' + get_impl_ver()[0]
  87. self.build_number = None
  88. self.py_limited_api = False
  89. self.plat_name_supplied = False
  90. def finalize_options(self):
  91. if self.bdist_dir is None:
  92. bdist_base = self.get_finalized_command('bdist').bdist_base
  93. self.bdist_dir = os.path.join(bdist_base, 'wheel')
  94. self.data_dir = self.wheel_dist_name + '.data'
  95. self.plat_name_supplied = self.plat_name is not None
  96. need_options = ('dist_dir', 'plat_name', 'skip_build')
  97. self.set_undefined_options('bdist',
  98. *zip(need_options, need_options))
  99. self.root_is_pure = not (self.distribution.has_ext_modules()
  100. or self.distribution.has_c_libraries())
  101. if self.py_limited_api and not re.match(PY_LIMITED_API_PATTERN, self.py_limited_api):
  102. raise ValueError("py-limited-api must match '%s'" % PY_LIMITED_API_PATTERN)
  103. # Support legacy [wheel] section for setting universal
  104. wheel = self.distribution.get_option_dict('wheel')
  105. if 'universal' in wheel:
  106. # please don't define this in your global configs
  107. val = wheel['universal'][1].strip()
  108. if val.lower() in ('1', 'true', 'yes'):
  109. self.universal = True
  110. if self.build_number is not None and not self.build_number[:1].isdigit():
  111. raise ValueError("Build tag (build-number) must start with a digit.")
  112. @property
  113. def wheel_dist_name(self):
  114. """Return distribution full name with - replaced with _"""
  115. components = (safer_name(self.distribution.get_name()),
  116. safer_version(self.distribution.get_version()))
  117. if self.build_number:
  118. components += (self.build_number,)
  119. return '-'.join(components)
  120. def get_tag(self):
  121. # bdist sets self.plat_name if unset, we should only use it for purepy
  122. # wheels if the user supplied it.
  123. if self.plat_name_supplied:
  124. plat_name = self.plat_name
  125. elif self.root_is_pure:
  126. plat_name = 'any'
  127. else:
  128. plat_name = self.plat_name or get_platform()
  129. if plat_name in ('linux-x86_64', 'linux_x86_64') and sys.maxsize == 2147483647:
  130. plat_name = 'linux_i686'
  131. plat_name = plat_name.replace('-', '_').replace('.', '_')
  132. if self.root_is_pure:
  133. if self.universal:
  134. impl = 'py2.py3'
  135. else:
  136. impl = self.python_tag
  137. tag = (impl, 'none', plat_name)
  138. else:
  139. impl_name = get_abbr_impl()
  140. impl_ver = get_impl_ver()
  141. impl = impl_name + impl_ver
  142. # We don't work on CPython 3.1, 3.0.
  143. if self.py_limited_api and (impl_name + impl_ver).startswith('cp3'):
  144. impl = self.py_limited_api
  145. abi_tag = 'abi3'
  146. else:
  147. abi_tag = str(get_abi_tag()).lower()
  148. tag = (impl, abi_tag, plat_name)
  149. supported_tags = pep425tags.get_supported(
  150. supplied_platform=plat_name if self.plat_name_supplied else None)
  151. # XXX switch to this alternate implementation for non-pure:
  152. if not self.py_limited_api:
  153. assert tag == supported_tags[0], "%s != %s" % (tag, supported_tags[0])
  154. assert tag in supported_tags, "would build wheel with unsupported tag {}".format(tag)
  155. return tag
  156. def get_archive_basename(self):
  157. """Return archive name without extension"""
  158. impl_tag, abi_tag, plat_tag = self.get_tag()
  159. archive_basename = "%s-%s-%s-%s" % (
  160. self.wheel_dist_name,
  161. impl_tag,
  162. abi_tag,
  163. plat_tag)
  164. return archive_basename
  165. def run(self):
  166. build_scripts = self.reinitialize_command('build_scripts')
  167. build_scripts.executable = 'python'
  168. if not self.skip_build:
  169. self.run_command('build')
  170. install = self.reinitialize_command('install',
  171. reinit_subcommands=True)
  172. install.root = self.bdist_dir
  173. install.compile = False
  174. install.skip_build = self.skip_build
  175. install.warn_dir = False
  176. # A wheel without setuptools scripts is more cross-platform.
  177. # Use the (undocumented) `no_ep` option to setuptools'
  178. # install_scripts command to avoid creating entry point scripts.
  179. install_scripts = self.reinitialize_command('install_scripts')
  180. install_scripts.no_ep = True
  181. # Use a custom scheme for the archive, because we have to decide
  182. # at installation time which scheme to use.
  183. for key in ('headers', 'scripts', 'data', 'purelib', 'platlib'):
  184. setattr(install,
  185. 'install_' + key,
  186. os.path.join(self.data_dir, key))
  187. basedir_observed = ''
  188. if os.name == 'nt':
  189. # win32 barfs if any of these are ''; could be '.'?
  190. # (distutils.command.install:change_roots bug)
  191. basedir_observed = os.path.normpath(os.path.join(self.data_dir, '..'))
  192. self.install_libbase = self.install_lib = basedir_observed
  193. setattr(install,
  194. 'install_purelib' if self.root_is_pure else 'install_platlib',
  195. basedir_observed)
  196. logger.info("installing to %s", self.bdist_dir)
  197. self.run_command('install')
  198. archive_basename = self.get_archive_basename()
  199. pseudoinstall_root = os.path.join(self.dist_dir, archive_basename)
  200. if not self.relative:
  201. archive_root = self.bdist_dir
  202. else:
  203. archive_root = os.path.join(
  204. self.bdist_dir,
  205. self._ensure_relative(install.install_base))
  206. self.set_undefined_options(
  207. 'install_egg_info', ('target', 'egginfo_dir'))
  208. self.distinfo_dir = os.path.join(self.bdist_dir,
  209. '%s.dist-info' % self.wheel_dist_name)
  210. self.egg2dist(self.egginfo_dir,
  211. self.distinfo_dir)
  212. self.write_wheelfile(self.distinfo_dir)
  213. self.write_record(self.bdist_dir, self.distinfo_dir)
  214. # Make the archive
  215. if not os.path.exists(self.dist_dir):
  216. os.makedirs(self.dist_dir)
  217. wheel_name = archive_wheelfile(pseudoinstall_root, archive_root)
  218. # Sign the archive
  219. if 'WHEEL_TOOL' in os.environ:
  220. subprocess.call([os.environ['WHEEL_TOOL'], 'sign', wheel_name])
  221. # Add to 'Distribution.dist_files' so that the "upload" command works
  222. getattr(self.distribution, 'dist_files', []).append(
  223. ('bdist_wheel', get_python_version(), wheel_name))
  224. if not self.keep_temp:
  225. logger.info('removing %s', self.bdist_dir)
  226. if not self.dry_run:
  227. rmtree(self.bdist_dir)
  228. def write_wheelfile(self, wheelfile_base, generator='bdist_wheel (' + wheel_version + ')'):
  229. from email.message import Message
  230. msg = Message()
  231. msg['Wheel-Version'] = '1.0' # of the spec
  232. msg['Generator'] = generator
  233. msg['Root-Is-Purelib'] = str(self.root_is_pure).lower()
  234. if self.build_number is not None:
  235. msg['Build'] = self.build_number
  236. # Doesn't work for bdist_wininst
  237. impl_tag, abi_tag, plat_tag = self.get_tag()
  238. for impl in impl_tag.split('.'):
  239. for abi in abi_tag.split('.'):
  240. for plat in plat_tag.split('.'):
  241. msg['Tag'] = '-'.join((impl, abi, plat))
  242. wheelfile_path = os.path.join(wheelfile_base, 'WHEEL')
  243. logger.info('creating %s', wheelfile_path)
  244. with open(wheelfile_path, 'w') as f:
  245. Generator(f, maxheaderlen=0).flatten(msg)
  246. def _ensure_relative(self, path):
  247. # copied from dir_util, deleted
  248. drive, path = os.path.splitdrive(path)
  249. if path[0:1] == os.sep:
  250. path = drive + path[1:]
  251. return path
  252. def license_file(self):
  253. """Return license filename from a license-file key in setup.cfg, or None."""
  254. metadata = self.distribution.get_option_dict('metadata')
  255. if 'license_file' not in metadata:
  256. return None
  257. return metadata['license_file'][1]
  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. # XXX heuristically copy any LICENSE/LICENSE.txt?
  302. license = self.license_file()
  303. if license:
  304. license_filename = 'LICENSE.txt'
  305. shutil.copy(license, os.path.join(distinfo_path, license_filename))
  306. adios(egginfo_path)
  307. def write_record(self, bdist_dir, distinfo_dir):
  308. from .util import urlsafe_b64encode
  309. record_path = os.path.join(distinfo_dir, 'RECORD')
  310. record_relpath = os.path.relpath(record_path, bdist_dir)
  311. def walk():
  312. for dir, dirs, files in os.walk(bdist_dir):
  313. dirs.sort()
  314. for f in sorted(files):
  315. yield os.path.join(dir, f)
  316. def skip(path):
  317. """Wheel hashes every possible file."""
  318. return (path == record_relpath)
  319. with open_for_csv(record_path, 'w+') as record_file:
  320. writer = csv.writer(record_file)
  321. for path in walk():
  322. relpath = os.path.relpath(path, bdist_dir)
  323. if skip(relpath):
  324. hash = ''
  325. size = ''
  326. else:
  327. with open(path, 'rb') as f:
  328. data = f.read()
  329. digest = hashlib.sha256(data).digest()
  330. hash = 'sha256=' + native(urlsafe_b64encode(digest))
  331. size = len(data)
  332. record_path = os.path.relpath(path, bdist_dir).replace(os.path.sep, '/')
  333. # On Python 2, re-encode the path as UTF-8 from the default file system encoding
  334. if isinstance(record_path, bytes):
  335. record_path = record_path.decode(sys.getfilesystemencoding()).encode('utf-8')
  336. writer.writerow((record_path, hash, size))