Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add progress to download_file if in Indicator context #5451

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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