Skip to content

Commit

Permalink
add progress bar to _download_file if in Progress.Indictor context
Browse files Browse the repository at this point in the history
  • Loading branch information
tgolsson committed Apr 14, 2022
1 parent a9d05f5 commit 9a02849
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@


class Indicator(ProgressIndicator):
CONTEXT: str | None = None

@staticmethod
def set_context(context: str | None) -> None:
Indicator.CONTEXT = context

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

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

Expand Down Expand Up @@ -781,7 +793,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
25 changes: 25 additions & 0 deletions src/poetry/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,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()

set_indicator = False
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

Indicator.set_context(f"Downloading {url}")
# 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
Indicator.set_context(f"Downloading {url} {percent:3}%")

Indicator.set_context(None)


def get_package_version_display_string(
package: Package, root: Path | None = None
Expand Down

0 comments on commit 9a02849

Please sign in to comment.