22
22
* *
23
23
***************************************************************************/
24
24
"""
25
+ import importlib
25
26
import pathlib
26
27
import subprocess
27
28
import sys
28
29
29
30
from packaging .version import parse as parse_version
31
+ from qgis .gui import QgisInterface
32
+ from qgis .utils import iface
30
33
31
- # from pip import main as pipmain
34
+ iface : QgisInterface
35
+
36
+
37
+ def install_vendor_dir ():
38
+ vendor_dir = pathlib .Path (__file__ ).parent .parent / "vendor"
39
+ if vendor_dir .exists ():
40
+ sys .path .insert (0 , str (vendor_dir .resolve ()))
41
+
42
+
43
+ def unload_module (mod : str ):
44
+ mod_prefix = f"{ mod } ."
45
+ for m in list (sys .modules .keys ()):
46
+ if m == mod or m .startswith (mod_prefix ):
47
+ del sys .modules [m ]
48
+
49
+
50
+ def reload_modules ():
51
+ vendor_dir = pathlib .Path (__file__ ).parent .parent / "vendor"
52
+ if vendor_dir .exists ():
53
+ for d in vendor_dir .iterdir ():
54
+ if d .suffix == '.py' or (d .is_dir () and (d / '__init__.py' ).exists ()):
55
+ mod = d .stem
56
+ if mod in sys .modules :
57
+ unload_module (mod )
58
+
59
+ importlib .import_module (mod )
32
60
33
61
34
62
def python_executable ():
@@ -42,26 +70,40 @@ def install_addon(pkg: str, *options):
42
70
installdir = pathlib .Path (__file__ ).parent .parent / "vendor"
43
71
if not installdir .exists ():
44
72
installdir .mkdir ()
73
+ install_vendor_dir ()
74
+
45
75
if (installdir / pkg ).exists ():
46
76
options = (* options , "--upgrade" )
47
- subprocess .check_call ([python_executable (), '-m' , 'pip' , 'install' , '-t' , str (installdir ), * options , pkg ])
48
- # pipmain(['install', '-t', str(installdir), *options, pkg])
77
+ try :
78
+ subprocess .check_call ([python_executable (), '-m' , 'pip' , 'install' , '-t' , str (installdir ), * options , pkg ])
79
+ except subprocess .CalledProcessError :
80
+ iface .messageBar ().pushWarning ("Warning" , f"Could not install addon { pkg } " )
81
+ return False
82
+
83
+ return True
49
84
50
85
51
86
def install_pyogrio ():
87
+ # pylint: disable=import-outside-toplevel
52
88
import geopandas
53
89
if parse_version (geopandas .__version__ ) < parse_version ('0.12.0' ):
54
- install_addon ('geopandas' , "--no-deps" )
90
+ install_addon ('geopandas' )
55
91
import shapely
56
92
if parse_version (shapely .__version__ ) < parse_version ('2.0.0' ):
57
- install_addon ('shapeley' , "--no-deps" )
93
+ install_addon ('shapeley' )
58
94
59
- install_addon ('pyogrio' , '--no-deps' )
95
+ install_addon ('pyogrio' )
96
+ reload_modules ()
60
97
61
98
62
99
def install_pyarrow ():
63
- install_addon ('pyarrow' , '--no-deps' )
100
+ install_addon ('pyarrow' )
101
+ reload_modules ()
64
102
65
103
66
104
def install_gerrychain ():
67
- install_addon ('gerrychain' , '--no-deps' )
105
+ install_addon ('gerrychain==0.3.1' )
106
+ reload_modules ()
107
+
108
+
109
+ install_vendor_dir ()
0 commit comments