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.

locators.py 51KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2015 Vinay Sajip.
  4. # Licensed to the Python Software Foundation under a contributor agreement.
  5. # See LICENSE.txt and CONTRIBUTORS.txt.
  6. #
  7. import gzip
  8. from io import BytesIO
  9. import json
  10. import logging
  11. import os
  12. import posixpath
  13. import re
  14. try:
  15. import threading
  16. except ImportError: # pragma: no cover
  17. import dummy_threading as threading
  18. import zlib
  19. from . import DistlibException
  20. from .compat import (urljoin, urlparse, urlunparse, url2pathname, pathname2url,
  21. queue, quote, unescape, string_types, build_opener,
  22. HTTPRedirectHandler as BaseRedirectHandler, text_type,
  23. Request, HTTPError, URLError)
  24. from .database import Distribution, DistributionPath, make_dist
  25. from .metadata import Metadata, MetadataInvalidError
  26. from .util import (cached_property, parse_credentials, ensure_slash,
  27. split_filename, get_project_data, parse_requirement,
  28. parse_name_and_version, ServerProxy, normalize_name)
  29. from .version import get_scheme, UnsupportedVersionError
  30. from .wheel import Wheel, is_compatible
  31. logger = logging.getLogger(__name__)
  32. HASHER_HASH = re.compile(r'^(\w+)=([a-f0-9]+)')
  33. CHARSET = re.compile(r';\s*charset\s*=\s*(.*)\s*$', re.I)
  34. HTML_CONTENT_TYPE = re.compile('text/html|application/x(ht)?ml')
  35. DEFAULT_INDEX = 'https://pypi.python.org/pypi'
  36. def get_all_distribution_names(url=None):
  37. """
  38. Return all distribution names known by an index.
  39. :param url: The URL of the index.
  40. :return: A list of all known distribution names.
  41. """
  42. if url is None:
  43. url = DEFAULT_INDEX
  44. client = ServerProxy(url, timeout=3.0)
  45. try:
  46. return client.list_packages()
  47. finally:
  48. client('close')()
  49. class RedirectHandler(BaseRedirectHandler):
  50. """
  51. A class to work around a bug in some Python 3.2.x releases.
  52. """
  53. # There's a bug in the base version for some 3.2.x
  54. # (e.g. 3.2.2 on Ubuntu Oneiric). If a Location header
  55. # returns e.g. /abc, it bails because it says the scheme ''
  56. # is bogus, when actually it should use the request's
  57. # URL for the scheme. See Python issue #13696.
  58. def http_error_302(self, req, fp, code, msg, headers):
  59. # Some servers (incorrectly) return multiple Location headers
  60. # (so probably same goes for URI). Use first header.
  61. newurl = None
  62. for key in ('location', 'uri'):
  63. if key in headers:
  64. newurl = headers[key]
  65. break
  66. if newurl is None: # pragma: no cover
  67. return
  68. urlparts = urlparse(newurl)
  69. if urlparts.scheme == '':
  70. newurl = urljoin(req.get_full_url(), newurl)
  71. if hasattr(headers, 'replace_header'):
  72. headers.replace_header(key, newurl)
  73. else:
  74. headers[key] = newurl
  75. return BaseRedirectHandler.http_error_302(self, req, fp, code, msg,
  76. headers)
  77. http_error_301 = http_error_303 = http_error_307 = http_error_302
  78. class Locator(object):
  79. """
  80. A base class for locators - things that locate distributions.
  81. """
  82. source_extensions = ('.tar.gz', '.tar.bz2', '.tar', '.zip', '.tgz', '.tbz')
  83. binary_extensions = ('.egg', '.exe', '.whl')
  84. excluded_extensions = ('.pdf',)
  85. # A list of tags indicating which wheels you want to match. The default
  86. # value of None matches against the tags compatible with the running
  87. # Python. If you want to match other values, set wheel_tags on a locator
  88. # instance to a list of tuples (pyver, abi, arch) which you want to match.
  89. wheel_tags = None
  90. downloadable_extensions = source_extensions + ('.whl',)
  91. def __init__(self, scheme='default'):
  92. """
  93. Initialise an instance.
  94. :param scheme: Because locators look for most recent versions, they
  95. need to know the version scheme to use. This specifies
  96. the current PEP-recommended scheme - use ``'legacy'``
  97. if you need to support existing distributions on PyPI.
  98. """
  99. self._cache = {}
  100. self.scheme = scheme
  101. # Because of bugs in some of the handlers on some of the platforms,
  102. # we use our own opener rather than just using urlopen.
  103. self.opener = build_opener(RedirectHandler())
  104. # If get_project() is called from locate(), the matcher instance
  105. # is set from the requirement passed to locate(). See issue #18 for
  106. # why this can be useful to know.
  107. self.matcher = None
  108. self.errors = queue.Queue()
  109. def get_errors(self):
  110. """
  111. Return any errors which have occurred.
  112. """
  113. result = []
  114. while not self.errors.empty(): # pragma: no cover
  115. try:
  116. e = self.errors.get(False)
  117. result.append(e)
  118. except self.errors.Empty:
  119. continue
  120. self.errors.task_done()
  121. return result
  122. def clear_errors(self):
  123. """
  124. Clear any errors which may have been logged.
  125. """
  126. # Just get the errors and throw them away
  127. self.get_errors()
  128. def clear_cache(self):
  129. self._cache.clear()
  130. def _get_scheme(self):
  131. return self._scheme
  132. def _set_scheme(self, value):
  133. self._scheme = value
  134. scheme = property(_get_scheme, _set_scheme)
  135. def _get_project(self, name):
  136. """
  137. For a given project, get a dictionary mapping available versions to Distribution
  138. instances.
  139. This should be implemented in subclasses.
  140. If called from a locate() request, self.matcher will be set to a
  141. matcher for the requirement to satisfy, otherwise it will be None.
  142. """
  143. raise NotImplementedError('Please implement in the subclass')
  144. def get_distribution_names(self):
  145. """
  146. Return all the distribution names known to this locator.
  147. """
  148. raise NotImplementedError('Please implement in the subclass')
  149. def get_project(self, name):
  150. """
  151. For a given project, get a dictionary mapping available versions to Distribution
  152. instances.
  153. This calls _get_project to do all the work, and just implements a caching layer on top.
  154. """
  155. if self._cache is None: # pragma: no cover
  156. result = self._get_project(name)
  157. elif name in self._cache:
  158. result = self._cache[name]
  159. else:
  160. self.clear_errors()
  161. result = self._get_project(name)
  162. self._cache[name] = result
  163. return result
  164. def score_url(self, url):
  165. """
  166. Give an url a score which can be used to choose preferred URLs
  167. for a given project release.
  168. """
  169. t = urlparse(url)
  170. basename = posixpath.basename(t.path)
  171. compatible = True
  172. is_wheel = basename.endswith('.whl')
  173. is_downloadable = basename.endswith(self.downloadable_extensions)
  174. if is_wheel:
  175. compatible = is_compatible(Wheel(basename), self.wheel_tags)
  176. return (t.scheme == 'https', 'pypi.python.org' in t.netloc,
  177. is_downloadable, is_wheel, compatible, basename)
  178. def prefer_url(self, url1, url2):
  179. """
  180. Choose one of two URLs where both are candidates for distribution
  181. archives for the same version of a distribution (for example,
  182. .tar.gz vs. zip).
  183. The current implementation favours https:// URLs over http://, archives
  184. from PyPI over those from other locations, wheel compatibility (if a
  185. wheel) and then the archive name.
  186. """
  187. result = url2
  188. if url1:
  189. s1 = self.score_url(url1)
  190. s2 = self.score_url(url2)
  191. if s1 > s2:
  192. result = url1
  193. if result != url2:
  194. logger.debug('Not replacing %r with %r', url1, url2)
  195. else:
  196. logger.debug('Replacing %r with %r', url1, url2)
  197. return result
  198. def split_filename(self, filename, project_name):
  199. """
  200. Attempt to split a filename in project name, version and Python version.
  201. """
  202. return split_filename(filename, project_name)
  203. def convert_url_to_download_info(self, url, project_name):
  204. """
  205. See if a URL is a candidate for a download URL for a project (the URL
  206. has typically been scraped from an HTML page).
  207. If it is, a dictionary is returned with keys "name", "version",
  208. "filename" and "url"; otherwise, None is returned.
  209. """
  210. def same_project(name1, name2):
  211. return normalize_name(name1) == normalize_name(name2)
  212. result = None
  213. scheme, netloc, path, params, query, frag = urlparse(url)
  214. if frag.lower().startswith('egg='): # pragma: no cover
  215. logger.debug('%s: version hint in fragment: %r',
  216. project_name, frag)
  217. m = HASHER_HASH.match(frag)
  218. if m:
  219. algo, digest = m.groups()
  220. else:
  221. algo, digest = None, None
  222. origpath = path
  223. if path and path[-1] == '/': # pragma: no cover
  224. path = path[:-1]
  225. if path.endswith('.whl'):
  226. try:
  227. wheel = Wheel(path)
  228. if not is_compatible(wheel, self.wheel_tags):
  229. logger.debug('Wheel not compatible: %s', path)
  230. else:
  231. if project_name is None:
  232. include = True
  233. else:
  234. include = same_project(wheel.name, project_name)
  235. if include:
  236. result = {
  237. 'name': wheel.name,
  238. 'version': wheel.version,
  239. 'filename': wheel.filename,
  240. 'url': urlunparse((scheme, netloc, origpath,
  241. params, query, '')),
  242. 'python-version': ', '.join(
  243. ['.'.join(list(v[2:])) for v in wheel.pyver]),
  244. }
  245. except Exception as e: # pragma: no cover
  246. logger.warning('invalid path for wheel: %s', path)
  247. elif not path.endswith(self.downloadable_extensions): # pragma: no cover
  248. logger.debug('Not downloadable: %s', path)
  249. else: # downloadable extension
  250. path = filename = posixpath.basename(path)
  251. for ext in self.downloadable_extensions:
  252. if path.endswith(ext):
  253. path = path[:-len(ext)]
  254. t = self.split_filename(path, project_name)
  255. if not t: # pragma: no cover
  256. logger.debug('No match for project/version: %s', path)
  257. else:
  258. name, version, pyver = t
  259. if not project_name or same_project(project_name, name):
  260. result = {
  261. 'name': name,
  262. 'version': version,
  263. 'filename': filename,
  264. 'url': urlunparse((scheme, netloc, origpath,
  265. params, query, '')),
  266. #'packagetype': 'sdist',
  267. }
  268. if pyver: # pragma: no cover
  269. result['python-version'] = pyver
  270. break
  271. if result and algo:
  272. result['%s_digest' % algo] = digest
  273. return result
  274. def _get_digest(self, info):
  275. """
  276. Get a digest from a dictionary by looking at keys of the form
  277. 'algo_digest'.
  278. Returns a 2-tuple (algo, digest) if found, else None. Currently
  279. looks only for SHA256, then MD5.
  280. """
  281. result = None
  282. for algo in ('sha256', 'md5'):
  283. key = '%s_digest' % algo
  284. if key in info:
  285. result = (algo, info[key])
  286. break
  287. return result
  288. def _update_version_data(self, result, info):
  289. """
  290. Update a result dictionary (the final result from _get_project) with a
  291. dictionary for a specific version, which typically holds information
  292. gleaned from a filename or URL for an archive for the distribution.
  293. """
  294. name = info.pop('name')
  295. version = info.pop('version')
  296. if version in result:
  297. dist = result[version]
  298. md = dist.metadata
  299. else:
  300. dist = make_dist(name, version, scheme=self.scheme)
  301. md = dist.metadata
  302. dist.digest = digest = self._get_digest(info)
  303. url = info['url']
  304. result['digests'][url] = digest
  305. if md.source_url != info['url']:
  306. md.source_url = self.prefer_url(md.source_url, url)
  307. result['urls'].setdefault(version, set()).add(url)
  308. dist.locator = self
  309. result[version] = dist
  310. def locate(self, requirement, prereleases=False):
  311. """
  312. Find the most recent distribution which matches the given
  313. requirement.
  314. :param requirement: A requirement of the form 'foo (1.0)' or perhaps
  315. 'foo (>= 1.0, < 2.0, != 1.3)'
  316. :param prereleases: If ``True``, allow pre-release versions
  317. to be located. Otherwise, pre-release versions
  318. are not returned.
  319. :return: A :class:`Distribution` instance, or ``None`` if no such
  320. distribution could be located.
  321. """
  322. result = None
  323. r = parse_requirement(requirement)
  324. if r is None: # pragma: no cover
  325. raise DistlibException('Not a valid requirement: %r' % requirement)
  326. scheme = get_scheme(self.scheme)
  327. self.matcher = matcher = scheme.matcher(r.requirement)
  328. logger.debug('matcher: %s (%s)', matcher, type(matcher).__name__)
  329. versions = self.get_project(r.name)
  330. if len(versions) > 2: # urls and digests keys are present
  331. # sometimes, versions are invalid
  332. slist = []
  333. vcls = matcher.version_class
  334. for k in versions:
  335. if k in ('urls', 'digests'):
  336. continue
  337. try:
  338. if not matcher.match(k):
  339. logger.debug('%s did not match %r', matcher, k)
  340. else:
  341. if prereleases or not vcls(k).is_prerelease:
  342. slist.append(k)
  343. else:
  344. logger.debug('skipping pre-release '
  345. 'version %s of %s', k, matcher.name)
  346. except Exception: # pragma: no cover
  347. logger.warning('error matching %s with %r', matcher, k)
  348. pass # slist.append(k)
  349. if len(slist) > 1:
  350. slist = sorted(slist, key=scheme.key)
  351. if slist:
  352. logger.debug('sorted list: %s', slist)
  353. version = slist[-1]
  354. result = versions[version]
  355. if result:
  356. if r.extras:
  357. result.extras = r.extras
  358. result.download_urls = versions.get('urls', {}).get(version, set())
  359. d = {}
  360. sd = versions.get('digests', {})
  361. for url in result.download_urls:
  362. if url in sd: # pragma: no cover
  363. d[url] = sd[url]
  364. result.digests = d
  365. self.matcher = None
  366. return result
  367. class PyPIRPCLocator(Locator):
  368. """
  369. This locator uses XML-RPC to locate distributions. It therefore
  370. cannot be used with simple mirrors (that only mirror file content).
  371. """
  372. def __init__(self, url, **kwargs):
  373. """
  374. Initialise an instance.
  375. :param url: The URL to use for XML-RPC.
  376. :param kwargs: Passed to the superclass constructor.
  377. """
  378. super(PyPIRPCLocator, self).__init__(**kwargs)
  379. self.base_url = url
  380. self.client = ServerProxy(url, timeout=3.0)
  381. def get_distribution_names(self):
  382. """
  383. Return all the distribution names known to this locator.
  384. """
  385. return set(self.client.list_packages())
  386. def _get_project(self, name):
  387. result = {'urls': {}, 'digests': {}}
  388. versions = self.client.package_releases(name, True)
  389. for v in versions:
  390. urls = self.client.release_urls(name, v)
  391. data = self.client.release_data(name, v)
  392. metadata = Metadata(scheme=self.scheme)
  393. metadata.name = data['name']
  394. metadata.version = data['version']
  395. metadata.license = data.get('license')
  396. metadata.keywords = data.get('keywords', [])
  397. metadata.summary = data.get('summary')
  398. dist = Distribution(metadata)
  399. if urls:
  400. info = urls[0]
  401. metadata.source_url = info['url']
  402. dist.digest = self._get_digest(info)
  403. dist.locator = self
  404. result[v] = dist
  405. for info in urls:
  406. url = info['url']
  407. digest = self._get_digest(info)
  408. result['urls'].setdefault(v, set()).add(url)
  409. result['digests'][url] = digest
  410. return result
  411. class PyPIJSONLocator(Locator):
  412. """
  413. This locator uses PyPI's JSON interface. It's very limited in functionality
  414. and probably not worth using.
  415. """
  416. def __init__(self, url, **kwargs):
  417. super(PyPIJSONLocator, self).__init__(**kwargs)
  418. self.base_url = ensure_slash(url)
  419. def get_distribution_names(self):
  420. """
  421. Return all the distribution names known to this locator.
  422. """
  423. raise NotImplementedError('Not available from this locator')
  424. def _get_project(self, name):
  425. result = {'urls': {}, 'digests': {}}
  426. url = urljoin(self.base_url, '%s/json' % quote(name))
  427. try:
  428. resp = self.opener.open(url)
  429. data = resp.read().decode() # for now
  430. d = json.loads(data)
  431. md = Metadata(scheme=self.scheme)
  432. data = d['info']
  433. md.name = data['name']
  434. md.version = data['version']
  435. md.license = data.get('license')
  436. md.keywords = data.get('keywords', [])
  437. md.summary = data.get('summary')
  438. dist = Distribution(md)
  439. dist.locator = self
  440. urls = d['urls']
  441. result[md.version] = dist
  442. for info in d['urls']:
  443. url = info['url']
  444. dist.download_urls.add(url)
  445. dist.digests[url] = self._get_digest(info)
  446. result['urls'].setdefault(md.version, set()).add(url)
  447. result['digests'][url] = self._get_digest(info)
  448. # Now get other releases
  449. for version, infos in d['releases'].items():
  450. if version == md.version:
  451. continue # already done
  452. omd = Metadata(scheme=self.scheme)
  453. omd.name = md.name
  454. omd.version = version
  455. odist = Distribution(omd)
  456. odist.locator = self
  457. result[version] = odist
  458. for info in infos:
  459. url = info['url']
  460. odist.download_urls.add(url)
  461. odist.digests[url] = self._get_digest(info)
  462. result['urls'].setdefault(version, set()).add(url)
  463. result['digests'][url] = self._get_digest(info)
  464. # for info in urls:
  465. # md.source_url = info['url']
  466. # dist.digest = self._get_digest(info)
  467. # dist.locator = self
  468. # for info in urls:
  469. # url = info['url']
  470. # result['urls'].setdefault(md.version, set()).add(url)
  471. # result['digests'][url] = self._get_digest(info)
  472. except Exception as e:
  473. self.errors.put(text_type(e))
  474. logger.exception('JSON fetch failed: %s', e)
  475. return result
  476. class Page(object):
  477. """
  478. This class represents a scraped HTML page.
  479. """
  480. # The following slightly hairy-looking regex just looks for the contents of
  481. # an anchor link, which has an attribute "href" either immediately preceded
  482. # or immediately followed by a "rel" attribute. The attribute values can be
  483. # declared with double quotes, single quotes or no quotes - which leads to
  484. # the length of the expression.
  485. _href = re.compile("""
  486. (rel\\s*=\\s*(?:"(?P<rel1>[^"]*)"|'(?P<rel2>[^']*)'|(?P<rel3>[^>\\s\n]*))\\s+)?
  487. href\\s*=\\s*(?:"(?P<url1>[^"]*)"|'(?P<url2>[^']*)'|(?P<url3>[^>\\s\n]*))
  488. (\\s+rel\\s*=\\s*(?:"(?P<rel4>[^"]*)"|'(?P<rel5>[^']*)'|(?P<rel6>[^>\\s\n]*)))?
  489. """, re.I | re.S | re.X)
  490. _base = re.compile(r"""<base\s+href\s*=\s*['"]?([^'">]+)""", re.I | re.S)
  491. def __init__(self, data, url):
  492. """
  493. Initialise an instance with the Unicode page contents and the URL they
  494. came from.
  495. """
  496. self.data = data
  497. self.base_url = self.url = url
  498. m = self._base.search(self.data)
  499. if m:
  500. self.base_url = m.group(1)
  501. _clean_re = re.compile(r'[^a-z0-9$&+,/:;=?@.#%_\\|-]', re.I)
  502. @cached_property
  503. def links(self):
  504. """
  505. Return the URLs of all the links on a page together with information
  506. about their "rel" attribute, for determining which ones to treat as
  507. downloads and which ones to queue for further scraping.
  508. """
  509. def clean(url):
  510. "Tidy up an URL."
  511. scheme, netloc, path, params, query, frag = urlparse(url)
  512. return urlunparse((scheme, netloc, quote(path),
  513. params, query, frag))
  514. result = set()
  515. for match in self._href.finditer(self.data):
  516. d = match.groupdict('')
  517. rel = (d['rel1'] or d['rel2'] or d['rel3'] or
  518. d['rel4'] or d['rel5'] or d['rel6'])
  519. url = d['url1'] or d['url2'] or d['url3']
  520. url = urljoin(self.base_url, url)
  521. url = unescape(url)
  522. url = self._clean_re.sub(lambda m: '%%%2x' % ord(m.group(0)), url)
  523. result.add((url, rel))
  524. # We sort the result, hoping to bring the most recent versions
  525. # to the front
  526. result = sorted(result, key=lambda t: t[0], reverse=True)
  527. return result
  528. class SimpleScrapingLocator(Locator):
  529. """
  530. A locator which scrapes HTML pages to locate downloads for a distribution.
  531. This runs multiple threads to do the I/O; performance is at least as good
  532. as pip's PackageFinder, which works in an analogous fashion.
  533. """
  534. # These are used to deal with various Content-Encoding schemes.
  535. decoders = {
  536. 'deflate': zlib.decompress,
  537. 'gzip': lambda b: gzip.GzipFile(fileobj=BytesIO(d)).read(),
  538. 'none': lambda b: b,
  539. }
  540. def __init__(self, url, timeout=None, num_workers=10, **kwargs):
  541. """
  542. Initialise an instance.
  543. :param url: The root URL to use for scraping.
  544. :param timeout: The timeout, in seconds, to be applied to requests.
  545. This defaults to ``None`` (no timeout specified).
  546. :param num_workers: The number of worker threads you want to do I/O,
  547. This defaults to 10.
  548. :param kwargs: Passed to the superclass.
  549. """
  550. super(SimpleScrapingLocator, self).__init__(**kwargs)
  551. self.base_url = ensure_slash(url)
  552. self.timeout = timeout
  553. self._page_cache = {}
  554. self._seen = set()
  555. self._to_fetch = queue.Queue()
  556. self._bad_hosts = set()
  557. self.skip_externals = False
  558. self.num_workers = num_workers
  559. self._lock = threading.RLock()
  560. # See issue #45: we need to be resilient when the locator is used
  561. # in a thread, e.g. with concurrent.futures. We can't use self._lock
  562. # as it is for coordinating our internal threads - the ones created
  563. # in _prepare_threads.
  564. self._gplock = threading.RLock()
  565. self.platform_check = False # See issue #112
  566. def _prepare_threads(self):
  567. """
  568. Threads are created only when get_project is called, and terminate
  569. before it returns. They are there primarily to parallelise I/O (i.e.
  570. fetching web pages).
  571. """
  572. self._threads = []
  573. for i in range(self.num_workers):
  574. t = threading.Thread(target=self._fetch)
  575. t.setDaemon(True)
  576. t.start()
  577. self._threads.append(t)
  578. def _wait_threads(self):
  579. """
  580. Tell all the threads to terminate (by sending a sentinel value) and
  581. wait for them to do so.
  582. """
  583. # Note that you need two loops, since you can't say which
  584. # thread will get each sentinel
  585. for t in self._threads:
  586. self._to_fetch.put(None) # sentinel
  587. for t in self._threads:
  588. t.join()
  589. self._threads = []
  590. def _get_project(self, name):
  591. result = {'urls': {}, 'digests': {}}
  592. with self._gplock:
  593. self.result = result
  594. self.project_name = name
  595. url = urljoin(self.base_url, '%s/' % quote(name))
  596. self._seen.clear()
  597. self._page_cache.clear()
  598. self._prepare_threads()
  599. try:
  600. logger.debug('Queueing %s', url)
  601. self._to_fetch.put(url)
  602. self._to_fetch.join()
  603. finally:
  604. self._wait_threads()
  605. del self.result
  606. return result
  607. platform_dependent = re.compile(r'\b(linux_(i\d86|x86_64|arm\w+)|'
  608. r'win(32|_amd64)|macosx_?\d+)\b', re.I)
  609. def _is_platform_dependent(self, url):
  610. """
  611. Does an URL refer to a platform-specific download?
  612. """
  613. return self.platform_dependent.search(url)
  614. def _process_download(self, url):
  615. """
  616. See if an URL is a suitable download for a project.
  617. If it is, register information in the result dictionary (for
  618. _get_project) about the specific version it's for.
  619. Note that the return value isn't actually used other than as a boolean
  620. value.
  621. """
  622. if self.platform_check and self._is_platform_dependent(url):
  623. info = None
  624. else:
  625. info = self.convert_url_to_download_info(url, self.project_name)
  626. logger.debug('process_download: %s -> %s', url, info)
  627. if info:
  628. with self._lock: # needed because self.result is shared
  629. self._update_version_data(self.result, info)
  630. return info
  631. def _should_queue(self, link, referrer, rel):
  632. """
  633. Determine whether a link URL from a referring page and with a
  634. particular "rel" attribute should be queued for scraping.
  635. """
  636. scheme, netloc, path, _, _, _ = urlparse(link)
  637. if path.endswith(self.source_extensions + self.binary_extensions +
  638. self.excluded_extensions):
  639. result = False
  640. elif self.skip_externals and not link.startswith(self.base_url):
  641. result = False
  642. elif not referrer.startswith(self.base_url):
  643. result = False
  644. elif rel not in ('homepage', 'download'):
  645. result = False
  646. elif scheme not in ('http', 'https', 'ftp'):
  647. result = False
  648. elif self._is_platform_dependent(link):
  649. result = False
  650. else:
  651. host = netloc.split(':', 1)[0]
  652. if host.lower() == 'localhost':
  653. result = False
  654. else:
  655. result = True
  656. logger.debug('should_queue: %s (%s) from %s -> %s', link, rel,
  657. referrer, result)
  658. return result
  659. def _fetch(self):
  660. """
  661. Get a URL to fetch from the work queue, get the HTML page, examine its
  662. links for download candidates and candidates for further scraping.
  663. This is a handy method to run in a thread.
  664. """
  665. while True:
  666. url = self._to_fetch.get()
  667. try:
  668. if url:
  669. page = self.get_page(url)
  670. if page is None: # e.g. after an error
  671. continue
  672. for link, rel in page.links:
  673. if link not in self._seen:
  674. try:
  675. self._seen.add(link)
  676. if (not self._process_download(link) and
  677. self._should_queue(link, url, rel)):
  678. logger.debug('Queueing %s from %s', link, url)
  679. self._to_fetch.put(link)
  680. except MetadataInvalidError: # e.g. invalid versions
  681. pass
  682. except Exception as e: # pragma: no cover
  683. self.errors.put(text_type(e))
  684. finally:
  685. # always do this, to avoid hangs :-)
  686. self._to_fetch.task_done()
  687. if not url:
  688. #logger.debug('Sentinel seen, quitting.')
  689. break
  690. def get_page(self, url):
  691. """
  692. Get the HTML for an URL, possibly from an in-memory cache.
  693. XXX TODO Note: this cache is never actually cleared. It's assumed that
  694. the data won't get stale over the lifetime of a locator instance (not
  695. necessarily true for the default_locator).
  696. """
  697. # http://peak.telecommunity.com/DevCenter/EasyInstall#package-index-api
  698. scheme, netloc, path, _, _, _ = urlparse(url)
  699. if scheme == 'file' and os.path.isdir(url2pathname(path)):
  700. url = urljoin(ensure_slash(url), 'index.html')
  701. if url in self._page_cache:
  702. result = self._page_cache[url]
  703. logger.debug('Returning %s from cache: %s', url, result)
  704. else:
  705. host = netloc.split(':', 1)[0]
  706. result = None
  707. if host in self._bad_hosts:
  708. logger.debug('Skipping %s due to bad host %s', url, host)
  709. else:
  710. req = Request(url, headers={'Accept-encoding': 'identity'})
  711. try:
  712. logger.debug('Fetching %s', url)
  713. resp = self.opener.open(req, timeout=self.timeout)
  714. logger.debug('Fetched %s', url)
  715. headers = resp.info()
  716. content_type = headers.get('Content-Type', '')
  717. if HTML_CONTENT_TYPE.match(content_type):
  718. final_url = resp.geturl()
  719. data = resp.read()
  720. encoding = headers.get('Content-Encoding')
  721. if encoding:
  722. decoder = self.decoders[encoding] # fail if not found
  723. data = decoder(data)
  724. encoding = 'utf-8'
  725. m = CHARSET.search(content_type)
  726. if m:
  727. encoding = m.group(1)
  728. try:
  729. data = data.decode(encoding)
  730. except UnicodeError: # pragma: no cover
  731. data = data.decode('latin-1') # fallback
  732. result = Page(data, final_url)
  733. self._page_cache[final_url] = result
  734. except HTTPError as e:
  735. if e.code != 404:
  736. logger.exception('Fetch failed: %s: %s', url, e)
  737. except URLError as e: # pragma: no cover
  738. logger.exception('Fetch failed: %s: %s', url, e)
  739. with self._lock:
  740. self._bad_hosts.add(host)
  741. except Exception as e: # pragma: no cover
  742. logger.exception('Fetch failed: %s: %s', url, e)
  743. finally:
  744. self._page_cache[url] = result # even if None (failure)
  745. return result
  746. _distname_re = re.compile('<a href=[^>]*>([^<]+)<')
  747. def get_distribution_names(self):
  748. """
  749. Return all the distribution names known to this locator.
  750. """
  751. result = set()
  752. page = self.get_page(self.base_url)
  753. if not page:
  754. raise DistlibException('Unable to get %s' % self.base_url)
  755. for match in self._distname_re.finditer(page.data):
  756. result.add(match.group(1))
  757. return result
  758. class DirectoryLocator(Locator):
  759. """
  760. This class locates distributions in a directory tree.
  761. """
  762. def __init__(self, path, **kwargs):
  763. """
  764. Initialise an instance.
  765. :param path: The root of the directory tree to search.
  766. :param kwargs: Passed to the superclass constructor,
  767. except for:
  768. * recursive - if True (the default), subdirectories are
  769. recursed into. If False, only the top-level directory
  770. is searched,
  771. """
  772. self.recursive = kwargs.pop('recursive', True)
  773. super(DirectoryLocator, self).__init__(**kwargs)
  774. path = os.path.abspath(path)
  775. if not os.path.isdir(path): # pragma: no cover
  776. raise DistlibException('Not a directory: %r' % path)
  777. self.base_dir = path
  778. def should_include(self, filename, parent):
  779. """
  780. Should a filename be considered as a candidate for a distribution
  781. archive? As well as the filename, the directory which contains it
  782. is provided, though not used by the current implementation.
  783. """
  784. return filename.endswith(self.downloadable_extensions)
  785. def _get_project(self, name):
  786. result = {'urls': {}, 'digests': {}}
  787. for root, dirs, files in os.walk(self.base_dir):
  788. for fn in files:
  789. if self.should_include(fn, root):
  790. fn = os.path.join(root, fn)
  791. url = urlunparse(('file', '',
  792. pathname2url(os.path.abspath(fn)),
  793. '', '', ''))
  794. info = self.convert_url_to_download_info(url, name)
  795. if info:
  796. self._update_version_data(result, info)
  797. if not self.recursive:
  798. break
  799. return result
  800. def get_distribution_names(self):
  801. """
  802. Return all the distribution names known to this locator.
  803. """
  804. result = set()
  805. for root, dirs, files in os.walk(self.base_dir):
  806. for fn in files:
  807. if self.should_include(fn, root):
  808. fn = os.path.join(root, fn)
  809. url = urlunparse(('file', '',
  810. pathname2url(os.path.abspath(fn)),
  811. '', '', ''))
  812. info = self.convert_url_to_download_info(url, None)
  813. if info:
  814. result.add(info['name'])
  815. if not self.recursive:
  816. break
  817. return result
  818. class JSONLocator(Locator):
  819. """
  820. This locator uses special extended metadata (not available on PyPI) and is
  821. the basis of performant dependency resolution in distlib. Other locators
  822. require archive downloads before dependencies can be determined! As you
  823. might imagine, that can be slow.
  824. """
  825. def get_distribution_names(self):
  826. """
  827. Return all the distribution names known to this locator.
  828. """
  829. raise NotImplementedError('Not available from this locator')
  830. def _get_project(self, name):
  831. result = {'urls': {}, 'digests': {}}
  832. data = get_project_data(name)
  833. if data:
  834. for info in data.get('files', []):
  835. if info['ptype'] != 'sdist' or info['pyversion'] != 'source':
  836. continue
  837. # We don't store summary in project metadata as it makes
  838. # the data bigger for no benefit during dependency
  839. # resolution
  840. dist = make_dist(data['name'], info['version'],
  841. summary=data.get('summary',
  842. 'Placeholder for summary'),
  843. scheme=self.scheme)
  844. md = dist.metadata
  845. md.source_url = info['url']
  846. # TODO SHA256 digest
  847. if 'digest' in info and info['digest']:
  848. dist.digest = ('md5', info['digest'])
  849. md.dependencies = info.get('requirements', {})
  850. dist.exports = info.get('exports', {})
  851. result[dist.version] = dist
  852. result['urls'].setdefault(dist.version, set()).add(info['url'])
  853. return result
  854. class DistPathLocator(Locator):
  855. """
  856. This locator finds installed distributions in a path. It can be useful for
  857. adding to an :class:`AggregatingLocator`.
  858. """
  859. def __init__(self, distpath, **kwargs):
  860. """
  861. Initialise an instance.
  862. :param distpath: A :class:`DistributionPath` instance to search.
  863. """
  864. super(DistPathLocator, self).__init__(**kwargs)
  865. assert isinstance(distpath, DistributionPath)
  866. self.distpath = distpath
  867. def _get_project(self, name):
  868. dist = self.distpath.get_distribution(name)
  869. if dist is None:
  870. result = {'urls': {}, 'digests': {}}
  871. else:
  872. result = {
  873. dist.version: dist,
  874. 'urls': {dist.version: set([dist.source_url])},
  875. 'digests': {dist.version: set([None])}
  876. }
  877. return result
  878. class AggregatingLocator(Locator):
  879. """
  880. This class allows you to chain and/or merge a list of locators.
  881. """
  882. def __init__(self, *locators, **kwargs):
  883. """
  884. Initialise an instance.
  885. :param locators: The list of locators to search.
  886. :param kwargs: Passed to the superclass constructor,
  887. except for:
  888. * merge - if False (the default), the first successful
  889. search from any of the locators is returned. If True,
  890. the results from all locators are merged (this can be
  891. slow).
  892. """
  893. self.merge = kwargs.pop('merge', False)
  894. self.locators = locators
  895. super(AggregatingLocator, self).__init__(**kwargs)
  896. def clear_cache(self):
  897. super(AggregatingLocator, self).clear_cache()
  898. for locator in self.locators:
  899. locator.clear_cache()
  900. def _set_scheme(self, value):
  901. self._scheme = value
  902. for locator in self.locators:
  903. locator.scheme = value
  904. scheme = property(Locator.scheme.fget, _set_scheme)
  905. def _get_project(self, name):
  906. result = {}
  907. for locator in self.locators:
  908. d = locator.get_project(name)
  909. if d:
  910. if self.merge:
  911. files = result.get('urls', {})
  912. digests = result.get('digests', {})
  913. # next line could overwrite result['urls'], result['digests']
  914. result.update(d)
  915. df = result.get('urls')
  916. if files and df:
  917. for k, v in files.items():
  918. if k in df:
  919. df[k] |= v
  920. else:
  921. df[k] = v
  922. dd = result.get('digests')
  923. if digests and dd:
  924. dd.update(digests)
  925. else:
  926. # See issue #18. If any dists are found and we're looking
  927. # for specific constraints, we only return something if
  928. # a match is found. For example, if a DirectoryLocator
  929. # returns just foo (1.0) while we're looking for
  930. # foo (>= 2.0), we'll pretend there was nothing there so
  931. # that subsequent locators can be queried. Otherwise we
  932. # would just return foo (1.0) which would then lead to a
  933. # failure to find foo (>= 2.0), because other locators
  934. # weren't searched. Note that this only matters when
  935. # merge=False.
  936. if self.matcher is None:
  937. found = True
  938. else:
  939. found = False
  940. for k in d:
  941. if self.matcher.match(k):
  942. found = True
  943. break
  944. if found:
  945. result = d
  946. break
  947. return result
  948. def get_distribution_names(self):
  949. """
  950. Return all the distribution names known to this locator.
  951. """
  952. result = set()
  953. for locator in self.locators:
  954. try:
  955. result |= locator.get_distribution_names()
  956. except NotImplementedError:
  957. pass
  958. return result
  959. # We use a legacy scheme simply because most of the dists on PyPI use legacy
  960. # versions which don't conform to PEP 426 / PEP 440.
  961. default_locator = AggregatingLocator(
  962. JSONLocator(),
  963. SimpleScrapingLocator('https://pypi.python.org/simple/',
  964. timeout=3.0),
  965. scheme='legacy')
  966. locate = default_locator.locate
  967. NAME_VERSION_RE = re.compile(r'(?P<name>[\w-]+)\s*'
  968. r'\(\s*(==\s*)?(?P<ver>[^)]+)\)$')
  969. class DependencyFinder(object):
  970. """
  971. Locate dependencies for distributions.
  972. """
  973. def __init__(self, locator=None):
  974. """
  975. Initialise an instance, using the specified locator
  976. to locate distributions.
  977. """
  978. self.locator = locator or default_locator
  979. self.scheme = get_scheme(self.locator.scheme)
  980. def add_distribution(self, dist):
  981. """
  982. Add a distribution to the finder. This will update internal information
  983. about who provides what.
  984. :param dist: The distribution to add.
  985. """
  986. logger.debug('adding distribution %s', dist)
  987. name = dist.key
  988. self.dists_by_name[name] = dist
  989. self.dists[(name, dist.version)] = dist
  990. for p in dist.provides:
  991. name, version = parse_name_and_version(p)
  992. logger.debug('Add to provided: %s, %s, %s', name, version, dist)
  993. self.provided.setdefault(name, set()).add((version, dist))
  994. def remove_distribution(self, dist):
  995. """
  996. Remove a distribution from the finder. This will update internal
  997. information about who provides what.
  998. :param dist: The distribution to remove.
  999. """
  1000. logger.debug('removing distribution %s', dist)
  1001. name = dist.key
  1002. del self.dists_by_name[name]
  1003. del self.dists[(name, dist.version)]
  1004. for p in dist.provides:
  1005. name, version = parse_name_and_version(p)
  1006. logger.debug('Remove from provided: %s, %s, %s', name, version, dist)
  1007. s = self.provided[name]
  1008. s.remove((version, dist))
  1009. if not s:
  1010. del self.provided[name]
  1011. def get_matcher(self, reqt):
  1012. """
  1013. Get a version matcher for a requirement.
  1014. :param reqt: The requirement
  1015. :type reqt: str
  1016. :return: A version matcher (an instance of
  1017. :class:`distlib.version.Matcher`).
  1018. """
  1019. try:
  1020. matcher = self.scheme.matcher(reqt)
  1021. except UnsupportedVersionError: # pragma: no cover
  1022. # XXX compat-mode if cannot read the version
  1023. name = reqt.split()[0]
  1024. matcher = self.scheme.matcher(name)
  1025. return matcher
  1026. def find_providers(self, reqt):
  1027. """
  1028. Find the distributions which can fulfill a requirement.
  1029. :param reqt: The requirement.
  1030. :type reqt: str
  1031. :return: A set of distribution which can fulfill the requirement.
  1032. """
  1033. matcher = self.get_matcher(reqt)
  1034. name = matcher.key # case-insensitive
  1035. result = set()
  1036. provided = self.provided
  1037. if name in provided:
  1038. for version, provider in provided[name]:
  1039. try:
  1040. match = matcher.match(version)
  1041. except UnsupportedVersionError:
  1042. match = False
  1043. if match:
  1044. result.add(provider)
  1045. break
  1046. return result
  1047. def try_to_replace(self, provider, other, problems):
  1048. """
  1049. Attempt to replace one provider with another. This is typically used
  1050. when resolving dependencies from multiple sources, e.g. A requires
  1051. (B >= 1.0) while C requires (B >= 1.1).
  1052. For successful replacement, ``provider`` must meet all the requirements
  1053. which ``other`` fulfills.
  1054. :param provider: The provider we are trying to replace with.
  1055. :param other: The provider we're trying to replace.
  1056. :param problems: If False is returned, this will contain what
  1057. problems prevented replacement. This is currently
  1058. a tuple of the literal string 'cantreplace',
  1059. ``provider``, ``other`` and the set of requirements
  1060. that ``provider`` couldn't fulfill.
  1061. :return: True if we can replace ``other`` with ``provider``, else
  1062. False.
  1063. """
  1064. rlist = self.reqts[other]
  1065. unmatched = set()
  1066. for s in rlist:
  1067. matcher = self.get_matcher(s)
  1068. if not matcher.match(provider.version):
  1069. unmatched.add(s)
  1070. if unmatched:
  1071. # can't replace other with provider
  1072. problems.add(('cantreplace', provider, other,
  1073. frozenset(unmatched)))
  1074. result = False
  1075. else:
  1076. # can replace other with provider
  1077. self.remove_distribution(other)
  1078. del self.reqts[other]
  1079. for s in rlist:
  1080. self.reqts.setdefault(provider, set()).add(s)
  1081. self.add_distribution(provider)
  1082. result = True
  1083. return result
  1084. def find(self, requirement, meta_extras=None, prereleases=False):
  1085. """
  1086. Find a distribution and all distributions it depends on.
  1087. :param requirement: The requirement specifying the distribution to
  1088. find, or a Distribution instance.
  1089. :param meta_extras: A list of meta extras such as :test:, :build: and
  1090. so on.
  1091. :param prereleases: If ``True``, allow pre-release versions to be
  1092. returned - otherwise, don't return prereleases
  1093. unless they're all that's available.
  1094. Return a set of :class:`Distribution` instances and a set of
  1095. problems.
  1096. The distributions returned should be such that they have the
  1097. :attr:`required` attribute set to ``True`` if they were
  1098. from the ``requirement`` passed to ``find()``, and they have the
  1099. :attr:`build_time_dependency` attribute set to ``True`` unless they
  1100. are post-installation dependencies of the ``requirement``.
  1101. The problems should be a tuple consisting of the string
  1102. ``'unsatisfied'`` and the requirement which couldn't be satisfied
  1103. by any distribution known to the locator.
  1104. """
  1105. self.provided = {}
  1106. self.dists = {}
  1107. self.dists_by_name = {}
  1108. self.reqts = {}
  1109. meta_extras = set(meta_extras or [])
  1110. if ':*:' in meta_extras:
  1111. meta_extras.remove(':*:')
  1112. # :meta: and :run: are implicitly included
  1113. meta_extras |= set([':test:', ':build:', ':dev:'])
  1114. if isinstance(requirement, Distribution):
  1115. dist = odist = requirement
  1116. logger.debug('passed %s as requirement', odist)
  1117. else:
  1118. dist = odist = self.locator.locate(requirement,
  1119. prereleases=prereleases)
  1120. if dist is None:
  1121. raise DistlibException('Unable to locate %r' % requirement)
  1122. logger.debug('located %s', odist)
  1123. dist.requested = True
  1124. problems = set()
  1125. todo = set([dist])
  1126. install_dists = set([odist])
  1127. while todo:
  1128. dist = todo.pop()
  1129. name = dist.key # case-insensitive
  1130. if name not in self.dists_by_name:
  1131. self.add_distribution(dist)
  1132. else:
  1133. #import pdb; pdb.set_trace()
  1134. other = self.dists_by_name[name]
  1135. if other != dist:
  1136. self.try_to_replace(dist, other, problems)
  1137. ireqts = dist.run_requires | dist.meta_requires
  1138. sreqts = dist.build_requires
  1139. ereqts = set()
  1140. if meta_extras and dist in install_dists:
  1141. for key in ('test', 'build', 'dev'):
  1142. e = ':%s:' % key
  1143. if e in meta_extras:
  1144. ereqts |= getattr(dist, '%s_requires' % key)
  1145. all_reqts = ireqts | sreqts | ereqts
  1146. for r in all_reqts:
  1147. providers = self.find_providers(r)
  1148. if not providers:
  1149. logger.debug('No providers found for %r', r)
  1150. provider = self.locator.locate(r, prereleases=prereleases)
  1151. # If no provider is found and we didn't consider
  1152. # prereleases, consider them now.
  1153. if provider is None and not prereleases:
  1154. provider = self.locator.locate(r, prereleases=True)
  1155. if provider is None:
  1156. logger.debug('Cannot satisfy %r', r)
  1157. problems.add(('unsatisfied', r))
  1158. else:
  1159. n, v = provider.key, provider.version
  1160. if (n, v) not in self.dists:
  1161. todo.add(provider)
  1162. providers.add(provider)
  1163. if r in ireqts and dist in install_dists:
  1164. install_dists.add(provider)
  1165. logger.debug('Adding %s to install_dists',
  1166. provider.name_and_version)
  1167. for p in providers:
  1168. name = p.key
  1169. if name not in self.dists_by_name:
  1170. self.reqts.setdefault(p, set()).add(r)
  1171. else:
  1172. other = self.dists_by_name[name]
  1173. if other != p:
  1174. # see if other can be replaced by p
  1175. self.try_to_replace(p, other, problems)
  1176. dists = set(self.dists.values())
  1177. for dist in dists:
  1178. dist.build_time_dependency = dist not in install_dists
  1179. if dist.build_time_dependency:
  1180. logger.debug('%s is a build-time dependency only.',
  1181. dist.name_and_version)
  1182. logger.debug('find done for %s', odist)
  1183. return dists, problems