Skip to content

Commit 82e6d38

Browse files
committed
refactor(get): Optimize get.py and make output more readable
1 parent 617da2f commit 82e6d38

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

tools/get.py

+38-35
Original file line numberDiff line numberDiff line change
@@ -101,52 +101,46 @@ def verify_files(filename, destination, rename_to):
101101
t1 = time.time()
102102
if filename.endswith(".zip"):
103103
try:
104-
with zipfile.ZipFile(filename, "r") as archive:
105-
first_dir = archive.namelist()[0].split("/")[0]
106-
total_files = len(archive.namelist())
107-
for i, zipped_file in enumerate(archive.namelist(), 1):
108-
local_path = os.path.join(extracted_dir_path, zipped_file.replace(first_dir, rename_to, 1))
109-
if not os.path.exists(local_path):
110-
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
111-
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
112-
return False
113-
print_verification_progress(total_files, i, t1)
104+
archive = zipfile.ZipFile(filename, "r")
105+
file_list = archive.namelist()
114106
except zipfile.BadZipFile:
115-
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
107+
if verbose:
108+
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
116109
return False
117110
elif filename.endswith(".tar.gz"):
118111
try:
119-
with tarfile.open(filename, "r:gz") as archive:
120-
first_dir = archive.getnames()[0].split("/")[0]
121-
total_files = len(archive.getnames())
122-
for i, zipped_file in enumerate(archive.getnames(), 1):
123-
local_path = os.path.join(extracted_dir_path, zipped_file.replace(first_dir, rename_to, 1))
124-
if not os.path.exists(local_path):
125-
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
126-
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
127-
return False
128-
print_verification_progress(total_files, i, t1)
112+
archive = tarfile.open(filename, "r:gz")
113+
file_list = archive.getnames()
129114
except tarfile.ReadError:
130-
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
115+
if verbose:
116+
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
131117
return False
132118
elif filename.endswith(".tar.xz"):
133119
try:
134-
with tarfile.open(filename, "r:xz") as archive:
135-
first_dir = archive.getnames()[0].split("/")[0]
136-
total_files = len(archive.getnames())
137-
for i, zipped_file in enumerate(archive.getnames(), 1):
138-
local_path = os.path.join(extracted_dir_path, zipped_file.replace(first_dir, rename_to, 1))
139-
if not os.path.exists(local_path):
140-
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
141-
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
142-
return False
143-
print_verification_progress(total_files, i, t1)
120+
archive = tarfile.open(filename, "r:xz")
121+
file_list = archive.getnames()
144122
except tarfile.ReadError:
145-
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
123+
if verbose:
124+
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
146125
return False
147126
else:
148127
raise NotImplementedError("Unsupported archive type")
149128

129+
try:
130+
first_dir = file_list[0].split("/")[0]
131+
total_files = len(file_list)
132+
for i, zipped_file in enumerate(file_list, 1):
133+
local_path = os.path.join(extracted_dir_path, zipped_file.replace(first_dir, rename_to, 1))
134+
if not os.path.exists(local_path):
135+
if verbose:
136+
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
137+
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
138+
return False
139+
print_verification_progress(total_files, i, t1)
140+
except Exception as e:
141+
print(f"\nError: {e}")
142+
return False
143+
150144
if verbose:
151145
print(f"\nVerification passed; completed in {format_time(time.time() - t1)}")
152146

@@ -231,7 +225,12 @@ def unpack(filename, destination, force_extract): # noqa: C901
231225
shutil.rmtree(rename_to)
232226
shutil.move(dirname, rename_to)
233227

234-
return True
228+
if verify_files(filename, destination, rename_to):
229+
print(" Files extracted successfully.")
230+
return True
231+
else:
232+
print(" Failed to extract files.")
233+
return False
235234

236235

237236
def download_file_with_progress(url, filename, start_time):
@@ -291,6 +290,7 @@ def get_tool(tool, force_download, force_extract):
291290
local_path = dist_dir + archive_name
292291
url = tool["url"]
293292
start_time = time.time()
293+
print("")
294294
if not os.path.isfile(local_path) or force_download:
295295
if verbose:
296296
print("Downloading '" + archive_name + "' to '" + local_path + "'")
@@ -421,6 +421,9 @@ def identify_platform():
421421
current_dir + "/../package/package_esp32_index.template.json", identified_platform
422422
)
423423
mkdir_p(dist_dir)
424+
425+
print("\nDownloading and extracting tools...")
426+
424427
for tool in tools_to_download:
425428
if is_test:
426429
print("Would install: {0}".format(tool["archiveFileName"]))
@@ -432,4 +435,4 @@ def identify_platform():
432435
print(f"Tool {tool['archiveFileName']} was corrupted, but re-downloading did not help!\n")
433436
sys.exit(1)
434437

435-
print("Platform Tools Installed")
438+
print("\nPlatform Tools Installed")

0 commit comments

Comments
 (0)