Skip to content
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

[WIP] unload plugin and free library before upgrading #534

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Mergin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -547,6 +571,22 @@ def unload(self):

QgsApplication.processingRegistry().removeProvider(self.provider)

# unlock geodiff library and revert monkeypatching
if os.name == "nt":
try:
lib_path = os.path.join(os.path.dirname(__file__), "mergin", "deps", "pygeodiff")
for f in os.listdir(lib_path):
if f.lower().endswith(".pyd"):
os.remove(os.path.join(lib_path, f))
PeterPetrik marked this conversation as resolved.
Show resolved Hide resolved
except:
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:
Expand Down
Loading