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

fix: performance bug in install #34

Merged
merged 1 commit into from
Sep 13, 2024
Merged
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
32 changes: 12 additions & 20 deletions moccasin/commands/install.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from argparse import Namespace
from dataclasses import dataclass
from io import BytesIO
from pathlib import Path
import shutil
import subprocess
Expand Down Expand Up @@ -164,29 +165,20 @@ def _stream_download(
download_url: str, target_path: str, headers: dict[str, str] = REQUEST_HEADERS
) -> None:
response = requests.get(download_url, stream=True, headers=headers)

response.raise_for_status()

total_size = int(response.headers.get("content-length", 0))
progress_bar = tqdm(total=total_size, unit="iB", unit_scale=True)
content = bytes()

for data in response.iter_content(None, decode_unicode=True):
progress_bar.update(len(data))
content += data
progress_bar.close()

temp_file = os.path.join(target_path, "temp_download.zip")

with (
open(temp_file, "wb") as f,
tqdm(
desc="Downloading",
total=total_size,
unit="iB",
unit_scale=True,
unit_divisor=1024,
) as progress_bar,
):
for data in response.iter_content(chunk_size=None):
size = f.write(data)
progress_bar.update(size)

with zipfile.ZipFile(temp_file, "r") as zip_ref:
zip_ref.extractall(target_path)

os.remove(temp_file)
with zipfile.ZipFile(BytesIO(content)) as zf:
zf.extractall(target_path)


def _maybe_retrieve_github_auth() -> dict[str, str]:
Expand Down