8000 add human friendly download logs (#8671) · codeperl/localstack@7f57e63 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f57e63

Browse files
add human friendly download logs (localstack#8671)
1 parent 5df0f4a commit 7f57e63

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

localstack/utils/http.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,24 +200,38 @@ def download(
200200
if not r.ok:
201201
raise Exception("Failed to download %s, response code %s" % (url, r.status_code))
202202

203-
total = 0
203+
total_size = 0
204+
if r.headers.get("Content-Length"):
205+
total_size = int(r.headers.get("Content-Length"))
206+
207+
total_written = 0
204208
if not os.path.exists(os.path.dirname(path)):
205209
os.makedirs(os.path.dirname(path))
206-
LOG.debug(
207-
"Starting download from %s to %s (%s bytes)", url, path, r.headers.get("Content-Length")
208-
)
210+
LOG.debug("Starting download from %s to %s", url, path)
209211
with open(path, "wb") as f:
210212
iter_length = 0
211213
iter_limit = 1000000 # print a log line for every 1MB chunk
212214
for chunk in r.iter_content(DOWNLOAD_CHUNK_SIZE):
213-
total += len(chunk)
215+
total_written += len(chunk)
214216
iter_length += len(chunk)
215217
if chunk: # filter out keep-alive new chunks
216218
f.write(chunk)
217219
else:
218-
LOG.debug("Empty chunk %s (total %s) from %s", chunk, total, url)
220+
LOG.debug(
221+
"Empty chunk %s (total %dK of %dK) from %s",
222+
chunk,
223+
total_written / 1024,
224+
total_size / 1024,
225+
url,
226+
)
219227
if iter_length >= iter_limit:
220-
LOG.debug("Written %s bytes (total %s) to %s", iter_length, total, path)
228+
LOG.debug(
229+
"Written %dK (total %dK of %dK) to %s",
230+
iter_length / 1024,
231+
total_written / 1024,
232+
total_size / 1024,
233+
path,
234+
)
221235
iter_length = 0
222236
f.flush()
223237
os.fsync(f)
@@ -226,7 +240,10 @@ def download(
226240
download(url, path, verify_ssl)
227241
return
228242
LOG.debug(
229-
"Done downloading %s, response code %s, total bytes %d", url, r.status_code, total
243+
"Done downloading %s, response code %s, total %dK",
244+
url,
245+
r.status_code,
246+
total_written / 1024,
230247
)
231248
except requests.exceptions.ReadTimeout as e:
232249
raise TimeoutError(f"Timeout ({timeout}) reached on download: {url} - {e}")

0 commit comments

Comments
 (0)
0