@@ -64,30 +64,23 @@ def mkdir_p(path):
64
64
if exc .errno != errno .EEXIST or not os .path .isdir (path ):
65
65
raise
66
66
67
- def report_progress (block_count , block_size , total_size ):
68
- global start_time
67
+ def format_time (seconds ):
68
+ minutes , seconds = divmod (seconds , 60 )
69
+ return "{:02}:{:05.2f}" .format (int (minutes ), seconds )
70
+
71
+ def report_progress (block_count , block_size , total_size , start_time ):
69
72
downloaded_size = block_count * block_size
70
73
time_elapsed = time .time () - start_time
71
74
current_speed = downloaded_size / (time_elapsed )
72
- elapsed = timedelta (seconds = time_elapsed )
73
-
74
- hours , remainder = divmod (time_elapsed , 3600 ) # 3600 seconds in an hour
75
- minutes , remainder = divmod (remainder , 60 )
76
- seconds , milliseconds = divmod (remainder , 1 )
77
- formatted_time = "{:02}:{:02}:{:02}.{:03}" .format (int (hours ), int (minutes ), int (seconds ), int (milliseconds * 1000 ))
78
75
79
76
if sys .stdout .isatty ():
80
77
if total_size > 0 :
81
78
percent_complete = min ((downloaded_size / total_size ) * 100 , 100 )
82
- sys .stdout .write (f"\r Downloading... { percent_complete :.2f} % - { downloaded_size / 1024 / 1024 :.2f} MB downloaded - Elapsed Time: { formatted_time } - Speed: { current_speed / 1024 / 1024 :.2f} MB/s" )
79
+ sys .stdout .write (f"\r Downloading... { percent_complete :.2f} % - { downloaded_size / 1024 / 1024 :.2f} MB downloaded - Elapsed Time: { format_time ( time_elapsed ) } - Speed: { current_speed / 1024 / 1024 :.2f} MB/s" )
83
80
else :
84
- sys .stdout .write (f"\r Downloading... { downloaded_size / 1024 / 1024 :.2f} MB downloaded - Elapsed Time: { formatted_time } - Speed: { current_speed / 1024 / 1024 :.2f} MB/s" )
81
+ sys .stdout .write (f"\r Downloading... { downloaded_size / 1024 / 1024 :.2f} MB downloaded - Elapsed Time: { format_time ( time_elapsed ) } - Speed: { current_speed / 1024 / 1024 :.2f} MB/s" )
85
82
sys .stdout .flush ()
86
83
87
- def format_time (seconds ):
88
- minutes , seconds = divmod (seconds , 60 )
89
- return "{:02}:{:05.2f}" .format (int (minutes ), seconds )
90
-
91
84
def print_verification_progress (total_files , i , t1 ):
92
85
if sys .stdout .isatty ():
93
86
sys .stdout .write (f'\r Elapsed time { format_time (time .time () - t1 )} ' )
@@ -106,7 +99,7 @@ def verify_files(filename, destination, rename_to):
106
99
local_path = os .path .join (extracted_dir_path , zipped_file .replace (first_dir , rename_to , 1 ))
107
100
if not os .path .exists (local_path ):
108
101
#print(f'\nMissing {zipped_file} on location: {extracted_dir_path}')
109
- print (f"\n Verification failed; aborted in { format_time (time .time () - t1 )} " )
102
+ print (f"Verification failed; aborted in { format_time (time .time () - t1 )} " )
110
103
return False
111
104
print_verification_progress (total_files , i , t1 )
112
105
except zipfile .BadZipFile :
@@ -154,7 +147,7 @@ def unpack(filename, destination, force_extract):
154
147
dirname = ''
155
148
file_is_corrupted = False
156
149
if (not force_extract ):
157
- print (f' > Verify... ' , end = "" , flush = True )
150
+ print (f' > Verify archive ... ' , end = "" , flush = True )
158
151
159
152
try :
160
153
if filename .endswith ('tar.gz' ):
@@ -197,12 +190,11 @@ def unpack(filename, destination, force_extract):
197
190
rename_to = 'esp32-arduino-libs'
198
191
199
192
if not force_extract :
200
- now = time .time ()
201
- if (verify_files (filename , destination , rename_to , now )):
193
+ if (verify_files (filename , destination , rename_to )):
202
194
print (" Files ok. Skipping Extraction" )
203
195
return True
204
196
else :
205
- print (" Failed. extracting " )
197
+ print (" Extracting archive... " )
206
198
else :
207
199
print (" Forcing extraction" )
208
200
@@ -224,7 +216,9 @@ def unpack(filename, destination, force_extract):
224
216
shutil .rmtree (rename_to )
225
217
shutil .move (dirname , rename_to )
226
218
227
- def download_file_with_progress (url ,filename ):
219
+ return True
220
+
221
+ def download_file_with_progress (url ,filename , start_time ):
228
222
import ssl
229
223
import contextlib
230
224
ctx = ssl .create_default_context ()
@@ -239,14 +233,14 @@ def download_file_with_progress(url,filename):
239
233
with open (filename ,'wb' ) as out_file :
240
234
out_file .write (block )
241
235
block_count += 1
242
- report_progress (block_count , block_size , total_size )
236
+ report_progress (block_count , block_size , total_size , start_time )
243
237
while True :
244
238
block = fp .read (block_size )
245
239
if not block :
246
240
break
247
241
out_file .write (block )
248
242
block_count += 1
249
- report_progress (block_count , block_size , total_size )
243
+ report_progress (block_count , block_size , total_size , start_time )
250
244
else :
251
245
raise Exception ('Non-existing file or connection error' )
252
246
@@ -271,7 +265,6 @@ def download_file(url,filename):
271
265
raise Exception ('Non-existing file or connection error' )
272
266
273
267
def get_tool (tool , force_download , force_extract ):
274
- global start_time
275
268
sys_name = platform .system ()
276
269
archive_name = tool ['archiveFileName' ]
277
270
local_path = dist_dir + archive_name
@@ -302,7 +295,7 @@ def get_tool(tool, force_download, force_extract):
302
295
try :
303
296
urlretrieve (url , local_path , report_progress )
304
297
except :
305
- download_file_with_progress (url , local_path )
298
+ download_file_with_progress (url , local_path , start_time )
306
299
sys .stdout .write (" - Done\n " )
307
300
sys .stdout .flush ()
308
301
else :
@@ -390,6 +383,6 @@ def print_help():
390
383
if (not get_tool (tool , force_download , force_extract )):
391
384
if (verbose ):
392
385
print (f"Tool { tool ['archiveFileName' ]} was corrupted. Re-downloading...\n " )
393
- if (not get_tool (tool , force_download , force_extract )): # Corrupted file was renamed, will not be found and will be re-downloaded
394
- print (f"Tool { tool } was corrupted, but re-downloading did not help!\n " )
386
+ if (not get_tool (tool , True , force_extract )): # Corrupted file was renamed, will not be found and will be re-downloaded
387
+ print (f"Tool { tool [ 'archiveFileName' ] } was corrupted, but re-downloading did not help!\n " )
395
388
print ('Platform Tools Installed' )
0 commit comments