-
-
Notifications
You must be signed in to change notification settings - Fork 3
Plugin installation #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Plugin installation #125
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4b4e413
Plugin installation
domi4484 d7a3ba1
Update docs
domi4484 63cda6f
Use pyplugin_installer
domi4484 a25401e
Check installation success
domi4484 2701bcf
Warning for qgis < 3.44.8
domi4484 0e47074
Add infos for profile and installed plugin version
domi4484 917bb47
Apply suggestions from code review
domi4484 037dc75
Don't pass iface all along the plugin
domi4484 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import os | ||
| import shutil | ||
| from zipfile import ZipFile | ||
|
|
||
| from qgis.PyQt.QtCore import QUrl | ||
| from qgis.PyQt.QtGui import QDesktopServices | ||
|
|
@@ -13,8 +14,7 @@ | |
|
|
||
|
|
||
| class PluginWidget(QWidget, DIALOG_UI): | ||
|
|
||
| def __init__(self, parent=None): | ||
| def __init__(self, parent=None, qgis_iface=None): | ||
| QWidget.__init__(self, parent) | ||
| self.setupUi(self) | ||
|
|
||
|
|
@@ -23,9 +23,17 @@ def __init__(self, parent=None): | |
| self.copyZipToDirectory_pushButton.clicked.connect(self.__copyZipToDirectoryClicked) | ||
|
|
||
| self.__current_module_package = None | ||
| self.__plugin_name = None | ||
| self.__iface = qgis_iface | ||
|
||
|
|
||
| if self.__iface: | ||
| self.qgisProfile_label.setText(self.__iface.userProfileManager().userProfile().name()) | ||
| else: | ||
| self.qgisProfile_label.setText("Unknown") | ||
domi4484 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def setModulePackage(self, module_package: ModulePackage): | ||
| self.__current_module_package = module_package | ||
| self.__plugin_name = None | ||
| self.__packagePrepareGetPluginFilename() | ||
|
|
||
| def clearModulePackage(self): | ||
|
|
@@ -58,6 +66,20 @@ def __packagePrepareGetPluginFilename(self): | |
| QtUtils.setFontItalic(self.info_label, True) | ||
| return | ||
|
|
||
| # Get the plugin name | ||
| self.__plugin_name = self.__extractPluginName(asset_plugin.package_zip) | ||
| if not self.__plugin_name: | ||
| self.info_label.setText( | ||
| self.tr(f"Couldn't determinate the plugin name for '{asset_plugin.package_zip}'.") | ||
| ) | ||
| QtUtils.setForegroundColor(self.info_label, PluginUtils.COLOR_WARNING) | ||
| QtUtils.setFontItalic(self.info_label, True) | ||
| return | ||
|
|
||
| # Get the installed plugin current version | ||
| version = self.__getInstalledPluginVersion(self.__plugin_name) | ||
| self.currentVersion_label.setText(version) | ||
|
|
||
| QtUtils.resetForegroundColor(self.info_label) | ||
| QtUtils.setFontItalic(self.info_label, False) | ||
| self.info_label.setText( | ||
|
|
@@ -76,21 +98,61 @@ def __installClicked(self): | |
| # Check if the package exists | ||
| asset_plugin = self.__current_module_package.asset_plugin | ||
| if not os.path.exists(asset_plugin.package_zip): | ||
| self.info_label.setText( | ||
| self.tr(f"Plugin zip file '{asset_plugin.package_zip}' does not exist.") | ||
| QMessageBox.critical( | ||
| self, | ||
| self.tr("Error"), | ||
| self.tr(f"Plugin zip file '{asset_plugin.package_zip}' does not exist."), | ||
| ) | ||
| QtUtils.setForegroundColor(self.info_label, PluginUtils.COLOR_WARNING) | ||
| QtUtils.setFontItalic(self.info_label, True) | ||
| return | ||
|
|
||
| QMessageBox.warning( | ||
| self, | ||
| self.tr("Not implemented"), | ||
| self.tr( | ||
| 'Installation is not implemented yet.\nAt the moment, you can only copy the plugin zip file to a directory and use "Install from ZIP" in QGIS.' | ||
| ), | ||
| ) | ||
| return | ||
| try: | ||
| from pyplugin_installer import instance as plugin_installer_instance | ||
| from qgis.core import Qgis | ||
| except ImportError: | ||
| QMessageBox.critical( | ||
| self, | ||
| self.tr("Error"), | ||
| self.tr("Plugin installation is not possible when oQtopus is running standalone."), | ||
| ) | ||
| return | ||
|
|
||
| try: | ||
| installer = plugin_installer_instance() | ||
| success = installer.installFromZipFile(asset_plugin.package_zip) | ||
|
|
||
| # installFromZipFile return success from QGIS 3.44.08 | ||
| if Qgis.QGIS_VERSION_INT < 34408: | ||
| version = self.__getInstalledPluginVersion(self.__plugin_name) | ||
| QMessageBox.information( | ||
| self, | ||
| self.tr("Installation finished"), | ||
| self.tr(f"Current '{self.__plugin_name}' plugin version is {version}"), | ||
| ) | ||
| self.__packagePrepareGetPluginFilename() | ||
| return | ||
|
|
||
| if not success: | ||
| QMessageBox.critical( | ||
| self, | ||
| self.tr("Error"), | ||
| self.tr(f"Plugin '{self.__plugin_name}' installation failed."), | ||
| ) | ||
| return | ||
|
|
||
| QMessageBox.information( | ||
| self, | ||
| self.tr("Success"), | ||
| self.tr(f"Plugin '{self.__plugin_name}' installed successfully."), | ||
| ) | ||
| self.__packagePrepareGetPluginFilename() | ||
|
|
||
| except Exception as e: | ||
| QMessageBox.critical( | ||
| self, | ||
| self.tr("Error"), | ||
| self.tr("Plugin installation failed with an exception: {0}").format(str(e)), | ||
| ) | ||
| return | ||
|
|
||
| def __seeChangelogClicked(self): | ||
| if self.__current_module_package is None: | ||
|
|
@@ -168,3 +230,23 @@ def __copyZipToDirectoryClicked(self): | |
| self.tr(f"Failed to copy plugin package: {e}"), | ||
| ) | ||
| return | ||
|
|
||
| def __extractPluginName(self, package_zip: str) -> str: | ||
| with ZipFile(package_zip, "r") as zip_ref: | ||
| for name in zip_ref.namelist(): | ||
| print(f"name: {name}") | ||
| if name.endswith("/metadata.txt"): | ||
| return name.split("/")[0] | ||
| return "" | ||
|
|
||
| def __getInstalledPluginVersion(self, plugin_name: str): | ||
| if not self.__iface: | ||
| return self.tr("Unknown") | ||
|
|
||
| import qgis | ||
|
|
||
| version = qgis.utils.pluginMetadata(plugin_name, "version") | ||
| if version == "__error__": | ||
| return self.tr("Not installed") | ||
|
|
||
| return version | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.