pypi.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. from distutils.version import LooseVersion
  2. import errno
  3. import hashlib
  4. import logging
  5. try:
  6. from pathlib import Path
  7. except ImportError:
  8. from pathlib2 import Path # Backport
  9. import re
  10. import shutil
  11. from tempfile import mkdtemp
  12. import zipfile
  13. import yarg
  14. from requests_download import download, HashTracker
  15. from .util import get_cache_dir
  16. logger = logging.getLogger(__name__)
  17. def find_pypi_release(requirement):
  18. if '==' in requirement:
  19. name, version = requirement.split('==', 1)
  20. return yarg.get(name).release(version)
  21. else:
  22. return yarg.get(requirement).latest_release
  23. class NoWheelError(Exception): pass
  24. class WheelLocator(object):
  25. def __init__(self, requirement, py_version, bitness, extra_sources=None):
  26. self.requirement = requirement
  27. self.py_version = py_version
  28. self.bitness = bitness
  29. self.extra_sources = extra_sources or []
  30. if requirement.count('==') != 1:
  31. raise ValueError("Requirement {!r} did not match name==version".format(requirement))
  32. self.name, self.version = requirement.split('==', 1)
  33. def score_platform(self, platform):
  34. target = 'win_amd64' if self.bitness == 64 else 'win32'
  35. d = {target: 2, 'any': 1}
  36. return max(d.get(p, 0) for p in platform.split('.'))
  37. def score_abi(self, abi):
  38. py_version_nodot = ''.join(self.py_version.split('.')[:2])
  39. # Are there other valid options here?
  40. d = {'cp%sm' % py_version_nodot: 3, # Is the m reliable?
  41. 'abi3': 2, 'none': 1}
  42. return max(d.get(a, 0) for a in abi.split('.'))
  43. def score_interpreter(self, interpreter):
  44. py_version_nodot = ''.join(self.py_version.split('.')[:2])
  45. py_version_major = self.py_version.split('.')[0]
  46. d = {'cp'+py_version_nodot: 4,
  47. 'cp'+py_version_major: 3,
  48. 'py'+py_version_nodot: 2,
  49. 'py'+py_version_major: 1
  50. }
  51. return max(d.get(i, 0) for i in interpreter.split('.'))
  52. def pick_best_wheel(self, release_list):
  53. best_score = (0, 0, 0)
  54. best = None
  55. for release in release_list:
  56. if release.package_type != 'wheel':
  57. continue
  58. m = re.search(r'-([^-]+)-([^-]+)-([^-]+)\.whl', release.filename)
  59. if not m:
  60. continue
  61. interpreter, abi, platform = m.group(1, 2, 3)
  62. score = (self.score_platform(platform),
  63. self.score_abi(abi),
  64. self.score_interpreter(interpreter)
  65. )
  66. if any(s==0 for s in score):
  67. # Incompatible
  68. continue
  69. if score > best_score:
  70. best = release
  71. best_score = score
  72. return best
  73. def check_extra_sources(self):
  74. """Find a compatible wheel in the specified extra_sources directories.
  75. Returns a Path or None.
  76. """
  77. whl_filename_prefix = '{name}-{version}-'.format(
  78. name=re.sub("[^\w\d.]+", "_", self.name),
  79. version=re.sub("[^\w\d.]+", "_", self.version),
  80. )
  81. for source in self.extra_sources:
  82. candidates = [CachedRelease(p.name)
  83. for p in source.iterdir()
  84. if p.name.startswith(whl_filename_prefix)]
  85. rel = self.pick_best_wheel(candidates)
  86. if rel:
  87. path = source / rel.filename
  88. logger.info('Using wheel from extra directory: %s', path)
  89. return path
  90. def check_cache(self):
  91. """Find a wheel previously downloaded from PyPI in the cache.
  92. Returns a Path or None.
  93. """
  94. release_dir = get_cache_dir() / 'pypi' / self.name / self.version
  95. if not release_dir.is_dir():
  96. return None
  97. rel = self.pick_best_wheel(CachedRelease(p.name)
  98. for p in release_dir.iterdir())
  99. if rel is None:
  100. return None
  101. logger.info('Using cached wheel: %s', rel.filename)
  102. return release_dir / rel.filename
  103. def get_from_pypi(self):
  104. """Download a compatible wheel from PyPI.
  105. Downloads to the cache directory and returns the destination as a Path.
  106. Raises NoWheelError if no compatible wheel is found.
  107. """
  108. release_list = yarg.get(self.name).release(self.version)
  109. preferred_release = self.pick_best_wheel(release_list)
  110. if preferred_release is None:
  111. raise NoWheelError('No compatible wheels found for {0.name} {0.version}'.format(self))
  112. download_to = get_cache_dir() / 'pypi' / self.name / self.version
  113. try:
  114. download_to.mkdir(parents=True)
  115. except OSError:
  116. # Ignore OSError only if the directory exists
  117. if not download_to.is_dir():
  118. raise
  119. target = download_to / preferred_release.filename
  120. from . import __version__
  121. hasher = HashTracker(hashlib.md5())
  122. headers = {'user-agent': 'pynsist/'+__version__}
  123. logger.info('Downloading wheel: %s', preferred_release.url)
  124. download(preferred_release.url, str(target), headers=headers,
  125. trackers=(hasher,))
  126. if hasher.hashobj.hexdigest() != preferred_release.md5_digest:
  127. target.unlink()
  128. raise ValueError('Downloaded wheel corrupted: {}'.format(preferred_release.url))
  129. return target
  130. def fetch(self):
  131. """Find and return a compatible wheel (main interface)"""
  132. p = self.check_extra_sources()
  133. if p is not None:
  134. return p
  135. p = self.check_cache()
  136. if p is not None:
  137. return p
  138. return self.get_from_pypi()
  139. class CachedRelease(object):
  140. # Mock enough of the yarg Release object to be compatible with
  141. # pick_best_release above
  142. def __init__(self, filename):
  143. self.filename = filename
  144. self.package_type = 'wheel' if filename.endswith('.whl') else ''
  145. def merge_dir_to(src, dst):
  146. """Merge all files from one directory into another.
  147. Subdirectories will be merged recursively. If filenames are the same, those
  148. from src will overwrite those in dst. If a regular file clashes with a
  149. directory, an error will occur.
  150. """
  151. for p in src.iterdir():
  152. if p.is_dir():
  153. dst_p = dst / p.name
  154. if dst_p.is_dir():
  155. merge_dir_to(p, dst_p)
  156. elif dst_p.is_file():
  157. raise RuntimeError('Directory {} clashes with file {}'
  158. .format(p, dst_p))
  159. else:
  160. shutil.copytree(str(p), str(dst_p))
  161. else:
  162. # Copy regular file
  163. dst_p = dst / p.name
  164. if dst_p.is_dir():
  165. raise RuntimeError('File {} clashes with directory {}'
  166. .format(p, dst_p))
  167. shutil.copy2(str(p), str(dst_p))
  168. def extract_wheel(whl_file, target_dir):
  169. """Extract importable modules from a wheel to the target directory
  170. """
  171. # Extract to temporary directory
  172. td = Path(mkdtemp())
  173. with zipfile.ZipFile(str(whl_file), mode='r') as zf:
  174. zf.extractall(str(td))
  175. # Move extra lib files out of the .data subdirectory
  176. for p in td.iterdir():
  177. if p.suffix == '.data':
  178. if (p / 'purelib').is_dir():
  179. merge_dir_to(p / 'purelib', td)
  180. if (p / 'platlib').is_dir():
  181. merge_dir_to(p / 'platlib', td)
  182. # Copy to target directory
  183. target = Path(target_dir)
  184. copied_something = False
  185. for p in td.iterdir():
  186. if p.suffix not in {'.data', '.dist-info'}:
  187. if p.is_dir():
  188. # If the dst directory already exists, this will combine them.
  189. # shutil.copytree will not combine them.
  190. try:
  191. target.joinpath(p.name).mkdir()
  192. except OSError:
  193. if not target.joinpath(p.name).is_dir():
  194. raise
  195. merge_dir_to(p, target / p.name)
  196. else:
  197. shutil.copy2(str(p), str(target))
  198. copied_something = True
  199. if not copied_something:
  200. raise RuntimeError("Did not find any files to extract from wheel {}"
  201. .format(whl_file))
  202. # Clean up temporary directory
  203. shutil.rmtree(str(td))
  204. def fetch_pypi_wheels(requirements, target_dir, py_version, bitness,
  205. extra_sources=None):
  206. for req in requirements:
  207. wl = WheelLocator(req, py_version, bitness, extra_sources)
  208. whl_file = wl.fetch()
  209. extract_wheel(whl_file, target_dir)