Skip to content

Commit 78a221c

Browse files
committed
Move Downloader to network.download
This will help us move Downloader construction out of RequirementPreparer, reducing its concerns and making it easier to test in isolation.
1 parent 3a2ff97 commit 78a221c

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

src/pip/_internal/network/download.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import mimetypes
66
import os
77

8+
from pip._vendor import requests
89
from pip._vendor.requests.models import CONTENT_CHUNK_SIZE
910

1011
from pip._internal.models.index import PyPI
@@ -157,3 +158,43 @@ def _http_get_download(session, link):
157158
)
158159
resp.raise_for_status()
159160
return resp
161+
162+
163+
class Download(object):
164+
def __init__(
165+
self,
166+
response, # type: Response
167+
filename, # type: str
168+
chunks, # type: Iterable[bytes]
169+
):
170+
# type: (...) -> None
171+
self.response = response
172+
self.filename = filename
173+
self.chunks = chunks
174+
175+
176+
class Downloader(object):
177+
def __init__(
178+
self,
179+
session, # type: PipSession
180+
progress_bar, # type: str
181+
):
182+
# type: (...) -> None
183+
self._session = session
184+
self._progress_bar = progress_bar
185+
186+
def __call__(self, link):
187+
# type: (Link) -> Download
188+
try:
189+
resp = _http_get_download(self._session, link)
190+
except requests.HTTPError as e:
191+
logger.critical(
192+
"HTTP error %s while getting %s", e.response.status_code, link
193+
)
194+
raise
195+
196+
return Download(
197+
resp,
198+
_get_http_response_filename(resp, link),
199+
_prepare_download(resp, link, self._progress_bar),
200+
)

src/pip/_internal/operations/prepare.py

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import sys
1313

1414
from pip._vendor import requests
15-
from pip._vendor.requests.models import Response
1615
from pip._vendor.six import PY2
1716

1817
from pip._internal.distributions import (
@@ -27,11 +26,7 @@
2726
PreviousBuildDirError,
2827
VcsHashUnsupported,
2928
)
30-
from pip._internal.network.download import (
31-
_get_http_response_filename,
32-
_http_get_download,
33-
_prepare_download,
34-
)
29+
from pip._internal.network.download import Downloader
3530
from pip._internal.utils.compat import expanduser
3631
from pip._internal.utils.filesystem import copy2_fixed
3732
from pip._internal.utils.hashes import MissingHashes
@@ -53,7 +48,7 @@
5348

5449
if MYPY_CHECK_RUNNING:
5550
from typing import (
56-
Callable, Iterable, List, Optional, Tuple,
51+
Callable, List, Optional, Tuple,
5752
)
5853

5954
from mypy_extensions import TypedDict
@@ -307,46 +302,6 @@ def unpack_url(
307302
)
308303

309304

310-
class Download(object):
311-
def __init__(
312-
self,
313-
response, # type: Response
314-
filename, # type: str
315-
chunks, # type: Iterable[bytes]
316-
):
317-
# type: (...) -> None
318-
self.response = response
319-
self.filename = filename
320-
self.chunks = chunks
321-
322-
323-
class Downloader(object):
324-
def __init__(
325-
self,
326-
session, # type: PipSession
327-
progress_bar, # type: str
328-
):
329-
# type: (...) -> None
330-
self._session = session
331-
self._progress_bar = progress_bar
332-
333-
def __call__(self, link):
334-
# type: (Link) -> Download
335-
try:
336-
resp = _http_get_download(self._session, link)
337-
except requests.HTTPError as e:
338-
logger.critical(
339-
"HTTP error %s while getting %s", e.response.status_code, link
340-
)
341-
raise
342-
343-
return Download(
344-
resp,
345-
_get_http_response_filename(resp, link),
346-
_prepare_download(resp, link, self._progress_bar),
347-
)
348-
349-
350305
def _download_http_url(
351306
link, # type: Link
352307
downloader, # type: Downloader

0 commit comments

Comments
 (0)