Skip to content

Commit edb75ff

Browse files
authored
Merge pull request #45 from opengisch/copilot/refactor-os-path-to-pathlib
Refactor plugin.py to use pathlib and add macOS Python search path
2 parents 4664dc5 + dd1e74b commit edb75ff

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

a00_qpip/plugin.py

Lines changed: 39 additions & 36 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.site_packages_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,15 @@ 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"
184-
)
185-
if os.path.isfile(requirements_path):
183+
requirements_path = self.plugins_path / plugin_name / "requirements.txt"
184+
if requirements_path.is_file():
186185
log(f"Loading requirements for {plugin_name}")
187186
with open(requirements_path, "r") as f:
188187
requirements = pkg_resources.parse_requirements(f)
@@ -267,7 +266,7 @@ def pip_install_reqs(self, reqs_to_install):
267266
"""
268267
Installs given reqs with pip
269268
"""
270-
os.makedirs(self.prefix_path, exist_ok=True)
269+
self.prefix_path.mkdir(parents=True, exist_ok=True)
271270
log(f"Will pip install {reqs_to_install}")
272271

273272
run_cmd(
@@ -278,38 +277,42 @@ def pip_install_reqs(self, reqs_to_install):
278277
"install",
279278
*reqs_to_install,
280279
"--target",
281-
self.prefix_path,
280+
str(self.prefix_path),
282281
],
283282
f"installing {len(reqs_to_install)} requirements",
284283
)
285284

286285
def python_command(self):
287-
if os.path.exists(os.path.join(sys.prefix, "conda-meta")): # Conda
286+
if (Path(sys.prefix) / "conda-meta").exists(): # Conda
288287
log("Attempt Conda install at 'python' shortcut")
289288
return "python"
290289

291290
# 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
292291
# 'python' doesnt seem to work, using this method instead
293292
if platform.system() == "Windows": # Windows
294-
base_path = sys.prefix
293+
base_path = Path(sys.prefix)
295294
for file in ["python.exe", "python3.exe"]:
296-
path = os.path.join(base_path, file)
297-
if os.path.isfile(path):
295+
path = base_path / file
296+
if path.is_file():
298297
log(f"Attempt Windows install at {str(path)}")
299-
return path
298+
return str(path)
300299
path = sys.executable
301300
log(f"Attempt Windows install at {str(path)}")
302301
return path
303302

304303
# Same bug on mac as windows: https://github.com/opengisch/qpip/issues/34#issuecomment-2995221985
305304
if platform.system() == "Darwin": # Mac
306-
base_paths = [sys.prefix, os.path.join(sys.prefix, "bin")]
305+
base_paths = [
306+
Path(sys.prefix),
307+
Path(sys.prefix) / "bin",
308+
Path(sys.executable).parent,
309+
]
307310
for base_path in base_paths:
308311
for file in ["python", "python3"]:
309-
path = os.path.join(base_path, file)
310-
if os.path.isfile(path):
312+
path = base_path / file
313+
if path.is_file():
311314
log(f"Attempt MacOS install at {str(path)}")
312-
return path
315+
return str(path)
313316
path = sys.executable
314317
log(f"Attempt MacOS install at {str(path)}")
315318
return path
@@ -326,11 +329,11 @@ def check(self):
326329

327330
def show_folder(self):
328331
if platform.system() == "Windows":
329-
os.startfile(self.prefix_path)
332+
os.startfile(str(self.prefix_path))
330333
elif platform.system() == "Darwin":
331-
subprocess.Popen(["open", self.prefix_path])
334+
subprocess.Popen(["open", str(self.prefix_path)])
332335
else:
333-
subprocess.Popen(["xdg-open", self.prefix_path])
336+
subprocess.Popen(["xdg-open", str(self.prefix_path)])
334337

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

0 commit comments

Comments
 (0)