Skip to content

Commit

Permalink
add progress to download_file if in Indicator context (#5451)
Browse files Browse the repository at this point in the history
Co-authored-by: Arun Babu Neelicattu <arun.neelicattu@gmail.com>
Co-authored-by: Bjorn Neergaard <bjorn@neersighted.com>
  • Loading branch information
3 people authored May 18, 2022
1 parent f05329a commit a614b9b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
23 changes: 22 additions & 1 deletion src/poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
from typing import Iterable
from typing import Iterator
from typing import cast
Expand Down Expand Up @@ -55,6 +56,24 @@


class Indicator(ProgressIndicator): # type: ignore[misc]
CONTEXT: str | None = None

@staticmethod
@contextmanager
def context() -> Iterator[Callable[[str | None], None]]:
def _set_context(context: str | None) -> None:
Indicator.CONTEXT = context

yield _set_context

_set_context(None)

def _formatter_context(self) -> str:
if Indicator.CONTEXT is None:
return " "
else:
return f" <c1>{Indicator.CONTEXT}</> "

def _formatter_elapsed(self) -> str:
elapsed = time.time() - self._start_time

Expand Down Expand Up @@ -796,7 +815,9 @@ def progress(self) -> Iterator[None]:
self._io.write_line("Resolving dependencies...")
yield
else:
indicator = Indicator(self._io, "{message} <debug>({elapsed:2s})</debug>")
indicator = Indicator(
self._io, "{message}{context}<debug>({elapsed:2s})</debug>"
)

with indicator.auto(
"<info>Resolving dependencies...</info>",
Expand Down
34 changes: 30 additions & 4 deletions src/poetry/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,41 @@ def download_file(
) -> None:
import requests

from poetry.puzzle.provider import Indicator

get = requests.get if not session else session.get

response = get(url, stream=True)
response.raise_for_status()

with open(dest, "wb") as f:
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)
set_indicator = False
with Indicator.context() as update_context:
update_context(f"Downloading {url}")

if "Content-Length" in response.headers:
try:
total_size = int(response.headers["Content-Length"])
except ValueError:
total_size = 0

fetched_size = 0
last_percent = 0

# if less than 1MB, we simply show that we're downloading
# but skip the updating
set_indicator = total_size > 1024 * 1024

with open(dest, "wb") as f:
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)

if set_indicator:
fetched_size += len(chunk)
percent = (fetched_size * 100) // total_size
if percent > last_percent:
last_percent = percent
update_context(f"Downloading {url} {percent:3}%")


def get_package_version_display_string(
Expand Down

0 comments on commit a614b9b

Please sign in to comment.