@@ -200,24 +200,38 @@ def download(
200
200
if not r .ok :
201
201
raise Exception ("Failed to download %s, response code %s" % (url , r .status_code ))
202
202
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
204
208
if not os .path .exists (os .path .dirname (path )):
205
209
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 )
209
211
with open (path , "wb" ) as f :
210
212
iter_length = 0
211
213
iter_limit = 1000000 # print a log line for every 1MB chunk
212
214
for chunk in r .iter_content (DOWNLOAD_CHUNK_SIZE ):
213
- total += len (chunk )
215
+ total_written += len (chunk )
214
216
iter_length += len (chunk )
215
217
if chunk : # filter out keep-alive new chunks
216
218
f .write (chunk )
217
219
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
+ )
219
227
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
+ )
221
235
iter_length = 0
222
236
f .flush ()
223
237
os .fsync (f )
@@ -226,7 +240,10 @@ def download(
226
240
download (url , path , verify_ssl )
227
241
return
228
242
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 ,
230
247
)
231
248
except requests .exceptions .ReadTimeout as e :
232
249
raise TimeoutError (f"Timeout ({ timeout } ) reached on download: { url } - { e } " )
0 commit comments