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.

download.py 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. from __future__ import absolute_import
  2. import cgi
  3. import email.utils
  4. import getpass
  5. import json
  6. import logging
  7. import mimetypes
  8. import os
  9. import platform
  10. import re
  11. import shutil
  12. import sys
  13. from pip._vendor import requests, six, urllib3
  14. from pip._vendor.cachecontrol import CacheControlAdapter
  15. from pip._vendor.cachecontrol.caches import FileCache
  16. from pip._vendor.lockfile import LockError
  17. from pip._vendor.requests.adapters import BaseAdapter, HTTPAdapter
  18. from pip._vendor.requests.auth import AuthBase, HTTPBasicAuth
  19. from pip._vendor.requests.models import CONTENT_CHUNK_SIZE, Response
  20. from pip._vendor.requests.structures import CaseInsensitiveDict
  21. from pip._vendor.requests.utils import get_netrc_auth
  22. # NOTE: XMLRPC Client is not annotated in typeshed as on 2017-07-17, which is
  23. # why we ignore the type on this import
  24. from pip._vendor.six.moves import xmlrpc_client # type: ignore
  25. from pip._vendor.six.moves.urllib import parse as urllib_parse
  26. from pip._vendor.six.moves.urllib import request as urllib_request
  27. from pip._vendor.urllib3.util import IS_PYOPENSSL
  28. import pip
  29. from pip._internal.exceptions import HashMismatch, InstallationError
  30. from pip._internal.locations import write_delete_marker_file
  31. from pip._internal.models.index import PyPI
  32. from pip._internal.utils.encoding import auto_decode
  33. from pip._internal.utils.filesystem import check_path_owner
  34. from pip._internal.utils.glibc import libc_ver
  35. from pip._internal.utils.logging import indent_log
  36. from pip._internal.utils.misc import (
  37. ARCHIVE_EXTENSIONS, ask_path_exists, backup_dir, call_subprocess, consume,
  38. display_path, format_size, get_installed_version, rmtree,
  39. split_auth_from_netloc, splitext, unpack_file,
  40. )
  41. from pip._internal.utils.setuptools_build import SETUPTOOLS_SHIM
  42. from pip._internal.utils.temp_dir import TempDirectory
  43. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  44. from pip._internal.utils.ui import DownloadProgressProvider
  45. from pip._internal.vcs import vcs
  46. if MYPY_CHECK_RUNNING:
  47. from typing import ( # noqa: F401
  48. Optional, Tuple, Dict, IO, Text, Union
  49. )
  50. from pip._internal.models.link import Link # noqa: F401
  51. from pip._internal.utils.hashes import Hashes # noqa: F401
  52. from pip._internal.vcs import AuthInfo # noqa: F401
  53. try:
  54. import ssl # noqa
  55. except ImportError:
  56. ssl = None
  57. HAS_TLS = (ssl is not None) or IS_PYOPENSSL
  58. __all__ = ['get_file_content',
  59. 'is_url', 'url_to_path', 'path_to_url',
  60. 'is_archive_file', 'unpack_vcs_link',
  61. 'unpack_file_url', 'is_vcs_url', 'is_file_url',
  62. 'unpack_http_url', 'unpack_url']
  63. logger = logging.getLogger(__name__)
  64. def user_agent():
  65. """
  66. Return a string representing the user agent.
  67. """
  68. data = {
  69. "installer": {"name": "pip", "version": pip.__version__},
  70. "python": platform.python_version(),
  71. "implementation": {
  72. "name": platform.python_implementation(),
  73. },
  74. }
  75. if data["implementation"]["name"] == 'CPython':
  76. data["implementation"]["version"] = platform.python_version()
  77. elif data["implementation"]["name"] == 'PyPy':
  78. if sys.pypy_version_info.releaselevel == 'final':
  79. pypy_version_info = sys.pypy_version_info[:3]
  80. else:
  81. pypy_version_info = sys.pypy_version_info
  82. data["implementation"]["version"] = ".".join(
  83. [str(x) for x in pypy_version_info]
  84. )
  85. elif data["implementation"]["name"] == 'Jython':
  86. # Complete Guess
  87. data["implementation"]["version"] = platform.python_version()
  88. elif data["implementation"]["name"] == 'IronPython':
  89. # Complete Guess
  90. data["implementation"]["version"] = platform.python_version()
  91. if sys.platform.startswith("linux"):
  92. from pip._vendor import distro
  93. distro_infos = dict(filter(
  94. lambda x: x[1],
  95. zip(["name", "version", "id"], distro.linux_distribution()),
  96. ))
  97. libc = dict(filter(
  98. lambda x: x[1],
  99. zip(["lib", "version"], libc_ver()),
  100. ))
  101. if libc:
  102. distro_infos["libc"] = libc
  103. if distro_infos:
  104. data["distro"] = distro_infos
  105. if sys.platform.startswith("darwin") and platform.mac_ver()[0]:
  106. data["distro"] = {"name": "macOS", "version": platform.mac_ver()[0]}
  107. if platform.system():
  108. data.setdefault("system", {})["name"] = platform.system()
  109. if platform.release():
  110. data.setdefault("system", {})["release"] = platform.release()
  111. if platform.machine():
  112. data["cpu"] = platform.machine()
  113. if HAS_TLS:
  114. data["openssl_version"] = ssl.OPENSSL_VERSION
  115. setuptools_version = get_installed_version("setuptools")
  116. if setuptools_version is not None:
  117. data["setuptools_version"] = setuptools_version
  118. return "{data[installer][name]}/{data[installer][version]} {json}".format(
  119. data=data,
  120. json=json.dumps(data, separators=(",", ":"), sort_keys=True),
  121. )
  122. class MultiDomainBasicAuth(AuthBase):
  123. def __init__(self, prompting=True):
  124. # type: (bool) -> None
  125. self.prompting = prompting
  126. self.passwords = {} # type: Dict[str, AuthInfo]
  127. def __call__(self, req):
  128. parsed = urllib_parse.urlparse(req.url)
  129. # Split the credentials from the netloc.
  130. netloc, url_user_password = split_auth_from_netloc(parsed.netloc)
  131. # Set the url of the request to the url without any credentials
  132. req.url = urllib_parse.urlunparse(parsed[:1] + (netloc,) + parsed[2:])
  133. # Use any stored credentials that we have for this netloc
  134. username, password = self.passwords.get(netloc, (None, None))
  135. # Use the credentials embedded in the url if we have none stored
  136. if username is None:
  137. username, password = url_user_password
  138. # Get creds from netrc if we still don't have them
  139. if username is None and password is None:
  140. netrc_auth = get_netrc_auth(req.url)
  141. username, password = netrc_auth if netrc_auth else (None, None)
  142. if username or password:
  143. # Store the username and password
  144. self.passwords[netloc] = (username, password)
  145. # Send the basic auth with this request
  146. req = HTTPBasicAuth(username or "", password or "")(req)
  147. # Attach a hook to handle 401 responses
  148. req.register_hook("response", self.handle_401)
  149. return req
  150. def handle_401(self, resp, **kwargs):
  151. # We only care about 401 responses, anything else we want to just
  152. # pass through the actual response
  153. if resp.status_code != 401:
  154. return resp
  155. # We are not able to prompt the user so simply return the response
  156. if not self.prompting:
  157. return resp
  158. parsed = urllib_parse.urlparse(resp.url)
  159. # Prompt the user for a new username and password
  160. username = six.moves.input("User for %s: " % parsed.netloc)
  161. password = getpass.getpass("Password: ")
  162. # Store the new username and password to use for future requests
  163. if username or password:
  164. self.passwords[parsed.netloc] = (username, password)
  165. # Consume content and release the original connection to allow our new
  166. # request to reuse the same one.
  167. resp.content
  168. resp.raw.release_conn()
  169. # Add our new username and password to the request
  170. req = HTTPBasicAuth(username or "", password or "")(resp.request)
  171. req.register_hook("response", self.warn_on_401)
  172. # Send our new request
  173. new_resp = resp.connection.send(req, **kwargs)
  174. new_resp.history.append(resp)
  175. return new_resp
  176. def warn_on_401(self, resp, **kwargs):
  177. # warn user that they provided incorrect credentials
  178. if resp.status_code == 401:
  179. logger.warning('401 Error, Credentials not correct for %s',
  180. resp.request.url)
  181. class LocalFSAdapter(BaseAdapter):
  182. def send(self, request, stream=None, timeout=None, verify=None, cert=None,
  183. proxies=None):
  184. pathname = url_to_path(request.url)
  185. resp = Response()
  186. resp.status_code = 200
  187. resp.url = request.url
  188. try:
  189. stats = os.stat(pathname)
  190. except OSError as exc:
  191. resp.status_code = 404
  192. resp.raw = exc
  193. else:
  194. modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
  195. content_type = mimetypes.guess_type(pathname)[0] or "text/plain"
  196. resp.headers = CaseInsensitiveDict({
  197. "Content-Type": content_type,
  198. "Content-Length": stats.st_size,
  199. "Last-Modified": modified,
  200. })
  201. resp.raw = open(pathname, "rb")
  202. resp.close = resp.raw.close
  203. return resp
  204. def close(self):
  205. pass
  206. class SafeFileCache(FileCache):
  207. """
  208. A file based cache which is safe to use even when the target directory may
  209. not be accessible or writable.
  210. """
  211. def __init__(self, *args, **kwargs):
  212. super(SafeFileCache, self).__init__(*args, **kwargs)
  213. # Check to ensure that the directory containing our cache directory
  214. # is owned by the user current executing pip. If it does not exist
  215. # we will check the parent directory until we find one that does exist.
  216. # If it is not owned by the user executing pip then we will disable
  217. # the cache and log a warning.
  218. if not check_path_owner(self.directory):
  219. logger.warning(
  220. "The directory '%s' or its parent directory is not owned by "
  221. "the current user and the cache has been disabled. Please "
  222. "check the permissions and owner of that directory. If "
  223. "executing pip with sudo, you may want sudo's -H flag.",
  224. self.directory,
  225. )
  226. # Set our directory to None to disable the Cache
  227. self.directory = None
  228. def get(self, *args, **kwargs):
  229. # If we don't have a directory, then the cache should be a no-op.
  230. if self.directory is None:
  231. return
  232. try:
  233. return super(SafeFileCache, self).get(*args, **kwargs)
  234. except (LockError, OSError, IOError):
  235. # We intentionally silence this error, if we can't access the cache
  236. # then we can just skip caching and process the request as if
  237. # caching wasn't enabled.
  238. pass
  239. def set(self, *args, **kwargs):
  240. # If we don't have a directory, then the cache should be a no-op.
  241. if self.directory is None:
  242. return
  243. try:
  244. return super(SafeFileCache, self).set(*args, **kwargs)
  245. except (LockError, OSError, IOError):
  246. # We intentionally silence this error, if we can't access the cache
  247. # then we can just skip caching and process the request as if
  248. # caching wasn't enabled.
  249. pass
  250. def delete(self, *args, **kwargs):
  251. # If we don't have a directory, then the cache should be a no-op.
  252. if self.directory is None:
  253. return
  254. try:
  255. return super(SafeFileCache, self).delete(*args, **kwargs)
  256. except (LockError, OSError, IOError):
  257. # We intentionally silence this error, if we can't access the cache
  258. # then we can just skip caching and process the request as if
  259. # caching wasn't enabled.
  260. pass
  261. class InsecureHTTPAdapter(HTTPAdapter):
  262. def cert_verify(self, conn, url, verify, cert):
  263. conn.cert_reqs = 'CERT_NONE'
  264. conn.ca_certs = None
  265. class PipSession(requests.Session):
  266. timeout = None # type: Optional[int]
  267. def __init__(self, *args, **kwargs):
  268. retries = kwargs.pop("retries", 0)
  269. cache = kwargs.pop("cache", None)
  270. insecure_hosts = kwargs.pop("insecure_hosts", [])
  271. super(PipSession, self).__init__(*args, **kwargs)
  272. # Attach our User Agent to the request
  273. self.headers["User-Agent"] = user_agent()
  274. # Attach our Authentication handler to the session
  275. self.auth = MultiDomainBasicAuth()
  276. # Create our urllib3.Retry instance which will allow us to customize
  277. # how we handle retries.
  278. retries = urllib3.Retry(
  279. # Set the total number of retries that a particular request can
  280. # have.
  281. total=retries,
  282. # A 503 error from PyPI typically means that the Fastly -> Origin
  283. # connection got interrupted in some way. A 503 error in general
  284. # is typically considered a transient error so we'll go ahead and
  285. # retry it.
  286. # A 500 may indicate transient error in Amazon S3
  287. # A 520 or 527 - may indicate transient error in CloudFlare
  288. status_forcelist=[500, 503, 520, 527],
  289. # Add a small amount of back off between failed requests in
  290. # order to prevent hammering the service.
  291. backoff_factor=0.25,
  292. )
  293. # We want to _only_ cache responses on securely fetched origins. We do
  294. # this because we can't validate the response of an insecurely fetched
  295. # origin, and we don't want someone to be able to poison the cache and
  296. # require manual eviction from the cache to fix it.
  297. if cache:
  298. secure_adapter = CacheControlAdapter(
  299. cache=SafeFileCache(cache, use_dir_lock=True),
  300. max_retries=retries,
  301. )
  302. else:
  303. secure_adapter = HTTPAdapter(max_retries=retries)
  304. # Our Insecure HTTPAdapter disables HTTPS validation. It does not
  305. # support caching (see above) so we'll use it for all http:// URLs as
  306. # well as any https:// host that we've marked as ignoring TLS errors
  307. # for.
  308. insecure_adapter = InsecureHTTPAdapter(max_retries=retries)
  309. self.mount("https://", secure_adapter)
  310. self.mount("http://", insecure_adapter)
  311. # Enable file:// urls
  312. self.mount("file://", LocalFSAdapter())
  313. # We want to use a non-validating adapter for any requests which are
  314. # deemed insecure.
  315. for host in insecure_hosts:
  316. self.mount("https://{}/".format(host), insecure_adapter)
  317. def request(self, method, url, *args, **kwargs):
  318. # Allow setting a default timeout on a session
  319. kwargs.setdefault("timeout", self.timeout)
  320. # Dispatch the actual request
  321. return super(PipSession, self).request(method, url, *args, **kwargs)
  322. def get_file_content(url, comes_from=None, session=None):
  323. # type: (str, Optional[str], Optional[PipSession]) -> Tuple[str, Text]
  324. """Gets the content of a file; it may be a filename, file: URL, or
  325. http: URL. Returns (location, content). Content is unicode.
  326. :param url: File path or url.
  327. :param comes_from: Origin description of requirements.
  328. :param session: Instance of pip.download.PipSession.
  329. """
  330. if session is None:
  331. raise TypeError(
  332. "get_file_content() missing 1 required keyword argument: 'session'"
  333. )
  334. match = _scheme_re.search(url)
  335. if match:
  336. scheme = match.group(1).lower()
  337. if (scheme == 'file' and comes_from and
  338. comes_from.startswith('http')):
  339. raise InstallationError(
  340. 'Requirements file %s references URL %s, which is local'
  341. % (comes_from, url))
  342. if scheme == 'file':
  343. path = url.split(':', 1)[1]
  344. path = path.replace('\\', '/')
  345. match = _url_slash_drive_re.match(path)
  346. if match:
  347. path = match.group(1) + ':' + path.split('|', 1)[1]
  348. path = urllib_parse.unquote(path)
  349. if path.startswith('/'):
  350. path = '/' + path.lstrip('/')
  351. url = path
  352. else:
  353. # FIXME: catch some errors
  354. resp = session.get(url)
  355. resp.raise_for_status()
  356. return resp.url, resp.text
  357. try:
  358. with open(url, 'rb') as f:
  359. content = auto_decode(f.read())
  360. except IOError as exc:
  361. raise InstallationError(
  362. 'Could not open requirements file: %s' % str(exc)
  363. )
  364. return url, content
  365. _scheme_re = re.compile(r'^(http|https|file):', re.I)
  366. _url_slash_drive_re = re.compile(r'/*([a-z])\|', re.I)
  367. def is_url(name):
  368. # type: (Union[str, Text]) -> bool
  369. """Returns true if the name looks like a URL"""
  370. if ':' not in name:
  371. return False
  372. scheme = name.split(':', 1)[0].lower()
  373. return scheme in ['http', 'https', 'file', 'ftp'] + vcs.all_schemes
  374. def url_to_path(url):
  375. # type: (str) -> str
  376. """
  377. Convert a file: URL to a path.
  378. """
  379. assert url.startswith('file:'), (
  380. "You can only turn file: urls into filenames (not %r)" % url)
  381. _, netloc, path, _, _ = urllib_parse.urlsplit(url)
  382. # if we have a UNC path, prepend UNC share notation
  383. if netloc:
  384. netloc = '\\\\' + netloc
  385. path = urllib_request.url2pathname(netloc + path)
  386. return path
  387. def path_to_url(path):
  388. # type: (Union[str, Text]) -> str
  389. """
  390. Convert a path to a file: URL. The path will be made absolute and have
  391. quoted path parts.
  392. """
  393. path = os.path.normpath(os.path.abspath(path))
  394. url = urllib_parse.urljoin('file:', urllib_request.pathname2url(path))
  395. return url
  396. def is_archive_file(name):
  397. # type: (str) -> bool
  398. """Return True if `name` is a considered as an archive file."""
  399. ext = splitext(name)[1].lower()
  400. if ext in ARCHIVE_EXTENSIONS:
  401. return True
  402. return False
  403. def unpack_vcs_link(link, location):
  404. vcs_backend = _get_used_vcs_backend(link)
  405. vcs_backend.unpack(location)
  406. def _get_used_vcs_backend(link):
  407. for backend in vcs.backends:
  408. if link.scheme in backend.schemes:
  409. vcs_backend = backend(link.url)
  410. return vcs_backend
  411. def is_vcs_url(link):
  412. # type: (Link) -> bool
  413. return bool(_get_used_vcs_backend(link))
  414. def is_file_url(link):
  415. # type: (Link) -> bool
  416. return link.url.lower().startswith('file:')
  417. def is_dir_url(link):
  418. # type: (Link) -> bool
  419. """Return whether a file:// Link points to a directory.
  420. ``link`` must not have any other scheme but file://. Call is_file_url()
  421. first.
  422. """
  423. link_path = url_to_path(link.url_without_fragment)
  424. return os.path.isdir(link_path)
  425. def _progress_indicator(iterable, *args, **kwargs):
  426. return iterable
  427. def _download_url(
  428. resp, # type: Response
  429. link, # type: Link
  430. content_file, # type: IO
  431. hashes, # type: Hashes
  432. progress_bar # type: str
  433. ):
  434. # type: (...) -> None
  435. try:
  436. total_length = int(resp.headers['content-length'])
  437. except (ValueError, KeyError, TypeError):
  438. total_length = 0
  439. cached_resp = getattr(resp, "from_cache", False)
  440. if logger.getEffectiveLevel() > logging.INFO:
  441. show_progress = False
  442. elif cached_resp:
  443. show_progress = False
  444. elif total_length > (40 * 1000):
  445. show_progress = True
  446. elif not total_length:
  447. show_progress = True
  448. else:
  449. show_progress = False
  450. show_url = link.show_url
  451. def resp_read(chunk_size):
  452. try:
  453. # Special case for urllib3.
  454. for chunk in resp.raw.stream(
  455. chunk_size,
  456. # We use decode_content=False here because we don't
  457. # want urllib3 to mess with the raw bytes we get
  458. # from the server. If we decompress inside of
  459. # urllib3 then we cannot verify the checksum
  460. # because the checksum will be of the compressed
  461. # file. This breakage will only occur if the
  462. # server adds a Content-Encoding header, which
  463. # depends on how the server was configured:
  464. # - Some servers will notice that the file isn't a
  465. # compressible file and will leave the file alone
  466. # and with an empty Content-Encoding
  467. # - Some servers will notice that the file is
  468. # already compressed and will leave the file
  469. # alone and will add a Content-Encoding: gzip
  470. # header
  471. # - Some servers won't notice anything at all and
  472. # will take a file that's already been compressed
  473. # and compress it again and set the
  474. # Content-Encoding: gzip header
  475. #
  476. # By setting this not to decode automatically we
  477. # hope to eliminate problems with the second case.
  478. decode_content=False):
  479. yield chunk
  480. except AttributeError:
  481. # Standard file-like object.
  482. while True:
  483. chunk = resp.raw.read(chunk_size)
  484. if not chunk:
  485. break
  486. yield chunk
  487. def written_chunks(chunks):
  488. for chunk in chunks:
  489. content_file.write(chunk)
  490. yield chunk
  491. progress_indicator = _progress_indicator
  492. if link.netloc == PyPI.netloc:
  493. url = show_url
  494. else:
  495. url = link.url_without_fragment
  496. if show_progress: # We don't show progress on cached responses
  497. progress_indicator = DownloadProgressProvider(progress_bar,
  498. max=total_length)
  499. if total_length:
  500. logger.info("Downloading %s (%s)", url, format_size(total_length))
  501. else:
  502. logger.info("Downloading %s", url)
  503. elif cached_resp:
  504. logger.info("Using cached %s", url)
  505. else:
  506. logger.info("Downloading %s", url)
  507. logger.debug('Downloading from URL %s', link)
  508. downloaded_chunks = written_chunks(
  509. progress_indicator(
  510. resp_read(CONTENT_CHUNK_SIZE),
  511. CONTENT_CHUNK_SIZE
  512. )
  513. )
  514. if hashes:
  515. hashes.check_against_chunks(downloaded_chunks)
  516. else:
  517. consume(downloaded_chunks)
  518. def _copy_file(filename, location, link):
  519. copy = True
  520. download_location = os.path.join(location, link.filename)
  521. if os.path.exists(download_location):
  522. response = ask_path_exists(
  523. 'The file %s exists. (i)gnore, (w)ipe, (b)ackup, (a)abort' %
  524. display_path(download_location), ('i', 'w', 'b', 'a'))
  525. if response == 'i':
  526. copy = False
  527. elif response == 'w':
  528. logger.warning('Deleting %s', display_path(download_location))
  529. os.remove(download_location)
  530. elif response == 'b':
  531. dest_file = backup_dir(download_location)
  532. logger.warning(
  533. 'Backing up %s to %s',
  534. display_path(download_location),
  535. display_path(dest_file),
  536. )
  537. shutil.move(download_location, dest_file)
  538. elif response == 'a':
  539. sys.exit(-1)
  540. if copy:
  541. shutil.copy(filename, download_location)
  542. logger.info('Saved %s', display_path(download_location))
  543. def unpack_http_url(
  544. link, # type: Link
  545. location, # type: str
  546. download_dir=None, # type: Optional[str]
  547. session=None, # type: Optional[PipSession]
  548. hashes=None, # type: Optional[Hashes]
  549. progress_bar="on" # type: str
  550. ):
  551. # type: (...) -> None
  552. if session is None:
  553. raise TypeError(
  554. "unpack_http_url() missing 1 required keyword argument: 'session'"
  555. )
  556. with TempDirectory(kind="unpack") as temp_dir:
  557. # If a download dir is specified, is the file already downloaded there?
  558. already_downloaded_path = None
  559. if download_dir:
  560. already_downloaded_path = _check_download_dir(link,
  561. download_dir,
  562. hashes)
  563. if already_downloaded_path:
  564. from_path = already_downloaded_path
  565. content_type = mimetypes.guess_type(from_path)[0]
  566. else:
  567. # let's download to a tmp dir
  568. from_path, content_type = _download_http_url(link,
  569. session,
  570. temp_dir.path,
  571. hashes,
  572. progress_bar)
  573. # unpack the archive to the build dir location. even when only
  574. # downloading archives, they have to be unpacked to parse dependencies
  575. unpack_file(from_path, location, content_type, link)
  576. # a download dir is specified; let's copy the archive there
  577. if download_dir and not already_downloaded_path:
  578. _copy_file(from_path, download_dir, link)
  579. if not already_downloaded_path:
  580. os.unlink(from_path)
  581. def unpack_file_url(
  582. link, # type: Link
  583. location, # type: str
  584. download_dir=None, # type: Optional[str]
  585. hashes=None # type: Optional[Hashes]
  586. ):
  587. # type: (...) -> None
  588. """Unpack link into location.
  589. If download_dir is provided and link points to a file, make a copy
  590. of the link file inside download_dir.
  591. """
  592. link_path = url_to_path(link.url_without_fragment)
  593. # If it's a url to a local directory
  594. if is_dir_url(link):
  595. if os.path.isdir(location):
  596. rmtree(location)
  597. shutil.copytree(link_path, location, symlinks=True)
  598. if download_dir:
  599. logger.info('Link is a directory, ignoring download_dir')
  600. return
  601. # If --require-hashes is off, `hashes` is either empty, the
  602. # link's embedded hash, or MissingHashes; it is required to
  603. # match. If --require-hashes is on, we are satisfied by any
  604. # hash in `hashes` matching: a URL-based or an option-based
  605. # one; no internet-sourced hash will be in `hashes`.
  606. if hashes:
  607. hashes.check_against_path(link_path)
  608. # If a download dir is specified, is the file already there and valid?
  609. already_downloaded_path = None
  610. if download_dir:
  611. already_downloaded_path = _check_download_dir(link,
  612. download_dir,
  613. hashes)
  614. if already_downloaded_path:
  615. from_path = already_downloaded_path
  616. else:
  617. from_path = link_path
  618. content_type = mimetypes.guess_type(from_path)[0]
  619. # unpack the archive to the build dir location. even when only downloading
  620. # archives, they have to be unpacked to parse dependencies
  621. unpack_file(from_path, location, content_type, link)
  622. # a download dir is specified and not already downloaded
  623. if download_dir and not already_downloaded_path:
  624. _copy_file(from_path, download_dir, link)
  625. def _copy_dist_from_dir(link_path, location):
  626. """Copy distribution files in `link_path` to `location`.
  627. Invoked when user requests to install a local directory. E.g.:
  628. pip install .
  629. pip install ~/dev/git-repos/python-prompt-toolkit
  630. """
  631. # Note: This is currently VERY SLOW if you have a lot of data in the
  632. # directory, because it copies everything with `shutil.copytree`.
  633. # What it should really do is build an sdist and install that.
  634. # See https://github.com/pypa/pip/issues/2195
  635. if os.path.isdir(location):
  636. rmtree(location)
  637. # build an sdist
  638. setup_py = 'setup.py'
  639. sdist_args = [sys.executable]
  640. sdist_args.append('-c')
  641. sdist_args.append(SETUPTOOLS_SHIM % setup_py)
  642. sdist_args.append('sdist')
  643. sdist_args += ['--dist-dir', location]
  644. logger.info('Running setup.py sdist for %s', link_path)
  645. with indent_log():
  646. call_subprocess(sdist_args, cwd=link_path, show_stdout=False)
  647. # unpack sdist into `location`
  648. sdist = os.path.join(location, os.listdir(location)[0])
  649. logger.info('Unpacking sdist %s into %s', sdist, location)
  650. unpack_file(sdist, location, content_type=None, link=None)
  651. class PipXmlrpcTransport(xmlrpc_client.Transport):
  652. """Provide a `xmlrpclib.Transport` implementation via a `PipSession`
  653. object.
  654. """
  655. def __init__(self, index_url, session, use_datetime=False):
  656. xmlrpc_client.Transport.__init__(self, use_datetime)
  657. index_parts = urllib_parse.urlparse(index_url)
  658. self._scheme = index_parts.scheme
  659. self._session = session
  660. def request(self, host, handler, request_body, verbose=False):
  661. parts = (self._scheme, host, handler, None, None, None)
  662. url = urllib_parse.urlunparse(parts)
  663. try:
  664. headers = {'Content-Type': 'text/xml'}
  665. response = self._session.post(url, data=request_body,
  666. headers=headers, stream=True)
  667. response.raise_for_status()
  668. self.verbose = verbose
  669. return self.parse_response(response.raw)
  670. except requests.HTTPError as exc:
  671. logger.critical(
  672. "HTTP error %s while getting %s",
  673. exc.response.status_code, url,
  674. )
  675. raise
  676. def unpack_url(
  677. link, # type: Optional[Link]
  678. location, # type: Optional[str]
  679. download_dir=None, # type: Optional[str]
  680. only_download=False, # type: bool
  681. session=None, # type: Optional[PipSession]
  682. hashes=None, # type: Optional[Hashes]
  683. progress_bar="on" # type: str
  684. ):
  685. # type: (...) -> None
  686. """Unpack link.
  687. If link is a VCS link:
  688. if only_download, export into download_dir and ignore location
  689. else unpack into location
  690. for other types of link:
  691. - unpack into location
  692. - if download_dir, copy the file into download_dir
  693. - if only_download, mark location for deletion
  694. :param hashes: A Hashes object, one of whose embedded hashes must match,
  695. or HashMismatch will be raised. If the Hashes is empty, no matches are
  696. required, and unhashable types of requirements (like VCS ones, which
  697. would ordinarily raise HashUnsupported) are allowed.
  698. """
  699. # non-editable vcs urls
  700. if is_vcs_url(link):
  701. unpack_vcs_link(link, location)
  702. # file urls
  703. elif is_file_url(link):
  704. unpack_file_url(link, location, download_dir, hashes=hashes)
  705. # http urls
  706. else:
  707. if session is None:
  708. session = PipSession()
  709. unpack_http_url(
  710. link,
  711. location,
  712. download_dir,
  713. session,
  714. hashes=hashes,
  715. progress_bar=progress_bar
  716. )
  717. if only_download:
  718. write_delete_marker_file(location)
  719. def _download_http_url(
  720. link, # type: Link
  721. session, # type: PipSession
  722. temp_dir, # type: str
  723. hashes, # type: Hashes
  724. progress_bar # type: str
  725. ):
  726. # type: (...) -> Tuple[str, str]
  727. """Download link url into temp_dir using provided session"""
  728. target_url = link.url.split('#', 1)[0]
  729. try:
  730. resp = session.get(
  731. target_url,
  732. # We use Accept-Encoding: identity here because requests
  733. # defaults to accepting compressed responses. This breaks in
  734. # a variety of ways depending on how the server is configured.
  735. # - Some servers will notice that the file isn't a compressible
  736. # file and will leave the file alone and with an empty
  737. # Content-Encoding
  738. # - Some servers will notice that the file is already
  739. # compressed and will leave the file alone and will add a
  740. # Content-Encoding: gzip header
  741. # - Some servers won't notice anything at all and will take
  742. # a file that's already been compressed and compress it again
  743. # and set the Content-Encoding: gzip header
  744. # By setting this to request only the identity encoding We're
  745. # hoping to eliminate the third case. Hopefully there does not
  746. # exist a server which when given a file will notice it is
  747. # already compressed and that you're not asking for a
  748. # compressed file and will then decompress it before sending
  749. # because if that's the case I don't think it'll ever be
  750. # possible to make this work.
  751. headers={"Accept-Encoding": "identity"},
  752. stream=True,
  753. )
  754. resp.raise_for_status()
  755. except requests.HTTPError as exc:
  756. logger.critical(
  757. "HTTP error %s while getting %s", exc.response.status_code, link,
  758. )
  759. raise
  760. content_type = resp.headers.get('content-type', '')
  761. filename = link.filename # fallback
  762. # Have a look at the Content-Disposition header for a better guess
  763. content_disposition = resp.headers.get('content-disposition')
  764. if content_disposition:
  765. type, params = cgi.parse_header(content_disposition)
  766. # We use ``or`` here because we don't want to use an "empty" value
  767. # from the filename param.
  768. filename = params.get('filename') or filename
  769. ext = splitext(filename)[1]
  770. if not ext:
  771. ext = mimetypes.guess_extension(content_type)
  772. if ext:
  773. filename += ext
  774. if not ext and link.url != resp.url:
  775. ext = os.path.splitext(resp.url)[1]
  776. if ext:
  777. filename += ext
  778. file_path = os.path.join(temp_dir, filename)
  779. with open(file_path, 'wb') as content_file:
  780. _download_url(resp, link, content_file, hashes, progress_bar)
  781. return file_path, content_type
  782. def _check_download_dir(link, download_dir, hashes):
  783. # type: (Link, str, Hashes) -> Optional[str]
  784. """ Check download_dir for previously downloaded file with correct hash
  785. If a correct file is found return its path else None
  786. """
  787. download_path = os.path.join(download_dir, link.filename)
  788. if os.path.exists(download_path):
  789. # If already downloaded, does its hash match?
  790. logger.info('File was already downloaded %s', download_path)
  791. if hashes:
  792. try:
  793. hashes.check_against_path(download_path)
  794. except HashMismatch:
  795. logger.warning(
  796. 'Previously-downloaded file %s has bad hash. '
  797. 'Re-downloading.',
  798. download_path
  799. )
  800. os.unlink(download_path)
  801. return None
  802. return download_path
  803. return None