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

/env/lib/python3.3/site-packages/setuptools/package_index.py

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