From 2b5700e4e11753023d40fb6c5902d05b3e332fb3 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Wed, 4 Jul 2018 14:50:30 +0200 Subject: [PATCH] Check the package file before installing it Make sure that the package file is there before we remove the existing installed files. --- cura/CuraPackageManager.py | 60 ++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/cura/CuraPackageManager.py b/cura/CuraPackageManager.py index d4ea445a4c..58a08ef733 100644 --- a/cura/CuraPackageManager.py +++ b/cura/CuraPackageManager.py @@ -308,43 +308,47 @@ class CuraPackageManager(QObject): package_id = package_info["package_id"] Logger.log("i", "Installing package [%s] from file [%s]", package_id, filename) - # remove it first and then install + # Load the cached package file and extract all contents to a temporary directory + if not os.path.exists(filename): + Logger.log("w", "Package [%s] file '%s' is missing, cannot install this package", package_id, filename) + return + try: + with zipfile.ZipFile(filename, "r") as archive: + temp_dir = tempfile.TemporaryDirectory() + archive.extractall(temp_dir.name) + except Exception: + Logger.logException("e", "Failed to install package from file [%s]", filename) + return + + from cura.CuraApplication import CuraApplication + installation_dirs_dict = { + "materials": Resources.getStoragePath(CuraApplication.ResourceTypes.MaterialInstanceContainer), + "qualities": Resources.getStoragePath(CuraApplication.ResourceTypes.QualityInstanceContainer), + "plugins": os.path.abspath(Resources.getStoragePath(Resources.Plugins)), + } + + # Remove it first and then install try: self._purgePackage(package_id) except: Logger.log("e", "There was an error deleting the package {package} during updating.".format(package = package_id)) return - if not os.path.exists(filename): - Logger.log("w", "Package [%s] file '%s' is missing, cannot install this package", package_id, filename) - return + # Copy the folders there + 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) + dst_dir_path = os.path.join(installation_root_dir, package_id) - # Install the package - with zipfile.ZipFile(filename, "r") as archive: + if not os.path.exists(src_dir_path): + continue + self.__installPackageFiles(package_id, src_dir_path, dst_dir_path) - temp_dir = tempfile.TemporaryDirectory() - archive.extractall(temp_dir.name) + # Remove the file + try: + os.remove(filename) + except Exception: + Logger.log("w", "Tried to delete file [%s], but it failed", filename) - from cura.CuraApplication import CuraApplication - installation_dirs_dict = { - "materials": Resources.getStoragePath(CuraApplication.ResourceTypes.MaterialInstanceContainer), - "qualities": Resources.getStoragePath(CuraApplication.ResourceTypes.QualityInstanceContainer), - "plugins": os.path.abspath(Resources.getStoragePath(Resources.Plugins)), - } - - 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) - dst_dir_path = os.path.join(installation_root_dir, package_id) - - if not os.path.exists(src_dir_path): - continue - self.__installPackageFiles(package_id, src_dir_path, dst_dir_path) - - # Remove the file - try: - os.remove(filename) - except Exception: - Logger.log("w", "Tried to delete file [%s], but it failed", filename) # Move the info to the installed list of packages only when it succeeds self._installed_package_dict[package_id] = self._to_install_package_dict[package_id]