summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2020-08-09 15:55:33 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2020-08-11 22:36:08 +0700
commite62f16e96938ee24e7a57168b829942526be56e2 (patch)
treef66b6dc4cdad22499a21f2f2bc75fcc553826e77
parentea10d0e253f497c9fc08097f14cdba6b94b45541 (diff)
downloadpip-e62f16e96938ee24e7a57168b829942526be56e2.tar.gz
Make Downloader perform the download
-rw-r--r--src/pip/_internal/network/download.py30
-rw-r--r--src/pip/_internal/operations/prepare.py31
-rw-r--r--tests/unit/test_operations_prepare.py13
3 files changed, 15 insertions, 59 deletions
diff --git a/src/pip/_internal/network/download.py b/src/pip/_internal/network/download.py
index 44f9985a3..a4d4bd2a5 100644
--- a/src/pip/_internal/network/download.py
+++ b/src/pip/_internal/network/download.py
@@ -24,7 +24,7 @@ from pip._internal.utils.misc import (
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
- from typing import Iterable, Optional
+ from typing import Iterable, Optional, Tuple
from pip._vendor.requests.models import Response
@@ -141,19 +141,6 @@ def _http_get_download(session, link):
return resp
-class Download(object):
- def __init__(
- self,
- response, # type: Response
- filename, # type: str
- chunks, # type: Iterable[bytes]
- ):
- # type: (...) -> None
- self.response = response
- self.filename = filename
- self.chunks = chunks
-
-
class Downloader(object):
def __init__(
self,
@@ -164,8 +151,8 @@ class Downloader(object):
self._session = session
self._progress_bar = progress_bar
- def __call__(self, link):
- # type: (Link) -> Download
+ def __call__(self, link, location):
+ # type: (Link, str) -> Tuple[str, str]
try:
resp = _http_get_download(self._session, link)
except NetworkConnectionError as e:
@@ -175,8 +162,9 @@ class Downloader(object):
)
raise
- return Download(
- resp,
- _get_http_response_filename(resp, link),
- _prepare_download(resp, link, self._progress_bar),
- )
+ filename = _get_http_response_filename(resp, link)
+ chunks = _prepare_download(resp, link, self._progress_bar)
+ with open(os.path.join(location, filename), 'wb') as content_file:
+ for chunk in chunks:
+ content_file.write(chunk)
+ return content_file.name, resp.headers.get('Content-Type', '')
diff --git a/src/pip/_internal/operations/prepare.py b/src/pip/_internal/operations/prepare.py
index e9e534e32..e4de1be4a 100644
--- a/src/pip/_internal/operations/prepare.py
+++ b/src/pip/_internal/operations/prepare.py
@@ -45,9 +45,7 @@ from pip._internal.utils.unpacking import unpack_file
from pip._internal.vcs import vcs
if MYPY_CHECK_RUNNING:
- from typing import (
- Callable, List, Optional, Tuple,
- )
+ from typing import Callable, List, Optional
from mypy_extensions import TypedDict
from pip._vendor.pkg_resources import Distribution
@@ -132,9 +130,9 @@ def get_http_url(
content_type = mimetypes.guess_type(from_path)[0]
else:
# let's download to a tmp dir
- from_path, content_type = _download_http_url(
- link, downloader, temp_dir.path, hashes
- )
+ from_path, content_type = downloader(link, temp_dir.path)
+ if hashes:
+ hashes.check_against_path(from_path)
return File(from_path, content_type)
@@ -273,27 +271,6 @@ def unpack_url(
return file
-def _download_http_url(
- link, # type: Link
- downloader, # type: Downloader
- temp_dir, # type: str
- hashes, # type: Optional[Hashes]
-):
- # type: (...) -> Tuple[str, str]
- """Download link url into temp_dir using provided session"""
- download = downloader(link)
-
- file_path = os.path.join(temp_dir, download.filename)
- with open(file_path, 'wb') as content_file:
- for chunk in download.chunks:
- content_file.write(chunk)
-
- if hashes:
- hashes.check_against_path(file_path)
-
- return file_path, download.response.headers.get('content-type', '')
-
-
def _check_download_dir(link, download_dir, hashes):
# type: (Link, str, Optional[Hashes]) -> Optional[str]
""" Check download_dir for previously downloaded file with correct hash
diff --git a/tests/unit/test_operations_prepare.py b/tests/unit/test_operations_prepare.py
index 41d8be260..d2e4d6091 100644
--- a/tests/unit/test_operations_prepare.py
+++ b/tests/unit/test_operations_prepare.py
@@ -10,11 +10,7 @@ from pip._internal.exceptions import HashMismatch
from pip._internal.models.link import Link
from pip._internal.network.download import Downloader
from pip._internal.network.session import PipSession
-from pip._internal.operations.prepare import (
- _copy_source_tree,
- _download_http_url,
- unpack_url,
-)
+from pip._internal.operations.prepare import _copy_source_tree, unpack_url
from pip._internal.utils.hashes import Hashes
from pip._internal.utils.urls import path_to_url
from tests.lib.filesystem import (
@@ -83,12 +79,7 @@ def test_download_http_url__no_directory_traversal(mock_raise_for_status,
download_dir = tmpdir.joinpath('download')
os.mkdir(download_dir)
- file_path, content_type = _download_http_url(
- link,
- downloader,
- download_dir,
- hashes=None,
- )
+ file_path, content_type = downloader(link, download_dir)
# The file should be downloaded to download_dir.
actual = os.listdir(download_dir)
assert actual == ['out_dir_file']