Skip to content

Commit 0af3a4c

Browse files
committed
Make the call to actual download a bit less nested
1 parent 68608d9 commit 0af3a4c

File tree

3 files changed

+20
-68
lines changed

3 files changed

+20
-68
lines changed

src/pip/_internal/network/download.py

+14-30
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
2525

2626
if MYPY_CHECK_RUNNING:
27-
from typing import Iterable, Optional
27+
from typing import Iterable, Optional, Tuple
2828

2929
from pip._vendor.requests.models import Response
3030

@@ -133,27 +133,6 @@ def _get_http_response_filename(resp, link):
133133
return filename
134134

135135

136-
def _http_get_download(session, link):
137-
# type: (PipSession, Link) -> Response
138-
target_url = link.url.split('#', 1)[0]
139-
resp = session.get(target_url, headers=HEADERS, stream=True)
140-
raise_for_status(resp)
141-
return resp
142-
143-
144-
class Download(object):
145-
def __init__(
146-
self,
147-
response, # type: Response
148-
filename, # type: str
149-
chunks, # type: Iterable[bytes]
150-
):
151-
# type: (...) -> None
152-
self.response = response
153-
self.filename = filename
154-
self.chunks = chunks
155-
156-
157136
class Downloader(object):
158137
def __init__(
159138
self,
@@ -164,19 +143,24 @@ def __init__(
164143
self._session = session
165144
self._progress_bar = progress_bar
166145

167-
def __call__(self, link):
168-
# type: (Link) -> Download
146+
def __call__(self, link, tmpdir):
147+
# type: (Link, str) -> Tuple[str, str]
148+
url = link.url.split('#', 1)[0]
149+
response = self._session.get(url, headers=HEADERS, stream=True)
169150
try:
170-
resp = _http_get_download(self._session, link)
151+
raise_for_status(response)
171152
except NetworkConnectionError as e:
172153
assert e.response is not None
173154
logger.critical(
174155
"HTTP error %s while getting %s", e.response.status_code, link
175156
)
176157
raise
177158

178-
return Download(
179-
resp,
180-
_get_http_response_filename(resp, link),
181-
_prepare_download(resp, link, self._progress_bar),
182-
)
159+
chunks = _prepare_download(response, link, self._progress_bar)
160+
filename = _get_http_response_filename(response, link)
161+
file_path = os.path.join(tmpdir, filename)
162+
163+
with open(file_path, 'wb') as content_file:
164+
for chunk in chunks:
165+
content_file.write(chunk)
166+
return file_path, response.headers.get('content-type', '')

src/pip/_internal/operations/prepare.py

+4-27
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939
from pip._internal.vcs import vcs
4040

4141
if MYPY_CHECK_RUNNING:
42-
from typing import (
43-
Callable, List, Optional, Tuple,
44-
)
42+
from typing import Callable, List, Optional
4543

4644
from mypy_extensions import TypedDict
4745

@@ -126,9 +124,9 @@ def get_http_url(
126124
content_type = mimetypes.guess_type(from_path)[0]
127125
else:
128126
# let's download to a tmp dir
129-
from_path, content_type = _download_http_url(
130-
link, downloader, temp_dir.path, hashes
131-
)
127+
from_path, content_type = downloader(link, temp_dir.path)
128+
if hashes:
129+
hashes.check_against_path(from_path)
132130

133131
return File(from_path, content_type)
134132

@@ -267,27 +265,6 @@ def unpack_url(
267265
return file
268266

269267

270-
def _download_http_url(
271-
link, # type: Link
272-
downloader, # type: Downloader
273-
temp_dir, # type: str
274-
hashes, # type: Optional[Hashes]
275-
):
276-
# type: (...) -> Tuple[str, str]
277-
"""Download link url into temp_dir using provided session"""
278-
download = downloader(link)
279-
280-
file_path = os.path.join(temp_dir, download.filename)
281-
with open(file_path, 'wb') as content_file:
282-
for chunk in download.chunks:
283-
content_file.write(chunk)
284-
285-
if hashes:
286-
hashes.check_against_path(file_path)
287-
288-
return file_path, download.response.headers.get('content-type', '')
289-
290-
291268
def _check_download_dir(link, download_dir, hashes):
292269
# type: (Link, str, Optional[Hashes]) -> Optional[str]
293270
""" Check download_dir for previously downloaded file with correct hash

tests/unit/test_operations_prepare.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
from pip._internal.models.link import Link
1111
from pip._internal.network.download import Downloader
1212
from pip._internal.network.session import PipSession
13-
from pip._internal.operations.prepare import (
14-
_copy_source_tree,
15-
_download_http_url,
16-
unpack_url,
17-
)
13+
from pip._internal.operations.prepare import _copy_source_tree, unpack_url
1814
from pip._internal.utils.hashes import Hashes
1915
from pip._internal.utils.urls import path_to_url
2016
from tests.lib.filesystem import (
@@ -83,12 +79,7 @@ def test_download_http_url__no_directory_traversal(mock_raise_for_status,
8379

8480
download_dir = tmpdir.joinpath('download')
8581
os.mkdir(download_dir)
86-
file_path, content_type = _download_http_url(
87-
link,
88-
downloader,
89-
download_dir,
90-
hashes=None,
91-
)
82+
file_path, content_type = downloader(link, download_dir)
9283
# The file should be downloaded to download_dir.
9384
actual = os.listdir(download_dir)
9485
assert actual == ['out_dir_file']

0 commit comments

Comments
 (0)