Reading packages from archive now uses With context

This commit is contained in:
Jaime van Kessel 2018-05-03 20:22:38 +02:00
parent defb22dc07
commit 7665f8a7cb
2 changed files with 31 additions and 35 deletions

View file

@ -190,6 +190,8 @@ class CuraPackageManager(QObject):
try: try:
# Get package information # Get package information
package_info = self.getPackageInfo(filename) package_info = self.getPackageInfo(filename)
if not package_info:
return
package_id = package_info["package_id"] package_id = package_info["package_id"]
# Check the delayed installation and removal lists first # Check the delayed installation and removal lists first
@ -282,30 +284,28 @@ class CuraPackageManager(QObject):
self._purgePackage(package_id) self._purgePackage(package_id)
# Install the package # Install the package
archive = zipfile.ZipFile(filename, "r") with zipfile.ZipFile(filename, "r") as archive:
temp_dir = tempfile.TemporaryDirectory() temp_dir = tempfile.TemporaryDirectory()
archive.extractall(temp_dir.name) archive.extractall(temp_dir.name)
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
installation_dirs_dict = { installation_dirs_dict = {
"materials": Resources.getStoragePath(CuraApplication.ResourceTypes.MaterialInstanceContainer), "materials": Resources.getStoragePath(CuraApplication.ResourceTypes.MaterialInstanceContainer),
"quality": Resources.getStoragePath(CuraApplication.ResourceTypes.QualityInstanceContainer), "quality": Resources.getStoragePath(CuraApplication.ResourceTypes.QualityInstanceContainer),
"plugins": os.path.abspath(Resources.getStoragePath(Resources.Plugins)), "plugins": os.path.abspath(Resources.getStoragePath(Resources.Plugins)),
} }
for sub_dir_name, installation_root_dir in installation_dirs_dict.items(): for sub_dir_name, installation_root_dir in installation_dirs_dict.items():
src_dir_path = os.path.join(temp_dir.name, "files", sub_dir_name) src_dir_path = os.path.join(temp_dir.name, "files", sub_dir_name)
dst_dir_path = os.path.join(installation_root_dir, package_id) dst_dir_path = os.path.join(installation_root_dir, package_id)
if not os.path.exists(src_dir_path): if not os.path.exists(src_dir_path):
continue continue
# Need to rename the container files so they don't get ID conflicts # Need to rename the container files so they don't get ID conflicts
to_rename_files = sub_dir_name not in ("plugins",) to_rename_files = sub_dir_name not in ("plugins",)
self.__installPackageFiles(package_id, src_dir_path, dst_dir_path, need_to_rename_files= to_rename_files) self.__installPackageFiles(package_id, src_dir_path, dst_dir_path, need_to_rename_files= to_rename_files)
archive.close()
# Remove the file # Remove the file
os.remove(filename) os.remove(filename)
@ -325,16 +325,15 @@ class CuraPackageManager(QObject):
# Gets package information from the given file. # Gets package information from the given file.
def getPackageInfo(self, filename: str) -> dict: def getPackageInfo(self, filename: str) -> dict:
archive = zipfile.ZipFile(filename, "r") with zipfile.ZipFile(filename, "r") as archive:
try: try:
# All information is in package.json # All information is in package.json
with archive.open("package.json", "r") as f: with archive.open("package.json", "r") as f:
package_info_dict = json.loads(f.read().decode("utf-8")) package_info_dict = json.loads(f.read().decode("utf-8"))
return package_info_dict return package_info_dict
except Exception as e: except Exception as e:
raise RuntimeError("Could not get package information from file '%s': %s" % (filename, e)) Logger.logException("Could not get package information from file '%s': %s" % (filename, e))
finally: return {}
archive.close()
# Gets the license file content if present in the given package file. # Gets the license file content if present in the given package file.
# Returns None if there is no license file found. # Returns None if there is no license file found.

View file

@ -420,10 +420,9 @@ class Toolbox(QObject, Extension):
def _onDownloadComplete(self, file_path: str): def _onDownloadComplete(self, file_path: str):
Logger.log("i", "Toolbox: Download complete.") Logger.log("i", "Toolbox: Download complete.")
try: package_info = self._package_manager.getPackageInfo(file_path)
package_info = self._package_manager.getPackageInfo(file_path) if not package_info:
except: Logger.log("w", "Toolbox: Package file [%s] was not a valid CuraPackage.", file_path)
Logger.logException("w", "Toolbox: Package file [%s] was not a valid CuraPackage.", file_path)
return return
license_content = self._package_manager.getPackageLicense(file_path) license_content = self._package_manager.getPackageLicense(file_path)
@ -434,8 +433,6 @@ class Toolbox(QObject, Extension):
self.install(file_path) self.install(file_path)
return return
# Getter & Setters for Properties: # Getter & Setters for Properties:
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
def setDownloadProgress(self, progress: int): def setDownloadProgress(self, progress: int):