Skip to content

Commit

Permalink
Remove (never-seen) progress & handle HTTP errors
Browse files Browse the repository at this point in the history
  • Loading branch information
danudey committed Mar 27, 2024
1 parent 1d1e31a commit ba40661
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions hack/postrelease/tests/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import pytest
import requests
import rich.progress
import yaml

import variables
Expand All @@ -15,31 +14,15 @@
def skip_if_master(reason):
return pytest.mark.skipif(variables.RELEASE_STREAM == "master", reason=reason)

class ResponseMissingContentDispositionError(Exception):
class FailedDownloadError(Exception):
pass

def get_filename_from_url(url):
url_path = urllib.parse.urlparse(url).path
filename = pathlib.Path(url_path).name
return filename


def get_content_disposition(response):
filename = response.headers["Content-Disposition"].replace(
"attachment; filename=", ""
)
return filename


def download_url_to_file(url, destination):
response = requests.get(url, stream=True)
length = int(response.headers["Content-Length"])
filename = get_content_disposition(response)
with rich.progress.wrap_file(
response.raw, total=length, description=filename
) as infile:
with open(destination, "wb") as outfile:
shutil.copyfileobj(infile, outfile)
if response.status_code != 200:
raise FailedDownloadError(f"HTTP error {response.status_code}")
with open(destination, "wb") as outfile:
shutil.copyfileobj(response.raw, outfile)


def download_url_to_file_noprogress(url, destination):
Expand Down

0 comments on commit ba40661

Please sign in to comment.