PageRenderTime 71ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/teca-env18/lib/python2.7/site-packages/wheel/bdist_wheel.py

https://github.com/dpritsos/WEGA
Python | 365 lines | 311 code | 36 blank | 18 comment | 34 complexity | 0f133b62c2707b497611a6bb8f72f0a2 MD5 | raw file
  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_dir = os.path.join(self.bdist_dir, '%s.dist-info' % self.wheel_dist_name)
  197. self.egg2dist(self.egginfo_dir, distinfo_dir)
  198. self.write_wheelfile(distinfo_dir)
  199. # Make the archive
  200. if not os.path.exists(self.dist_dir):
  201. os.makedirs(self.dist_dir)
  202. wheel_path = os.path.join(self.dist_dir, archive_basename + '.whl')
  203. with WheelFile(wheel_path, 'w') as wf:
  204. wf.write_files(archive_root)
  205. # Add to 'Distribution.dist_files' so that the "upload" command works
  206. getattr(self.distribution, 'dist_files', []).append(
  207. ('bdist_wheel', get_python_version(), wheel_path))
  208. if not self.keep_temp:
  209. logger.info('removing %s', self.bdist_dir)
  210. if not self.dry_run:
  211. rmtree(self.bdist_dir)
  212. def write_wheelfile(self, wheelfile_base, generator='bdist_wheel (' + wheel_version + ')'):
  213. from email.message import Message
  214. msg = Message()
  215. msg['Wheel-Version'] = '1.0' # of the spec
  216. msg['Generator'] = generator
  217. msg['Root-Is-Purelib'] = str(self.root_is_pure).lower()
  218. if self.build_number is not None:
  219. msg['Build'] = self.build_number
  220. # Doesn't work for bdist_wininst
  221. impl_tag, abi_tag, plat_tag = self.get_tag()
  222. for impl in impl_tag.split('.'):
  223. for abi in abi_tag.split('.'):
  224. for plat in plat_tag.split('.'):
  225. msg['Tag'] = '-'.join((impl, abi, plat))
  226. wheelfile_path = os.path.join(wheelfile_base, 'WHEEL')
  227. logger.info('creating %s', wheelfile_path)
  228. with open(wheelfile_path, 'w') as f:
  229. Generator(f, maxheaderlen=0).flatten(msg)
  230. def _ensure_relative(self, path):
  231. # copied from dir_util, deleted
  232. drive, path = os.path.splitdrive(path)
  233. if path[0:1] == os.sep:
  234. path = drive + path[1:]
  235. return path
  236. @property
  237. def license_paths(self):
  238. metadata = self.distribution.get_option_dict('metadata')
  239. files = set()
  240. patterns = sorted({
  241. option for option in metadata.get('license_files', ('', ''))[1].split()
  242. })
  243. if 'license_file' in metadata:
  244. warn('The "license_file" option is deprecated. Use "license_files" instead.',
  245. DeprecationWarning)
  246. files.add(metadata['license_file'][1])
  247. if 'license_file' not in metadata and 'license_files' not in metadata:
  248. patterns = ('LICEN[CS]E*', 'COPYING*', 'NOTICE*', 'AUTHORS*')
  249. for pattern in patterns:
  250. for path in iglob(pattern):
  251. if path not in files and os.path.isfile(path):
  252. logger.info('adding license file "%s" (matched pattern "%s")', path, pattern)
  253. files.add(path)
  254. return files
  255. def egg2dist(self, egginfo_path, distinfo_path):
  256. """Convert an .egg-info directory into a .dist-info directory"""
  257. def adios(p):
  258. """Appropriately delete directory, file or link."""
  259. if os.path.exists(p) and not os.path.islink(p) and os.path.isdir(p):
  260. shutil.rmtree(p)
  261. elif os.path.exists(p):
  262. os.unlink(p)
  263. adios(distinfo_path)
  264. if not os.path.exists(egginfo_path):
  265. # There is no egg-info. This is probably because the egg-info
  266. # file/directory is not named matching the distribution name used
  267. # to name the archive file. Check for this case and report
  268. # accordingly.
  269. import glob
  270. pat = os.path.join(os.path.dirname(egginfo_path), '*.egg-info')
  271. possible = glob.glob(pat)
  272. err = "Egg metadata expected at %s but not found" % (egginfo_path,)
  273. if possible:
  274. alt = os.path.basename(possible[0])
  275. err += " (%s found - possible misnamed archive file?)" % (alt,)
  276. raise ValueError(err)
  277. if os.path.isfile(egginfo_path):
  278. # .egg-info is a single file
  279. pkginfo_path = egginfo_path
  280. pkg_info = pkginfo_to_metadata(egginfo_path, egginfo_path)
  281. os.mkdir(distinfo_path)
  282. else:
  283. # .egg-info is a directory
  284. pkginfo_path = os.path.join(egginfo_path, 'PKG-INFO')
  285. pkg_info = pkginfo_to_metadata(egginfo_path, pkginfo_path)
  286. # ignore common egg metadata that is useless to wheel
  287. shutil.copytree(egginfo_path, distinfo_path,
  288. ignore=lambda x, y: {'PKG-INFO', 'requires.txt', 'SOURCES.txt',
  289. 'not-zip-safe'}
  290. )
  291. # delete dependency_links if it is only whitespace
  292. dependency_links_path = os.path.join(distinfo_path, 'dependency_links.txt')
  293. with open(dependency_links_path, 'r') as dependency_links_file:
  294. dependency_links = dependency_links_file.read().strip()
  295. if not dependency_links:
  296. adios(dependency_links_path)
  297. write_pkg_info(os.path.join(distinfo_path, 'METADATA'), pkg_info)
  298. for license_path in self.license_paths:
  299. filename = os.path.basename(license_path)
  300. shutil.copy(license_path, os.path.join(distinfo_path, filename))
  301. adios(egginfo_path)