Explicitly close the downloaded curapackage

Sometime very small curapackage where download and the `canInstallChanged`
signal was emitted before the zipfile was completely processed.
This could result in a failed installation, which was often the case for
materials.

I also narrowed down the try-catch block.
This commit is contained in:
Jelle Spijker 2021-12-15 15:07:23 +01:00
parent 5f444fa5b7
commit 8b2ced122c
No known key found for this signature in database
GPG key ID: 6662DC033BE6B99A

View file

@ -209,15 +209,12 @@ class PackageList(ListModel):
) )
def _downloadFinished(self, package_id: str, reply: "QNetworkReply", update: bool = False) -> None: def _downloadFinished(self, package_id: str, reply: "QNetworkReply", update: bool = False) -> None:
try:
with tempfile.NamedTemporaryFile(mode = "wb+", suffix = ".curapackage", delete = False) as temp_file: with tempfile.NamedTemporaryFile(mode = "wb+", suffix = ".curapackage", delete = False) as temp_file:
try:
bytes_read = reply.read(self.DISK_WRITE_BUFFER_SIZE) bytes_read = reply.read(self.DISK_WRITE_BUFFER_SIZE)
while bytes_read: while bytes_read:
temp_file.write(bytes_read) temp_file.write(bytes_read)
bytes_read = reply.read(self.DISK_WRITE_BUFFER_SIZE) bytes_read = reply.read(self.DISK_WRITE_BUFFER_SIZE)
self._to_install[package_id] = temp_file.name
self._ongoing_requests["download_package"] = None
self.canInstallChanged.emit(package_id, update)
except IOError as e: except IOError as e:
Logger.error(f"Failed to write downloaded package to temp file {e}") Logger.error(f"Failed to write downloaded package to temp file {e}")
temp_file.close() temp_file.close()
@ -226,7 +223,12 @@ class PackageList(ListModel):
# Setting the ownership of this object to not qml can still result in a RuntimeError. Which can occur when quickly toggling # Setting the ownership of this object to not qml can still result in a RuntimeError. Which can occur when quickly toggling
# between de-/constructing Remote or Local PackageLists. This try-except is here to prevent a hard crash when the wrapped C++ object # between de-/constructing Remote or Local PackageLists. This try-except is here to prevent a hard crash when the wrapped C++ object
# was deleted when it was still parsing the response # was deleted when it was still parsing the response
temp_file.close()
return return
temp_file.close()
self._to_install[package_id] = temp_file.name
self._ongoing_requests["download_package"] = None
self.canInstallChanged.emit(package_id, update)
def _downloadError(self, package_id: str, update: bool = False, reply: Optional["QNetworkReply"] = None, error: Optional["QNetworkReply.NetworkError"] = None) -> None: def _downloadError(self, package_id: str, update: bool = False, reply: Optional["QNetworkReply"] = None, error: Optional["QNetworkReply.NetworkError"] = None) -> None:
if reply: if reply: