Skip to content

Commit 58fef0b

Browse files
Copilotm-kuhn
andcommitted
Refactor os.path to pathlib in plugin.py and add macOS python search path
Co-authored-by: m-kuhn <588407+m-kuhn@users.noreply.github.com>
1 parent 2ec8513 commit 58fef0b

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

a00_qpip/plugin.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
from collections import defaultdict, namedtuple
66
from importlib import metadata
7+
from pathlib import Path
78
from typing import Union
89

910
import pkg_resources
@@ -29,29 +30,29 @@ def __init__(self, iface, plugin_path=None):
2930
self.settings.beginGroup("QPIP")
3031

3132
if plugin_path is None:
32-
self.plugins_path = os.path.join(
33-
QgsApplication.qgisSettingsDirPath(), "python", "plugins"
33+
self.plugins_path = (
34+
Path(QgsApplication.qgisSettingsDirPath()) / "python" / "plugins"
3435
)
3536
else:
36-
self.plugins_path = plugin_path
37-
self.prefix_path = os.path.join(
38-
QgsApplication.qgisSettingsDirPath().replace("/", os.path.sep),
39-
"python",
40-
"dependencies",
37+
self.plugins_path = Path(plugin_path)
38+
self.prefix_path = (
39+
Path(QgsApplication.qgisSettingsDirPath()) / "python" / "dependencies"
4140
)
42-
self.site_packages_path = os.path.join(self.prefix_path)
43-
self.bin_path = os.path.join(self.prefix_path, "bin")
41+
self.site_packages_path = self.prefix_path
42+
self.bin_path = self.prefix_path / "bin"
4443

4544
if self.site_packages_path not in sys.path:
4645
log(f"Adding {self.site_packages_path} to PYTHONPATH")
47-
sys.path.insert(0, self.site_packages_path)
46+
sys.path.insert(0, str(self.site_packages_path))
4847
os.environ["PYTHONPATH"] = (
49-
self.site_packages_path + os.pathsep + os.environ.get("PYTHONPATH", "")
48+
str(self.site_packages_path)
49+
+ os.pathsep
50+
+ os.environ.get("PYTHONPATH", "")
5051
)
5152

52-
if self.bin_path not in os.environ["PATH"]:
53+
if str(self.bin_path) not in os.environ["PATH"]:
5354
log(f"Adding {self.bin_path} to PATH")
54-
os.environ["PATH"] = self.bin_path + os.pathsep + os.environ["PATH"]
55+
os.environ["PATH"] = str(self.bin_path) + os.pathsep + os.environ["PATH"]
5556

5657
sys.path_importer_cache.clear()
5758

@@ -94,13 +95,13 @@ def unload(self):
9495
installer.loadPlugin = self._original_loadPlugin
9596

9697
# Remove path alterations
97-
if self.site_packages_path in sys.path:
98-
sys.path.remove(self.site_packages_path)
98+
if str(self.site_packages_path) in sys.path:
99+
sys.path.remove(str(self.site_packages_path))
99100
os.environ["PYTHONPATH"] = os.environ["PYTHONPATH"].replace(
100-
self.bin_path + os.pathsep, ""
101+
str(self.bin_path) + os.pathsep, ""
101102
)
102103
os.environ["PATH"] = os.environ["PATH"].replace(
103-
self.bin_path + os.pathsep, ""
104+
str(self.bin_path) + os.pathsep, ""
104105
)
105106

106107
def patched_load_plugin(self, packageName):
@@ -172,17 +173,17 @@ def check_deps(self, additional_plugins=[]) -> Union[MainDialog, bool]:
172173
name = dist.metadata["Name"]
173174
libs[name].name = name
174175
libs[name].installed_dist = dist
175-
if os.path.dirname(str(dist._path)) != self.site_packages_path:
176+
if Path(dist._path).parent != self.site_packages_path:
176177
libs[name].qpip = False
177178

178179
# Checking requirements of all plugins
179180
needs_gui = False
180181
for plugin_name in plugin_names:
181182
# If requirements.txt is present, we see if we can load it
182-
requirements_path = os.path.join(
183-
self.plugins_path, plugin_name, "requirements.txt"
183+
requirements_path = (
184+
Path(self.plugins_path) / plugin_name / "requirements.txt"
184185
)
185-
if os.path.isfile(requirements_path):
186+
if requirements_path.is_file():
186187
log(f"Loading requirements for {plugin_name}")
187188
with open(requirements_path, "r") as f:
188189
requirements = pkg_resources.parse_requirements(f)
@@ -267,7 +268,7 @@ def pip_install_reqs(self, reqs_to_install):
267268
"""
268269
Installs given reqs with pip
269270
"""
270-
os.makedirs(self.prefix_path, exist_ok=True)
271+
self.prefix_path.mkdir(parents=True, exist_ok=True)
271272
log(f"Will pip install {reqs_to_install}")
272273

273274
run_cmd(
@@ -278,38 +279,42 @@ def pip_install_reqs(self, reqs_to_install):
278279
"install",
279280
*reqs_to_install,
280281
"--target",
281-
self.prefix_path,
282+
str(self.prefix_path),
282283
],
283284
f"installing {len(reqs_to_install)} requirements",
284285
)
285286

286287
def python_command(self):
287-
if os.path.exists(os.path.join(sys.prefix, "conda-meta")): # Conda
288+
if (Path(sys.prefix) / "conda-meta").exists(): # Conda
288289
log("Attempt Conda install at 'python' shortcut")
289290
return "python"
290291

291292
# python is normally found at sys.executable, but there is an issue on windows qgis so use 'python' instead: https://github.com/qgis/QGIS/issues/45646
292293
# 'python' doesnt seem to work, using this method instead
293294
if platform.system() == "Windows": # Windows
294-
base_path = sys.prefix
295+
base_path = Path(sys.prefix)
295296
for file in ["python.exe", "python3.exe"]:
296-
path = os.path.join(base_path, file)
297-
if os.path.isfile(path):
297+
path = base_path / file
298+
if path.is_file():
298299
log(f"Attempt Windows install at {str(path)}")
299-
return path
300+
return str(path)
300301
path = sys.executable
301302
log(f"Attempt Windows install at {str(path)}")
302303
return path
303304

304305
# Same bug on mac as windows: https://github.com/opengisch/qpip/issues/34#issuecomment-2995221985
305306
if platform.system() == "Darwin": # Mac
306-
base_paths = [sys.prefix, os.path.join(sys.prefix, "bin")]
307+
base_paths = [
308+
Path(sys.prefix),
309+
Path(sys.prefix) / "bin",
310+
Path(sys.executable).parent,
311+
]
307312
for base_path in base_paths:
308313
for file in ["python", "python3"]:
309-
path = os.path.join(base_path, file)
310-
if os.path.isfile(path):
314+
path = base_path / file
315+
if path.is_file():
311316
log(f"Attempt MacOS install at {str(path)}")
312-
return path
317+
return str(path)
313318
path = sys.executable
314319
log(f"Attempt MacOS install at {str(path)}")
315320
return path
@@ -326,11 +331,11 @@ def check(self):
326331

327332
def show_folder(self):
328333
if platform.system() == "Windows":
329-
os.startfile(self.prefix_path)
334+
os.startfile(str(self.prefix_path))
330335
elif platform.system() == "Darwin":
331-
subprocess.Popen(["open", self.prefix_path])
336+
subprocess.Popen(["open", str(self.prefix_path)])
332337
else:
333-
subprocess.Popen(["xdg-open", self.prefix_path])
338+
subprocess.Popen(["xdg-open", str(self.prefix_path)])
334339

335340
def _check_on_startup(self):
336341
return self.settings.value("check_on_startup", "no") == "yes"

0 commit comments

Comments
 (0)