diff --git a/Mergin/plugin.py b/Mergin/plugin.py index cd81f52b..c53ab1ab 100644 --- a/Mergin/plugin.py +++ b/Mergin/plugin.py @@ -67,9 +67,28 @@ from .processing.provider import MerginProvider import processing +from pyplugin_installer.installer import QgsPluginInstaller + MERGIN_CLIENT_LOG = os.path.join(QgsApplication.qgisSettingsDirPath(), "mergin-client-log.txt") os.environ["MERGIN_CLIENT_LOG"] = MERGIN_CLIENT_LOG +# store method that will be monkeypatched +_original_method = QgsPluginInstaller.installPlugin + + +def install_plugin(self, key, quiet=False, stable=True): + """ + On Windows we need to release lock on geodiff library and unload plugin before + performing an update. See https://github.com/MerginMaps/qgis-mergin-plugin/issues/504 + and https://github.com/MerginMaps/geodiff/issues/205 + """ + if key == "Mergin" and os.name == "nt": + from qgis.utils import unloadPlugin + + unloadPlugin(key) + + _original_method(self, key, quiet, stable) + class MerginPlugin: def __init__(self, iface): @@ -185,6 +204,11 @@ def initGui(self): QgsProject.instance().layersAdded.connect(self.add_context_menu_actions) + # monkeypatch plugin installer to allow unlocking geodiff library and unloading plugin before installing + # see https://github.com/MerginMaps/qgis-mergin-plugin/issues/504, https://github.com/MerginMaps/geodiff/issues/205 + if os.name == "nt": + QgsPluginInstaller.installPlugin = install_plugin + def add_action( self, icon_name, @@ -547,6 +571,16 @@ def unload(self): QgsApplication.processingRegistry().removeProvider(self.provider) + # unlock geodiff library and revert monkeypatching + if os.name == "nt": + from _ctypes import FreeLibrary + from .mergin.deps import pygeodiff + + geodiff = pygeodiff.GeoDiff() + FreeLibrary(geodiff.clib.lib._handle) + + QgsPluginInstaller.installPlugin = _original_method + def view_local_changes(self): project_path = QgsProject.instance().homePath() if not project_path: