package_index.py 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. """PyPI and direct package downloading"""
  2. import sys
  3. import os
  4. import re
  5. import io
  6. import shutil
  7. import socket
  8. import base64
  9. import hashlib
  10. import itertools
  11. import warnings
  12. import configparser
  13. import html
  14. import http.client
  15. import urllib.parse
  16. import urllib.request
  17. import urllib.error
  18. from functools import wraps
  19. import setuptools
  20. from pkg_resources import (
  21. CHECKOUT_DIST, Distribution, BINARY_DIST, normalize_path, SOURCE_DIST,
  22. Environment, find_distributions, safe_name, safe_version,
  23. to_filename, Requirement, DEVELOP_DIST, EGG_DIST,
  24. )
  25. from setuptools import ssl_support
  26. from distutils import log
  27. from distutils.errors import DistutilsError
  28. from fnmatch import translate
  29. from setuptools.wheel import Wheel
  30. EGG_FRAGMENT = re.compile(r'^egg=([-A-Za-z0-9_.+!]+)$')
  31. HREF = re.compile(r"""href\s*=\s*['"]?([^'"> ]+)""", re.I)
  32. PYPI_MD5 = re.compile(
  33. r'<a href="([^"#]+)">([^<]+)</a>\n\s+\(<a (?:title="MD5 hash"\n\s+)'
  34. r'href="[^?]+\?:action=show_md5&amp;digest=([0-9a-f]{32})">md5</a>\)'
  35. )
  36. URL_SCHEME = re.compile('([-+.a-z0-9]{2,}):', re.I).match
  37. EXTENSIONS = ".tar.gz .tar.bz2 .tar .zip .tgz".split()
  38. __all__ = [
  39. 'PackageIndex', 'distros_for_url', 'parse_bdist_wininst',
  40. 'interpret_distro_name',
  41. ]
  42. _SOCKET_TIMEOUT = 15
  43. _tmpl = "setuptools/{setuptools.__version__} Python-urllib/{py_major}"
  44. user_agent = _tmpl.format(
  45. py_major='{}.{}'.format(*sys.version_info), setuptools=setuptools)
  46. def parse_requirement_arg(spec):
  47. try:
  48. return Requirement.parse(spec)
  49. except ValueError as e:
  50. raise DistutilsError(
  51. "Not a URL, existing file, or requirement spec: %r" % (spec,)
  52. ) from e
  53. def parse_bdist_wininst(name):
  54. """Return (base,pyversion) or (None,None) for possible .exe name"""
  55. lower = name.lower()
  56. base, py_ver, plat = None, None, None
  57. if lower.endswith('.exe'):
  58. if lower.endswith('.win32.exe'):
  59. base = name[:-10]
  60. plat = 'win32'
  61. elif lower.startswith('.win32-py', -16):
  62. py_ver = name[-7:-4]
  63. base = name[:-16]
  64. plat = 'win32'
  65. elif lower.endswith('.win-amd64.exe'):
  66. base = name[:-14]
  67. plat = 'win-amd64'
  68. elif lower.startswith('.win-amd64-py', -20):
  69. py_ver = name[-7:-4]
  70. base = name[:-20]
  71. plat = 'win-amd64'
  72. return base, py_ver, plat
  73. def egg_info_for_url(url):
  74. parts = urllib.parse.urlparse(url)
  75. scheme, server, path, parameters, query, fragment = parts
  76. base = urllib.parse.unquote(path.split('/')[-1])
  77. if server == 'sourceforge.net' and base == 'download': # XXX Yuck
  78. base = urllib.parse.unquote(path.split('/')[-2])
  79. if '#' in base:
  80. base, fragment = base.split('#', 1)
  81. return base, fragment
  82. def distros_for_url(url, metadata=None):
  83. """Yield egg or source distribution objects that might be found at a URL"""
  84. base, fragment = egg_info_for_url(url)
  85. for dist in distros_for_location(url, base, metadata):
  86. yield dist
  87. if fragment:
  88. match = EGG_FRAGMENT.match(fragment)
  89. if match:
  90. for dist in interpret_distro_name(
  91. url, match.group(1), metadata, precedence=CHECKOUT_DIST
  92. ):
  93. yield dist
  94. def distros_for_location(location, basename, metadata=None):
  95. """Yield egg or source distribution objects based on basename"""
  96. if basename.endswith('.egg.zip'):
  97. basename = basename[:-4] # strip the .zip
  98. if basename.endswith('.egg') and '-' in basename:
  99. # only one, unambiguous interpretation
  100. return [Distribution.from_location(location, basename, metadata)]
  101. if basename.endswith('.whl') and '-' in basename:
  102. wheel = Wheel(basename)
  103. if not wheel.is_compatible():
  104. return []
  105. return [Distribution(
  106. location=location,
  107. project_name=wheel.project_name,
  108. version=wheel.version,
  109. # Increase priority over eggs.
  110. precedence=EGG_DIST + 1,
  111. )]
  112. if basename.endswith('.exe'):
  113. win_base, py_ver, platform = parse_bdist_wininst(basename)
  114. if win_base is not None:
  115. return interpret_distro_name(
  116. location, win_base, metadata, py_ver, BINARY_DIST, platform
  117. )
  118. # Try source distro extensions (.zip, .tgz, etc.)
  119. #
  120. for ext in EXTENSIONS:
  121. if basename.endswith(ext):
  122. basename = basename[:-len(ext)]
  123. return interpret_distro_name(location, basename, metadata)
  124. return [] # no extension matched
  125. def distros_for_filename(filename, metadata=None):
  126. """Yield possible egg or source distribution objects based on a filename"""
  127. return distros_for_location(
  128. normalize_path(filename), os.path.basename(filename), metadata
  129. )
  130. def interpret_distro_name(
  131. location, basename, metadata, py_version=None, precedence=SOURCE_DIST,
  132. platform=None
  133. ):
  134. """Generate alternative interpretations of a source distro name
  135. Note: if `location` is a filesystem filename, you should call
  136. ``pkg_resources.normalize_path()`` on it before passing it to this
  137. routine!
  138. """
  139. # Generate alternative interpretations of a source distro name
  140. # Because some packages are ambiguous as to name/versions split
  141. # e.g. "adns-python-1.1.0", "egenix-mx-commercial", etc.
  142. # So, we generate each possible interepretation (e.g. "adns, python-1.1.0"
  143. # "adns-python, 1.1.0", and "adns-python-1.1.0, no version"). In practice,
  144. # the spurious interpretations should be ignored, because in the event
  145. # there's also an "adns" package, the spurious "python-1.1.0" version will
  146. # compare lower than any numeric version number, and is therefore unlikely
  147. # to match a request for it. It's still a potential problem, though, and
  148. # in the long run PyPI and the distutils should go for "safe" names and
  149. # versions in distribution archive names (sdist and bdist).
  150. parts = basename.split('-')
  151. if not py_version and any(re.match(r'py\d\.\d$', p) for p in parts[2:]):
  152. # it is a bdist_dumb, not an sdist -- bail out
  153. return
  154. for p in range(1, len(parts) + 1):
  155. yield Distribution(
  156. location, metadata, '-'.join(parts[:p]), '-'.join(parts[p:]),
  157. py_version=py_version, precedence=precedence,
  158. platform=platform
  159. )
  160. # From Python 2.7 docs
  161. def unique_everseen(iterable, key=None):
  162. "List unique elements, preserving order. Remember all elements ever seen."
  163. # unique_everseen('AAAABBBCCDAABBB') --> A B C D
  164. # unique_everseen('ABBCcAD', str.lower) --> A B C D
  165. seen = set()
  166. seen_add = seen.add
  167. if key is None:
  168. for element in itertools.filterfalse(seen.__contains__, iterable):
  169. seen_add(element)
  170. yield element
  171. else:
  172. for element in iterable:
  173. k = key(element)
  174. if k not in seen:
  175. seen_add(k)
  176. yield element
  177. def unique_values(func):
  178. """
  179. Wrap a function returning an iterable such that the resulting iterable
  180. only ever yields unique items.
  181. """
  182. @wraps(func)
  183. def wrapper(*args, **kwargs):
  184. return unique_everseen(func(*args, **kwargs))
  185. return wrapper
  186. REL = re.compile(r"""<([^>]*\srel\s*=\s*['"]?([^'">]+)[^>]*)>""", re.I)
  187. # this line is here to fix emacs' cruddy broken syntax highlighting
  188. @unique_values
  189. def find_external_links(url, page):
  190. """Find rel="homepage" and rel="download" links in `page`, yielding URLs"""
  191. for match in REL.finditer(page):
  192. tag, rel = match.groups()
  193. rels = set(map(str.strip, rel.lower().split(',')))
  194. if 'homepage' in rels or 'download' in rels:
  195. for match in HREF.finditer(tag):
  196. yield urllib.parse.urljoin(url, htmldecode(match.group(1)))
  197. for tag in ("<th>Home Page", "<th>Download URL"):
  198. pos = page.find(tag)
  199. if pos != -1:
  200. match = HREF.search(page, pos)
  201. if match:
  202. yield urllib.parse.urljoin(url, htmldecode(match.group(1)))
  203. class ContentChecker:
  204. """
  205. A null content checker that defines the interface for checking content
  206. """
  207. def feed(self, block):
  208. """
  209. Feed a block of data to the hash.
  210. """
  211. return
  212. def is_valid(self):
  213. """
  214. Check the hash. Return False if validation fails.
  215. """
  216. return True
  217. def report(self, reporter, template):
  218. """
  219. Call reporter with information about the checker (hash name)
  220. substituted into the template.
  221. """
  222. return
  223. class HashChecker(ContentChecker):
  224. pattern = re.compile(
  225. r'(?P<hash_name>sha1|sha224|sha384|sha256|sha512|md5)='
  226. r'(?P<expected>[a-f0-9]+)'
  227. )
  228. def __init__(self, hash_name, expected):
  229. self.hash_name = hash_name
  230. self.hash = hashlib.new(hash_name)
  231. self.expected = expected
  232. @classmethod
  233. def from_url(cls, url):
  234. "Construct a (possibly null) ContentChecker from a URL"
  235. fragment = urllib.parse.urlparse(url)[-1]
  236. if not fragment:
  237. return ContentChecker()
  238. match = cls.pattern.search(fragment)
  239. if not match:
  240. return ContentChecker()
  241. return cls(**match.groupdict())
  242. def feed(self, block):
  243. self.hash.update(block)
  244. def is_valid(self):
  245. return self.hash.hexdigest() == self.expected
  246. def report(self, reporter, template):
  247. msg = template % self.hash_name
  248. return reporter(msg)
  249. class PackageIndex(Environment):
  250. """A distribution index that scans web pages for download URLs"""
  251. def __init__(
  252. self, index_url="https://pypi.org/simple/", hosts=('*',),
  253. ca_bundle=None, verify_ssl=True, *args, **kw
  254. ):
  255. Environment.__init__(self, *args, **kw)
  256. self.index_url = index_url + "/" [:not index_url.endswith('/')]
  257. self.scanned_urls = {}
  258. self.fetched_urls = {}
  259. self.package_pages = {}
  260. self.allows = re.compile('|'.join(map(translate, hosts))).match
  261. self.to_scan = []
  262. use_ssl = (
  263. verify_ssl
  264. and ssl_support.is_available
  265. and (ca_bundle or ssl_support.find_ca_bundle())
  266. )
  267. if use_ssl:
  268. self.opener = ssl_support.opener_for(ca_bundle)
  269. else:
  270. self.opener = urllib.request.urlopen
  271. def process_url(self, url, retrieve=False):
  272. """Evaluate a URL as a possible download, and maybe retrieve it"""
  273. if url in self.scanned_urls and not retrieve:
  274. return
  275. self.scanned_urls[url] = True
  276. if not URL_SCHEME(url):
  277. self.process_filename(url)
  278. return
  279. else:
  280. dists = list(distros_for_url(url))
  281. if dists:
  282. if not self.url_ok(url):
  283. return
  284. self.debug("Found link: %s", url)
  285. if dists or not retrieve or url in self.fetched_urls:
  286. list(map(self.add, dists))
  287. return # don't need the actual page
  288. if not self.url_ok(url):
  289. self.fetched_urls[url] = True
  290. return
  291. self.info("Reading %s", url)
  292. self.fetched_urls[url] = True # prevent multiple fetch attempts
  293. tmpl = "Download error on %s: %%s -- Some packages may not be found!"
  294. f = self.open_url(url, tmpl % url)
  295. if f is None:
  296. return
  297. if isinstance(f, urllib.error.HTTPError) and f.code == 401:
  298. self.info("Authentication error: %s" % f.msg)
  299. self.fetched_urls[f.url] = True
  300. if 'html' not in f.headers.get('content-type', '').lower():
  301. f.close() # not html, we can't process it
  302. return
  303. base = f.url # handle redirects
  304. page = f.read()
  305. if not isinstance(page, str):
  306. # In Python 3 and got bytes but want str.
  307. if isinstance(f, urllib.error.HTTPError):
  308. # Errors have no charset, assume latin1:
  309. charset = 'latin-1'
  310. else:
  311. charset = f.headers.get_param('charset') or 'latin-1'
  312. page = page.decode(charset, "ignore")
  313. f.close()
  314. for match in HREF.finditer(page):
  315. link = urllib.parse.urljoin(base, htmldecode(match.group(1)))
  316. self.process_url(link)
  317. if url.startswith(self.index_url) and getattr(f, 'code', None) != 404:
  318. page = self.process_index(url, page)
  319. def process_filename(self, fn, nested=False):
  320. # process filenames or directories
  321. if not os.path.exists(fn):
  322. self.warn("Not found: %s", fn)
  323. return
  324. if os.path.isdir(fn) and not nested:
  325. path = os.path.realpath(fn)
  326. for item in os.listdir(path):
  327. self.process_filename(os.path.join(path, item), True)
  328. dists = distros_for_filename(fn)
  329. if dists:
  330. self.debug("Found: %s", fn)
  331. list(map(self.add, dists))
  332. def url_ok(self, url, fatal=False):
  333. s = URL_SCHEME(url)
  334. is_file = s and s.group(1).lower() == 'file'
  335. if is_file or self.allows(urllib.parse.urlparse(url)[1]):
  336. return True
  337. msg = (
  338. "\nNote: Bypassing %s (disallowed host; see "
  339. "http://bit.ly/2hrImnY for details).\n")
  340. if fatal:
  341. raise DistutilsError(msg % url)
  342. else:
  343. self.warn(msg, url)
  344. def scan_egg_links(self, search_path):
  345. dirs = filter(os.path.isdir, search_path)
  346. egg_links = (
  347. (path, entry)
  348. for path in dirs
  349. for entry in os.listdir(path)
  350. if entry.endswith('.egg-link')
  351. )
  352. list(itertools.starmap(self.scan_egg_link, egg_links))
  353. def scan_egg_link(self, path, entry):
  354. with open(os.path.join(path, entry)) as raw_lines:
  355. # filter non-empty lines
  356. lines = list(filter(None, map(str.strip, raw_lines)))
  357. if len(lines) != 2:
  358. # format is not recognized; punt
  359. return
  360. egg_path, setup_path = lines
  361. for dist in find_distributions(os.path.join(path, egg_path)):
  362. dist.location = os.path.join(path, *lines)
  363. dist.precedence = SOURCE_DIST
  364. self.add(dist)
  365. def process_index(self, url, page):
  366. """Process the contents of a PyPI page"""
  367. def scan(link):
  368. # Process a URL to see if it's for a package page
  369. if link.startswith(self.index_url):
  370. parts = list(map(
  371. urllib.parse.unquote, link[len(self.index_url):].split('/')
  372. ))
  373. if len(parts) == 2 and '#' not in parts[1]:
  374. # it's a package page, sanitize and index it
  375. pkg = safe_name(parts[0])
  376. ver = safe_version(parts[1])
  377. self.package_pages.setdefault(pkg.lower(), {})[link] = True
  378. return to_filename(pkg), to_filename(ver)
  379. return None, None
  380. # process an index page into the package-page index
  381. for match in HREF.finditer(page):
  382. try:
  383. scan(urllib.parse.urljoin(url, htmldecode(match.group(1))))
  384. except ValueError:
  385. pass
  386. pkg, ver = scan(url) # ensure this page is in the page index
  387. if pkg:
  388. # process individual package page
  389. for new_url in find_external_links(url, page):
  390. # Process the found URL
  391. base, frag = egg_info_for_url(new_url)
  392. if base.endswith('.py') and not frag:
  393. if ver:
  394. new_url += '#egg=%s-%s' % (pkg, ver)
  395. else:
  396. self.need_version_info(url)
  397. self.scan_url(new_url)
  398. return PYPI_MD5.sub(
  399. lambda m: '<a href="%s#md5=%s">%s</a>' % m.group(1, 3, 2), page
  400. )
  401. else:
  402. return "" # no sense double-scanning non-package pages
  403. def need_version_info(self, url):
  404. self.scan_all(
  405. "Page at %s links to .py file(s) without version info; an index "
  406. "scan is required.", url
  407. )
  408. def scan_all(self, msg=None, *args):
  409. if self.index_url not in self.fetched_urls:
  410. if msg:
  411. self.warn(msg, *args)
  412. self.info(
  413. "Scanning index of all packages (this may take a while)"
  414. )
  415. self.scan_url(self.index_url)
  416. def find_packages(self, requirement):
  417. self.scan_url(self.index_url + requirement.unsafe_name + '/')
  418. if not self.package_pages.get(requirement.key):
  419. # Fall back to safe version of the name
  420. self.scan_url(self.index_url + requirement.project_name + '/')
  421. if not self.package_pages.get(requirement.key):
  422. # We couldn't find the target package, so search the index page too
  423. self.not_found_in_index(requirement)
  424. for url in list(self.package_pages.get(requirement.key, ())):
  425. # scan each page that might be related to the desired package
  426. self.scan_url(url)
  427. def obtain(self, requirement, installer=None):
  428. self.prescan()
  429. self.find_packages(requirement)
  430. for dist in self[requirement.key]:
  431. if dist in requirement:
  432. return dist
  433. self.debug("%s does not match %s", requirement, dist)
  434. return super(PackageIndex, self).obtain(requirement, installer)
  435. def check_hash(self, checker, filename, tfp):
  436. """
  437. checker is a ContentChecker
  438. """
  439. checker.report(
  440. self.debug,
  441. "Validating %%s checksum for %s" % filename)
  442. if not checker.is_valid():
  443. tfp.close()
  444. os.unlink(filename)
  445. raise DistutilsError(
  446. "%s validation failed for %s; "
  447. "possible download problem?"
  448. % (checker.hash.name, os.path.basename(filename))
  449. )
  450. def add_find_links(self, urls):
  451. """Add `urls` to the list that will be prescanned for searches"""
  452. for url in urls:
  453. if (
  454. self.to_scan is None # if we have already "gone online"
  455. or not URL_SCHEME(url) # or it's a local file/directory
  456. or url.startswith('file:')
  457. or list(distros_for_url(url)) # or a direct package link
  458. ):
  459. # then go ahead and process it now
  460. self.scan_url(url)
  461. else:
  462. # otherwise, defer retrieval till later
  463. self.to_scan.append(url)
  464. def prescan(self):
  465. """Scan urls scheduled for prescanning (e.g. --find-links)"""
  466. if self.to_scan:
  467. list(map(self.scan_url, self.to_scan))
  468. self.to_scan = None # from now on, go ahead and process immediately
  469. def not_found_in_index(self, requirement):
  470. if self[requirement.key]: # we've seen at least one distro
  471. meth, msg = self.info, "Couldn't retrieve index page for %r"
  472. else: # no distros seen for this name, might be misspelled
  473. meth, msg = (
  474. self.warn,
  475. "Couldn't find index page for %r (maybe misspelled?)")
  476. meth(msg, requirement.unsafe_name)
  477. self.scan_all()
  478. def download(self, spec, tmpdir):
  479. """Locate and/or download `spec` to `tmpdir`, returning a local path
  480. `spec` may be a ``Requirement`` object, or a string containing a URL,
  481. an existing local filename, or a project/version requirement spec
  482. (i.e. the string form of a ``Requirement`` object). If it is the URL
  483. of a .py file with an unambiguous ``#egg=name-version`` tag (i.e., one
  484. that escapes ``-`` as ``_`` throughout), a trivial ``setup.py`` is
  485. automatically created alongside the downloaded file.
  486. If `spec` is a ``Requirement`` object or a string containing a
  487. project/version requirement spec, this method returns the location of
  488. a matching distribution (possibly after downloading it to `tmpdir`).
  489. If `spec` is a locally existing file or directory name, it is simply
  490. returned unchanged. If `spec` is a URL, it is downloaded to a subpath
  491. of `tmpdir`, and the local filename is returned. Various errors may be
  492. raised if a problem occurs during downloading.
  493. """
  494. if not isinstance(spec, Requirement):
  495. scheme = URL_SCHEME(spec)
  496. if scheme:
  497. # It's a url, download it to tmpdir
  498. found = self._download_url(scheme.group(1), spec, tmpdir)
  499. base, fragment = egg_info_for_url(spec)
  500. if base.endswith('.py'):
  501. found = self.gen_setup(found, fragment, tmpdir)
  502. return found
  503. elif os.path.exists(spec):
  504. # Existing file or directory, just return it
  505. return spec
  506. else:
  507. spec = parse_requirement_arg(spec)
  508. return getattr(self.fetch_distribution(spec, tmpdir), 'location', None)
  509. def fetch_distribution(
  510. self, requirement, tmpdir, force_scan=False, source=False,
  511. develop_ok=False, local_index=None):
  512. """Obtain a distribution suitable for fulfilling `requirement`
  513. `requirement` must be a ``pkg_resources.Requirement`` instance.
  514. If necessary, or if the `force_scan` flag is set, the requirement is
  515. searched for in the (online) package index as well as the locally
  516. installed packages. If a distribution matching `requirement` is found,
  517. the returned distribution's ``location`` is the value you would have
  518. gotten from calling the ``download()`` method with the matching
  519. distribution's URL or filename. If no matching distribution is found,
  520. ``None`` is returned.
  521. If the `source` flag is set, only source distributions and source
  522. checkout links will be considered. Unless the `develop_ok` flag is
  523. set, development and system eggs (i.e., those using the ``.egg-info``
  524. format) will be ignored.
  525. """
  526. # process a Requirement
  527. self.info("Searching for %s", requirement)
  528. skipped = {}
  529. dist = None
  530. def find(req, env=None):
  531. if env is None:
  532. env = self
  533. # Find a matching distribution; may be called more than once
  534. for dist in env[req.key]:
  535. if dist.precedence == DEVELOP_DIST and not develop_ok:
  536. if dist not in skipped:
  537. self.warn(
  538. "Skipping development or system egg: %s", dist,
  539. )
  540. skipped[dist] = 1
  541. continue
  542. test = (
  543. dist in req
  544. and (dist.precedence <= SOURCE_DIST or not source)
  545. )
  546. if test:
  547. loc = self.download(dist.location, tmpdir)
  548. dist.download_location = loc
  549. if os.path.exists(dist.download_location):
  550. return dist
  551. if force_scan:
  552. self.prescan()
  553. self.find_packages(requirement)
  554. dist = find(requirement)
  555. if not dist and local_index is not None:
  556. dist = find(requirement, local_index)
  557. if dist is None:
  558. if self.to_scan is not None:
  559. self.prescan()
  560. dist = find(requirement)
  561. if dist is None and not force_scan:
  562. self.find_packages(requirement)
  563. dist = find(requirement)
  564. if dist is None:
  565. self.warn(
  566. "No local packages or working download links found for %s%s",
  567. (source and "a source distribution of " or ""),
  568. requirement,
  569. )
  570. else:
  571. self.info("Best match: %s", dist)
  572. return dist.clone(location=dist.download_location)
  573. def fetch(self, requirement, tmpdir, force_scan=False, source=False):
  574. """Obtain a file suitable for fulfilling `requirement`
  575. DEPRECATED; use the ``fetch_distribution()`` method now instead. For
  576. backward compatibility, this routine is identical but returns the
  577. ``location`` of the downloaded distribution instead of a distribution
  578. object.
  579. """
  580. dist = self.fetch_distribution(requirement, tmpdir, force_scan, source)
  581. if dist is not None:
  582. return dist.location
  583. return None
  584. def gen_setup(self, filename, fragment, tmpdir):
  585. match = EGG_FRAGMENT.match(fragment)
  586. dists = match and [
  587. d for d in
  588. interpret_distro_name(filename, match.group(1), None) if d.version
  589. ] or []
  590. if len(dists) == 1: # unambiguous ``#egg`` fragment
  591. basename = os.path.basename(filename)
  592. # Make sure the file has been downloaded to the temp dir.
  593. if os.path.dirname(filename) != tmpdir:
  594. dst = os.path.join(tmpdir, basename)
  595. from setuptools.command.easy_install import samefile
  596. if not samefile(filename, dst):
  597. shutil.copy2(filename, dst)
  598. filename = dst
  599. with open(os.path.join(tmpdir, 'setup.py'), 'w') as file:
  600. file.write(
  601. "from setuptools import setup\n"
  602. "setup(name=%r, version=%r, py_modules=[%r])\n"
  603. % (
  604. dists[0].project_name, dists[0].version,
  605. os.path.splitext(basename)[0]
  606. )
  607. )
  608. return filename
  609. elif match:
  610. raise DistutilsError(
  611. "Can't unambiguously interpret project/version identifier %r; "
  612. "any dashes in the name or version should be escaped using "
  613. "underscores. %r" % (fragment, dists)
  614. )
  615. else:
  616. raise DistutilsError(
  617. "Can't process plain .py files without an '#egg=name-version'"
  618. " suffix to enable automatic setup script generation."
  619. )
  620. dl_blocksize = 8192
  621. def _download_to(self, url, filename):
  622. self.info("Downloading %s", url)
  623. # Download the file
  624. fp = None
  625. try:
  626. checker = HashChecker.from_url(url)
  627. fp = self.open_url(url)
  628. if isinstance(fp, urllib.error.HTTPError):
  629. raise DistutilsError(
  630. "Can't download %s: %s %s" % (url, fp.code, fp.msg)
  631. )
  632. headers = fp.info()
  633. blocknum = 0
  634. bs = self.dl_blocksize
  635. size = -1
  636. if "content-length" in headers:
  637. # Some servers return multiple Content-Length headers :(
  638. sizes = headers.get_all('Content-Length')
  639. size = max(map(int, sizes))
  640. self.reporthook(url, filename, blocknum, bs, size)
  641. with open(filename, 'wb') as tfp:
  642. while True:
  643. block = fp.read(bs)
  644. if block:
  645. checker.feed(block)
  646. tfp.write(block)
  647. blocknum += 1
  648. self.reporthook(url, filename, blocknum, bs, size)
  649. else:
  650. break
  651. self.check_hash(checker, filename, tfp)
  652. return headers
  653. finally:
  654. if fp:
  655. fp.close()
  656. def reporthook(self, url, filename, blocknum, blksize, size):
  657. pass # no-op
  658. def open_url(self, url, warning=None):
  659. if url.startswith('file:'):
  660. return local_open(url)
  661. try:
  662. return open_with_auth(url, self.opener)
  663. except (ValueError, http.client.InvalidURL) as v:
  664. msg = ' '.join([str(arg) for arg in v.args])
  665. if warning:
  666. self.warn(warning, msg)
  667. else:
  668. raise DistutilsError('%s %s' % (url, msg)) from v
  669. except urllib.error.HTTPError as v:
  670. return v
  671. except urllib.error.URLError as v:
  672. if warning:
  673. self.warn(warning, v.reason)
  674. else:
  675. raise DistutilsError("Download error for %s: %s"
  676. % (url, v.reason)) from v
  677. except http.client.BadStatusLine as v:
  678. if warning:
  679. self.warn(warning, v.line)
  680. else:
  681. raise DistutilsError(
  682. '%s returned a bad status line. The server might be '
  683. 'down, %s' %
  684. (url, v.line)
  685. ) from v
  686. except (http.client.HTTPException, socket.error) as v:
  687. if warning:
  688. self.warn(warning, v)
  689. else:
  690. raise DistutilsError("Download error for %s: %s"
  691. % (url, v)) from v
  692. def _download_url(self, scheme, url, tmpdir):
  693. # Determine download filename
  694. #
  695. name, fragment = egg_info_for_url(url)
  696. if name:
  697. while '..' in name:
  698. name = name.replace('..', '.').replace('\\', '_')
  699. else:
  700. name = "__downloaded__" # default if URL has no path contents
  701. if name.endswith('.egg.zip'):
  702. name = name[:-4] # strip the extra .zip before download
  703. filename = os.path.join(tmpdir, name)
  704. # Download the file
  705. #
  706. if scheme == 'svn' or scheme.startswith('svn+'):
  707. return self._download_svn(url, filename)
  708. elif scheme == 'git' or scheme.startswith('git+'):
  709. return self._download_git(url, filename)
  710. elif scheme.startswith('hg+'):
  711. return self._download_hg(url, filename)
  712. elif scheme == 'file':
  713. return urllib.request.url2pathname(urllib.parse.urlparse(url)[2])
  714. else:
  715. self.url_ok(url, True) # raises error if not allowed
  716. return self._attempt_download(url, filename)
  717. def scan_url(self, url):
  718. self.process_url(url, True)
  719. def _attempt_download(self, url, filename):
  720. headers = self._download_to(url, filename)
  721. if 'html' in headers.get('content-type', '').lower():
  722. return self._download_html(url, headers, filename)
  723. else:
  724. return filename
  725. def _download_html(self, url, headers, filename):
  726. file = open(filename)
  727. for line in file:
  728. if line.strip():
  729. # Check for a subversion index page
  730. if re.search(r'<title>([^- ]+ - )?Revision \d+:', line):
  731. # it's a subversion index page:
  732. file.close()
  733. os.unlink(filename)
  734. return self._download_svn(url, filename)
  735. break # not an index page
  736. file.close()
  737. os.unlink(filename)
  738. raise DistutilsError("Unexpected HTML page found at " + url)
  739. def _download_svn(self, url, filename):
  740. warnings.warn("SVN download support is deprecated", UserWarning)
  741. url = url.split('#', 1)[0] # remove any fragment for svn's sake
  742. creds = ''
  743. if url.lower().startswith('svn:') and '@' in url:
  744. scheme, netloc, path, p, q, f = urllib.parse.urlparse(url)
  745. if not netloc and path.startswith('//') and '/' in path[2:]:
  746. netloc, path = path[2:].split('/', 1)
  747. auth, host = _splituser(netloc)
  748. if auth:
  749. if ':' in auth:
  750. user, pw = auth.split(':', 1)
  751. creds = " --username=%s --password=%s" % (user, pw)
  752. else:
  753. creds = " --username=" + auth
  754. netloc = host
  755. parts = scheme, netloc, url, p, q, f
  756. url = urllib.parse.urlunparse(parts)
  757. self.info("Doing subversion checkout from %s to %s", url, filename)
  758. os.system("svn checkout%s -q %s %s" % (creds, url, filename))
  759. return filename
  760. @staticmethod
  761. def _vcs_split_rev_from_url(url, pop_prefix=False):
  762. scheme, netloc, path, query, frag = urllib.parse.urlsplit(url)
  763. scheme = scheme.split('+', 1)[-1]
  764. # Some fragment identification fails
  765. path = path.split('#', 1)[0]
  766. rev = None
  767. if '@' in path:
  768. path, rev = path.rsplit('@', 1)
  769. # Also, discard fragment
  770. url = urllib.parse.urlunsplit((scheme, netloc, path, query, ''))
  771. return url, rev
  772. def _download_git(self, url, filename):
  773. filename = filename.split('#', 1)[0]
  774. url, rev = self._vcs_split_rev_from_url(url, pop_prefix=True)
  775. self.info("Doing git clone from %s to %s", url, filename)
  776. os.system("git clone --quiet %s %s" % (url, filename))
  777. if rev is not None:
  778. self.info("Checking out %s", rev)
  779. os.system("git -C %s checkout --quiet %s" % (
  780. filename,
  781. rev,
  782. ))
  783. return filename
  784. def _download_hg(self, url, filename):
  785. filename = filename.split('#', 1)[0]
  786. url, rev = self._vcs_split_rev_from_url(url, pop_prefix=True)
  787. self.info("Doing hg clone from %s to %s", url, filename)
  788. os.system("hg clone --quiet %s %s" % (url, filename))
  789. if rev is not None:
  790. self.info("Updating to %s", rev)
  791. os.system("hg --cwd %s up -C -r %s -q" % (
  792. filename,
  793. rev,
  794. ))
  795. return filename
  796. def debug(self, msg, *args):
  797. log.debug(msg, *args)
  798. def info(self, msg, *args):
  799. log.info(msg, *args)
  800. def warn(self, msg, *args):
  801. log.warn(msg, *args)
  802. # This pattern matches a character entity reference (a decimal numeric
  803. # references, a hexadecimal numeric reference, or a named reference).
  804. entity_sub = re.compile(r'&(#(\d+|x[\da-fA-F]+)|[\w.:-]+);?').sub
  805. def decode_entity(match):
  806. what = match.group(0)
  807. return html.unescape(what)
  808. def htmldecode(text):
  809. """
  810. Decode HTML entities in the given text.
  811. >>> htmldecode(
  812. ... 'https://../package_name-0.1.2.tar.gz'
  813. ... '?tokena=A&amp;tokenb=B">package_name-0.1.2.tar.gz')
  814. 'https://../package_name-0.1.2.tar.gz?tokena=A&tokenb=B">package_name-0.1.2.tar.gz'
  815. """
  816. return entity_sub(decode_entity, text)
  817. def socket_timeout(timeout=15):
  818. def _socket_timeout(func):
  819. def _socket_timeout(*args, **kwargs):
  820. old_timeout = socket.getdefaulttimeout()
  821. socket.setdefaulttimeout(timeout)
  822. try:
  823. return func(*args, **kwargs)
  824. finally:
  825. socket.setdefaulttimeout(old_timeout)
  826. return _socket_timeout
  827. return _socket_timeout
  828. def _encode_auth(auth):
  829. """
  830. Encode auth from a URL suitable for an HTTP header.
  831. >>> str(_encode_auth('username%3Apassword'))
  832. 'dXNlcm5hbWU6cGFzc3dvcmQ='
  833. Long auth strings should not cause a newline to be inserted.
  834. >>> long_auth = 'username:' + 'password'*10
  835. >>> chr(10) in str(_encode_auth(long_auth))
  836. False
  837. """
  838. auth_s = urllib.parse.unquote(auth)
  839. # convert to bytes
  840. auth_bytes = auth_s.encode()
  841. encoded_bytes = base64.b64encode(auth_bytes)
  842. # convert back to a string
  843. encoded = encoded_bytes.decode()
  844. # strip the trailing carriage return
  845. return encoded.replace('\n', '')
  846. class Credential:
  847. """
  848. A username/password pair. Use like a namedtuple.
  849. """
  850. def __init__(self, username, password):
  851. self.username = username
  852. self.password = password
  853. def __iter__(self):
  854. yield self.username
  855. yield self.password
  856. def __str__(self):
  857. return '%(username)s:%(password)s' % vars(self)
  858. class PyPIConfig(configparser.RawConfigParser):
  859. def __init__(self):
  860. """
  861. Load from ~/.pypirc
  862. """
  863. defaults = dict.fromkeys(['username', 'password', 'repository'], '')
  864. configparser.RawConfigParser.__init__(self, defaults)
  865. rc = os.path.join(os.path.expanduser('~'), '.pypirc')
  866. if os.path.exists(rc):
  867. self.read(rc)
  868. @property
  869. def creds_by_repository(self):
  870. sections_with_repositories = [
  871. section for section in self.sections()
  872. if self.get(section, 'repository').strip()
  873. ]
  874. return dict(map(self._get_repo_cred, sections_with_repositories))
  875. def _get_repo_cred(self, section):
  876. repo = self.get(section, 'repository').strip()
  877. return repo, Credential(
  878. self.get(section, 'username').strip(),
  879. self.get(section, 'password').strip(),
  880. )
  881. def find_credential(self, url):
  882. """
  883. If the URL indicated appears to be a repository defined in this
  884. config, return the credential for that repository.
  885. """
  886. for repository, cred in self.creds_by_repository.items():
  887. if url.startswith(repository):
  888. return cred
  889. def open_with_auth(url, opener=urllib.request.urlopen):
  890. """Open a urllib2 request, handling HTTP authentication"""
  891. parsed = urllib.parse.urlparse(url)
  892. scheme, netloc, path, params, query, frag = parsed
  893. # Double scheme does not raise on macOS as revealed by a
  894. # failing test. We would expect "nonnumeric port". Refs #20.
  895. if netloc.endswith(':'):
  896. raise http.client.InvalidURL("nonnumeric port: ''")
  897. if scheme in ('http', 'https'):
  898. auth, address = _splituser(netloc)
  899. else:
  900. auth = None
  901. if not auth:
  902. cred = PyPIConfig().find_credential(url)
  903. if cred:
  904. auth = str(cred)
  905. info = cred.username, url
  906. log.info('Authenticating as %s for %s (from .pypirc)', *info)
  907. if auth:
  908. auth = "Basic " + _encode_auth(auth)
  909. parts = scheme, address, path, params, query, frag
  910. new_url = urllib.parse.urlunparse(parts)
  911. request = urllib.request.Request(new_url)
  912. request.add_header("Authorization", auth)
  913. else:
  914. request = urllib.request.Request(url)
  915. request.add_header('User-Agent', user_agent)
  916. fp = opener(request)
  917. if auth:
  918. # Put authentication info back into request URL if same host,
  919. # so that links found on the page will work
  920. s2, h2, path2, param2, query2, frag2 = urllib.parse.urlparse(fp.url)
  921. if s2 == scheme and h2 == address:
  922. parts = s2, netloc, path2, param2, query2, frag2
  923. fp.url = urllib.parse.urlunparse(parts)
  924. return fp
  925. # copy of urllib.parse._splituser from Python 3.8
  926. def _splituser(host):
  927. """splituser('user[:passwd]@host[:port]')
  928. --> 'user[:passwd]', 'host[:port]'."""
  929. user, delim, host = host.rpartition('@')
  930. return (user if delim else None), host
  931. # adding a timeout to avoid freezing package_index
  932. open_with_auth = socket_timeout(_SOCKET_TIMEOUT)(open_with_auth)
  933. def fix_sf_url(url):
  934. return url # backward compatibility
  935. def local_open(url):
  936. """Read a local path, with special support for directories"""
  937. scheme, server, path, param, query, frag = urllib.parse.urlparse(url)
  938. filename = urllib.request.url2pathname(path)
  939. if os.path.isfile(filename):
  940. return urllib.request.urlopen(url)
  941. elif path.endswith('/') and os.path.isdir(filename):
  942. files = []
  943. for f in os.listdir(filename):
  944. filepath = os.path.join(filename, f)
  945. if f == 'index.html':
  946. with open(filepath, 'r') as fp:
  947. body = fp.read()
  948. break
  949. elif os.path.isdir(filepath):
  950. f += '/'
  951. files.append('<a href="{name}">{name}</a>'.format(name=f))
  952. else:
  953. tmpl = (
  954. "<html><head><title>{url}</title>"
  955. "</head><body>{files}</body></html>")
  956. body = tmpl.format(url=url, files='\n'.join(files))
  957. status, message = 200, "OK"
  958. else:
  959. status, message, body = 404, "Path not found", "Not found"
  960. headers = {'content-type': 'text/html'}
  961. body_stream = io.StringIO(body)
  962. return urllib.error.HTTPError(url, status, message, headers, body_stream)