Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2d2e5b5

Browse files
PilnyTomasme-no-dev
authored andcommittedOct 5, 2023
Minor fixes
1 parent 3670c8b commit 2d2e5b5

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed
 

‎tools/get.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,23 @@ def mkdir_p(path):
6464
if exc.errno != errno.EEXIST or not os.path.isdir(path):
6565
raise
6666

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):
6972
downloaded_size = block_count * block_size
7073
time_elapsed = time.time() - start_time
7174
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))
7875

7976
if sys.stdout.isatty():
8077
if total_size > 0:
8178
percent_complete = min((downloaded_size / total_size) * 100, 100)
82-
sys.stdout.write(f"\rDownloading... {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"\rDownloading... {percent_complete:.2f}% - {downloaded_size / 1024 / 1024:.2f} MB downloaded - Elapsed Time: {format_time(time_elapsed)} - Speed: {current_speed / 1024 / 1024:.2f} MB/s")
8380
else:
84-
sys.stdout.write(f"\rDownloading... {downloaded_size / 1024 / 1024:.2f} MB downloaded - Elapsed Time: {formatted_time} - Speed: {current_speed / 1024 / 1024:.2f} MB/s")
81+
sys.stdout.write(f"\rDownloading... {downloaded_size / 1024 / 1024:.2f} MB downloaded - Elapsed Time: {format_time(time_elapsed)} - Speed: {current_speed / 1024 / 1024:.2f} MB/s")
8582
sys.stdout.flush()
8683

87-
def format_time(seconds):
88-
minutes, seconds = divmod(seconds, 60)
89-
return "{:02}:{:05.2f}".format(int(minutes), seconds)
90-
9184
def print_verification_progress(total_files, i, t1):
9285
if sys.stdout.isatty():
9386
sys.stdout.write(f'\rElapsed time {format_time(time.time() - t1)}')
@@ -106,7 +99,7 @@ def verify_files(filename, destination, rename_to):
10699
local_path = os.path.join(extracted_dir_path, zipped_file.replace(first_dir, rename_to, 1))
107100
if not os.path.exists(local_path):
108101
#print(f'\nMissing {zipped_file} on location: {extracted_dir_path}')
109-
print(f"\nVerification failed; aborted in {format_time(time.time() - t1)}")
102+
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
110103
return False
111104
print_verification_progress(total_files, i , t1)
112105
except zipfile.BadZipFile:
@@ -154,7 +147,7 @@ def unpack(filename, destination, force_extract):
154147
dirname = ''
155148
file_is_corrupted=False
156149
if(not force_extract):
157-
print(f' > Verify... ', end="", flush=True)
150+
print(f' > Verify archive... ', end="", flush=True)
158151

159152
try:
160153
if filename.endswith('tar.gz'):
@@ -197,12 +190,11 @@ def unpack(filename, destination, force_extract):
197190
rename_to = 'esp32-arduino-libs'
198191

199192
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)):
202194
print(" Files ok. Skipping Extraction")
203195
return True
204196
else:
205-
print(" Failed. extracting")
197+
print(" Extracting archive...")
206198
else:
207199
print(" Forcing extraction")
208200

@@ -224,7 +216,9 @@ def unpack(filename, destination, force_extract):
224216
shutil.rmtree(rename_to)
225217
shutil.move(dirname, rename_to)
226218

227-
def download_file_with_progress(url,filename):
219+
return True
220+
221+
def download_file_with_progress(url,filename, start_time):
228222
import ssl
229223
import contextlib
230224
ctx = ssl.create_default_context()
@@ -239,14 +233,14 @@ def download_file_with_progress(url,filename):
239233
with open(filename,'wb') as out_file:
240234
out_file.write(block)
241235
block_count += 1
242-
report_progress(block_count, block_size, total_size)
236+
report_progress(block_count, block_size, total_size, start_time)
243237
while True:
244238
block = fp.read(block_size)
245239
if not block:
246240
break
247241
out_file.write(block)
248242
block_count += 1
249-
report_progress(block_count, block_size, total_size)
243+
report_progress(block_count, block_size, total_size, start_time)
250244
else:
251245
raise Exception('Non-existing file or connection error')
252246

@@ -271,7 +265,6 @@ def download_file(url,filename):
271265
raise Exception ('Non-existing file or connection error')
272266

273267
def get_tool(tool, force_download, force_extract):
274-
global start_time
275268
sys_name = platform.system()
276269
archive_name = tool['archiveFileName']
277270
local_path = dist_dir + archive_name
@@ -302,7 +295,7 @@ def get_tool(tool, force_download, force_extract):
302295
try:
303296
urlretrieve(url, local_path, report_progress)
304297
except:
305-
download_file_with_progress(url, local_path)
298+
download_file_with_progress(url, local_path, start_time)
306299
sys.stdout.write(" - Done\n")
307300
sys.stdout.flush()
308301
else:
@@ -390,6 +383,6 @@ def print_help():
390383
if(not get_tool(tool, force_download, force_extract)):
391384
if(verbose):
392385
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")
395388
print('Platform Tools Installed')

0 commit comments

Comments
 (0)
Please sign in to comment.