Skip to content

Commit

Permalink
Fix prusa3d#4407 initial depenency bootstrapping fails
Browse files Browse the repository at this point in the history
- replace urlretrieve with requests
- cleanup download_and_unzip function during bootstrap
- adjust python requirements for pipeline and dev
  • Loading branch information
AndreasArendt committed Jan 14, 2025
1 parent 8978fe1 commit 2ee73e7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ python-bidi==0.4.2
pyyaml~=6.0
qoi~=0.5.0
setuptools~=70.1.0
requests==2.32.3
55 changes: 42 additions & 13 deletions utils/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import stat
from argparse import ArgumentParser
from pathlib import Path
from urllib.request import urlretrieve
from urllib.parse import urlparse
import requests

assert sys.version_info >= (3, 8), 'Python 3.8+ is required.'
is_windows = platform.system() == 'Windows'
Expand Down Expand Up @@ -135,24 +136,52 @@ def find_single_subdir(path: Path):
raise RuntimeError


def download_url(url: str, filename: Path):
"""Download file from url and write it to given filename"""
with requests.get(url, stream=True) as response:
response.raise_for_status()
with open(filename, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
file.write(chunk)


def download_and_unzip(url: str, directory: Path):
"""Download a compressed file and extract it at `directory`."""
extract_dir = directory.with_suffix('.temp')
shutil.rmtree(directory, ignore_errors=True)
shutil.rmtree(extract_dir, ignore_errors=True)

print('Downloading ' + directory.name)
f, _ = urlretrieve(url, filename=None)
print('Extracting ' + directory.name)
if '.tar.bz2' in url or '.tar.gz' in url or '.tar.xz' in url:
obj = tarfile.open(f)
else:
obj = zipfile.ZipFile(f, 'r')
obj.extractall(path=str(extract_dir))

print('Downloading ' + directory.name, end=" ")

# temporary local filepath
parsed_url = urlparse(url)
file = Path(parsed_url.path).name
filename = Path(dependencies_dir) / file

download_url(url=url, filename=filename)

print('done')
print('Extracting ' + file, end=" ")

# Check if tar or zip
if any(url.endswith(ext) for ext in ['.tar.bz2', '.tar.gz', '.tar.xz']):
with tarfile.open(filename) as obj:
obj.extractall(path=extract_dir)
else:
with zipfile.ZipFile(filename, 'r') as obj:
obj.extractall(path=extract_dir)

subdir = find_single_subdir(extract_dir)
shutil.move(str(subdir), str(directory))
shutil.rmtree(extract_dir, ignore_errors=True)
shutil.move(subdir, directory)

print('done')

# remove temp unzip folder
shutil.rmtree(extract_dir, ignore_errors=True)

# remove downloaded zip
os.remove(filename)


def run(*cmd):
Expand Down Expand Up @@ -204,7 +233,7 @@ def install_dependency(dependency):
os.mkdir(installation_directory)
for file in files:
basename = file.split('/')[-1]
urlretrieve(file, installation_directory / basename)
download_url(url=file, filename=installation_directory / basename)
else:
raise ('dependency is missing payload')

Expand Down
1 change: 1 addition & 0 deletions utils/holly/build-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pyyaml~=6.0
qoi~=0.5.0
numpy==1.26.4
setuptools~=70.1.0
requests==2.32.3

0 comments on commit 2ee73e7

Please sign in to comment.