fix(get.py): Verify files after extraction and make it more readable

This commit is contained in:
Lucas Saavedra Vaz 2024-06-06 12:26:14 -03:00
parent e3452ea6e1
commit cc40edb407

View File

@ -107,12 +107,14 @@ def verify_files(filename, destination, rename_to):
for i, zipped_file in enumerate(archive.namelist(), 1):
local_path = os.path.join(extracted_dir_path, zipped_file.replace(first_dir, rename_to, 1))
if not os.path.exists(local_path):
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
if verbose:
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
return False
print_verification_progress(total_files, i, t1)
except zipfile.BadZipFile:
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
if verbose:
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
return False
elif filename.endswith(".tar.gz"):
try:
@ -122,12 +124,14 @@ def verify_files(filename, destination, rename_to):
for i, zipped_file in enumerate(archive.getnames(), 1):
local_path = os.path.join(extracted_dir_path, zipped_file.replace(first_dir, rename_to, 1))
if not os.path.exists(local_path):
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
if verbose:
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
return False
print_verification_progress(total_files, i, t1)
except tarfile.ReadError:
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
if verbose:
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
return False
elif filename.endswith(".tar.xz"):
try:
@ -137,12 +141,14 @@ def verify_files(filename, destination, rename_to):
for i, zipped_file in enumerate(archive.getnames(), 1):
local_path = os.path.join(extracted_dir_path, zipped_file.replace(first_dir, rename_to, 1))
if not os.path.exists(local_path):
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
if verbose:
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
return False
print_verification_progress(total_files, i, t1)
except tarfile.ReadError:
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
if verbose:
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
return False
else:
raise NotImplementedError("Unsupported archive type")
@ -231,7 +237,12 @@ def unpack(filename, destination, force_extract): # noqa: C901
shutil.rmtree(rename_to)
shutil.move(dirname, rename_to)
return True
if verify_files(filename, destination, rename_to):
print(" Files extracted successfully.")
return True
else:
print(" Failed to extract files.")
return False
def download_file_with_progress(url, filename, start_time):
@ -291,6 +302,7 @@ def get_tool(tool, force_download, force_extract):
local_path = dist_dir + archive_name
url = tool["url"]
start_time = time.time()
print("")
if not os.path.isfile(local_path) or force_download:
if verbose:
print("Downloading '" + archive_name + "' to '" + local_path + "'")
@ -421,6 +433,9 @@ if __name__ == "__main__":
current_dir + "/../package/package_esp32_index.template.json", identified_platform
)
mkdir_p(dist_dir)
print("\nDownloading and extracting tools...")
for tool in tools_to_download:
if is_test:
print("Would install: {0}".format(tool["archiveFileName"]))
@ -432,4 +447,4 @@ if __name__ == "__main__":
print(f"Tool {tool['archiveFileName']} was corrupted, but re-downloading did not help!\n")
sys.exit(1)
print("Platform Tools Installed")
print("\nPlatform Tools Installed")