pypi.py 9.1 KB

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