Skip to content

Commit

Permalink
[brazil-data-cube#68] Adding pip install libs check in plugin start
Browse files Browse the repository at this point in the history
  • Loading branch information
AbnerErnaniADSFatec committed Sep 17, 2024
1 parent ea6b7ea commit cb2e1ec
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ parts/
sdist/
var/
wheels/
plugins/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
Expand Down Expand Up @@ -195,7 +196,9 @@ services_storage_user_application.json
# Ignore pb_tool builds
*zip_build

# Ignore libs
# Ignore libs path
*lib

*lib-paths.txt

*requirements.txt
27 changes: 27 additions & 0 deletions scripts/build_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# This file is part of Python QGIS Plugin for WTSS.
# Copyright (C) 2024 INPE.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
#

import distutils.core
from pathlib import Path

dist = distutils.core.run_setup("setup.py")

file = open(Path('wtss_plugin') / 'requirements.txt','w')
for req in dist.install_requires:
file.write(str(req) + "\n")
file.close()
2 changes: 1 addition & 1 deletion scripts/get-source-codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import wtss
lib_paths.append(wtss.__file__)

file = open(Path('scripts') / 'lib-paths.txt','w')
file = open(Path('scripts') / 'lib-paths.txt', 'w')
for path in lib_paths:
file.write(str(path).replace('__init__.py', '') + "\n")
file.close()
3 changes: 3 additions & 0 deletions scripts/linux/make-lib-path.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ done

echo "Removing backends for matplotlib..."
rm -R $LIB_PATH/matplotlib/backends/web_backend/jquery

echo "Removing _pycache__..."
find $LIB_PATH -type f -name "__pycache__" -exec rm -r {} \;
9 changes: 7 additions & 2 deletions scripts/linux/run-qgis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
#
#!/bin/bash

xhost +

docker run --rm --name="qgis-3-desktop" \
docker run --rm \
--interactive \
--tty \
--name="qgis-3-desktop" \
-i -t \
-v ${PWD}:/home/wtss_plugin \
-v ${PWD}/plugins:/root/.local/share/QGIS/QGIS3/profiles/default/python/plugins/ \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e DISPLAY=unix$DISPLAY \
qgis/qgis qgis
qgis/qgis:release-3_32 qgis

xhost -
1 change: 1 addition & 0 deletions scripts/linux/run-tests-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
#
#!/bin/bash

pydocstyle wtss_plugin/*.py wtss_plugin/controller/*.py wtss_plugin/controller/files_export/*.py setup.py && \
isort wtss_plugin setup.py --check-only --diff && \
Expand Down
85 changes: 73 additions & 12 deletions wtss_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,49 @@

"""Python QGIS Plugin for WTSS."""

import os
from pathlib import Path

def lib_path():
"""Get the path for python installed lib path."""
return str(Path(os.path.abspath(os.path.dirname(__file__))) / 'lib')

def lib_path_end():
"""Get the path for python installed lib path."""
return str(os.path.join(str(Path(os.path.abspath(os.path.dirname(__file__))) / 'lib'), ''))

def get_lib_paths():
"""Get the path for python installed lib path."""
return [lib_path(), lib_path_end()]

def requirements_file():
"""Get the path for requirements file."""
return str(Path(os.path.abspath(os.path.dirname(__file__))) / 'requirements.txt')

def warning(title, message):
"""Show a simple warning when ImportError."""
from PyQt5.QtWidgets import QMessageBox
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setWindowTitle(title)
msg.setText(message)
msg.setStandardButtons(QMessageBox.Ok)
return msg.exec_()

def set_lib_path():
"""Setting lib path for installed libraries."""
import sys
if lib_path() in sys.path:
sys.path.remove(lib_path())
if lib_path_end() in sys.path:
sys.path.remove(lib_path_end())
sys.path = get_lib_paths() + sys.path

def start(iface):
"""Start WTSS QGIS Plugin"""
#
# Setting PYTHONPATH to use dependencies
import os
import sys
from pathlib import Path
sys.path.append(str(Path(os.path.abspath(os.path.dirname(__file__))) / 'lib'))
set_lib_path()
#
# Start plugin GUI
from .wtss_qgis import wtss_qgis
Expand All @@ -39,11 +74,37 @@ def classFactory(iface):
"""
try:
return start(iface)
except:
import pip
pip.main([
'install',
'-r', 'requirements.txt',
'--target', 'lib'
])
return start(iface)
except (ModuleNotFoundError, ImportError) as error:
ok_install_requirements = warning(
"ImportError!",
("Your environment does not have the minimal " +
"requirements to run WTSS Plugin, " +
"click OK to install them.\n\n" + str(error))
)
if ok_install_requirements:
import subprocess
try:
subprocess.run([
'pip', 'uninstall',
'-r', requirements_file(),
])
except:
pass
subprocess.run([
'pip', 'install',
'--target', lib_path(),
'-r', requirements_file(),
])
ok_restart = warning(
"Restart Required!",
"Restart your QGIS environment to load updates!"
)
if ok_restart:
import sys
python = sys.executable
os.execl(python, python, *sys.argv)
else:
return None
return start(iface)
else:
return None
4 changes: 2 additions & 2 deletions wtss_plugin/pb_tool.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ compiled_ui_files: resources.py
resource_files: resources.qrc

# Other files required for the plugin
extras: metadata.txt
extras: metadata.txt requirements.txt

# Other directories to be deployed with the plugin.
# These must be subdirectories under the plugin directory
extra_dirs: controller help lib
extra_dirs: controller help

# ISO code(s) for any locales (translations), separated by spaces.
# Corresponding .ts files must exist in the i18n directory
Expand Down

0 comments on commit cb2e1ec

Please sign in to comment.