mirror of
https://github.com/dortania/OpenCore-Legacy-Patcher.git
synced 2026-06-20 22:20:53 +10:00
network_handler.py: Match formatting and check download path space
This commit is contained in:
@@ -139,10 +139,10 @@ class DownloadObject:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
self.status = DownloadStatus.DOWNLOADING
|
self.status = DownloadStatus.DOWNLOADING
|
||||||
logging.info(f"Starting download: {self.filename}")
|
logging.info(f"- Starting download: {self.filename}")
|
||||||
if spawn_thread:
|
if spawn_thread:
|
||||||
if self.active_thread:
|
if self.active_thread:
|
||||||
logging.error("Download already in progress")
|
logging.error("- Download already in progress")
|
||||||
return
|
return
|
||||||
self.should_checksum = verify_checksum
|
self.should_checksum = verify_checksum
|
||||||
self.active_thread = threading.Thread(target=self._download, args=(display_progress,))
|
self.active_thread = threading.Thread(target=self._download, args=(display_progress,))
|
||||||
@@ -201,8 +201,8 @@ class DownloadObject:
|
|||||||
else:
|
else:
|
||||||
raise Exception("Content-Length missing from headers")
|
raise Exception("Content-Length missing from headers")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Error determining file size {self.url}: {str(e)}")
|
logging.error(f"- Error determining file size {self.url}: {str(e)}")
|
||||||
logging.error("Assuming file size is 0")
|
logging.error("- Assuming file size is 0")
|
||||||
self.total_file_size = 0.0
|
self.total_file_size = 0.0
|
||||||
|
|
||||||
|
|
||||||
@@ -229,17 +229,17 @@ class DownloadObject:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if Path(path).exists():
|
if Path(path).exists():
|
||||||
logging.info(f"Deleting existing file: {path}")
|
logging.info(f"- Deleting existing file: {path}")
|
||||||
Path(path).unlink()
|
Path(path).unlink()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if not Path(path).parent.exists():
|
if not Path(path).parent.exists():
|
||||||
logging.info(f"Creating directory: {Path(path).parent}")
|
logging.info(f"- Creating directory: {Path(path).parent}")
|
||||||
Path(path).parent.mkdir(parents=True, exist_ok=True)
|
Path(path).parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
available_space = utilities.get_free_space()
|
available_space = utilities.get_free_space(Path(path).parent)
|
||||||
if self.total_file_size > available_space:
|
if self.total_file_size > available_space:
|
||||||
msg = f"Not enough free space to download {self.filename}, need {utilities.human_fmt(self.total_file_size)}, have {utilities.human_fmt(available_space)}"
|
msg = f"- Not enough free space to download {self.filename}, need {utilities.human_fmt(self.total_file_size)}, have {utilities.human_fmt(available_space)}"
|
||||||
logging.error(msg)
|
logging.error(msg)
|
||||||
raise Exception(msg)
|
raise Exception(msg)
|
||||||
|
|
||||||
@@ -247,7 +247,7 @@ class DownloadObject:
|
|||||||
self.error = True
|
self.error = True
|
||||||
self.error_msg = str(e)
|
self.error_msg = str(e)
|
||||||
self.status = DownloadStatus.ERROR
|
self.status = DownloadStatus.ERROR
|
||||||
logging.error(f"Error preparing working directory {path}: {self.error_msg}")
|
logging.error(f"- Error preparing working directory {path}: {self.error_msg}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@@ -286,21 +286,21 @@ class DownloadObject:
|
|||||||
if display_progress and i % 100:
|
if display_progress and i % 100:
|
||||||
# Don't use logging here, as we'll be spamming the log file
|
# Don't use logging here, as we'll be spamming the log file
|
||||||
if self.total_file_size == 0.0:
|
if self.total_file_size == 0.0:
|
||||||
print(f"Downloaded {utilities.human_fmt(self.downloaded_file_size)} of {self.filename}")
|
print(f"- Downloaded {utilities.human_fmt(self.downloaded_file_size)} of {self.filename}")
|
||||||
else:
|
else:
|
||||||
print(f"Downloaded {self.get_percent():.2f}% of {self.filename} ({utilities.human_fmt(self.get_speed())}/s) ({self.get_time_remaining():.2f} seconds remaining)")
|
print(f"- Downloaded {self.get_percent():.2f}% of {self.filename} ({utilities.human_fmt(self.get_speed())}/s) ({self.get_time_remaining():.2f} seconds remaining)")
|
||||||
self.download_complete = True
|
self.download_complete = True
|
||||||
logging.info(f"Download complete: {self.filename}")
|
logging.info(f"- Download complete: {self.filename}")
|
||||||
logging.info("Stats:")
|
logging.info("- Stats:")
|
||||||
logging.info(f" Downloaded size: {utilities.human_fmt(self.downloaded_file_size)}")
|
logging.info(f"- Downloaded size: {utilities.human_fmt(self.downloaded_file_size)}")
|
||||||
logging.info(f" Time elapsed: {(time.time() - self.start_time):.2f} seconds")
|
logging.info(f"- Time elapsed: {(time.time() - self.start_time):.2f} seconds")
|
||||||
logging.info(f" Speed: {utilities.human_fmt(self.downloaded_file_size / (time.time() - self.start_time))}/s")
|
logging.info(f"- Speed: {utilities.human_fmt(self.downloaded_file_size / (time.time() - self.start_time))}/s")
|
||||||
logging.info(f" Location: {self.filepath}")
|
logging.info(f"- Location: {self.filepath}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.error = True
|
self.error = True
|
||||||
self.error_msg = str(e)
|
self.error_msg = str(e)
|
||||||
self.status = DownloadStatus.ERROR
|
self.status = DownloadStatus.ERROR
|
||||||
logging.error(f"Error downloading {self.url}: {self.error_msg}")
|
logging.error(f"- Error downloading {self.url}: {self.error_msg}")
|
||||||
|
|
||||||
self.status = DownloadStatus.COMPLETE
|
self.status = DownloadStatus.COMPLETE
|
||||||
utilities.enable_sleep_after_running()
|
utilities.enable_sleep_after_running()
|
||||||
@@ -315,7 +315,7 @@ class DownloadObject:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if self.total_file_size == 0.0:
|
if self.total_file_size == 0.0:
|
||||||
logging.error("File size is 0, cannot calculate percent")
|
logging.error("- File size is 0, cannot calculate percent")
|
||||||
return -1
|
return -1
|
||||||
return self.downloaded_file_size / self.total_file_size * 100
|
return self.downloaded_file_size / self.total_file_size * 100
|
||||||
|
|
||||||
@@ -340,7 +340,7 @@ class DownloadObject:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if self.total_file_size == 0.0:
|
if self.total_file_size == 0.0:
|
||||||
logging.error("File size is 0, cannot calculate time remaining")
|
logging.error("- File size is 0, cannot calculate time remaining")
|
||||||
return -1
|
return -1
|
||||||
return (self.total_file_size - self.downloaded_file_size) / self.get_speed()
|
return (self.total_file_size - self.downloaded_file_size) / self.get_speed()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user