Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0330c6cf83 | |||
| e98a80d45a | |||
| 2d7e0e9e92 |
+35
-40
@@ -3,9 +3,11 @@ Provides uniform and consistent path helpers for `specklepy`
|
|||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from importlib import import_module, invalidate_caches
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from importlib import import_module, invalidate_caches
|
|
||||||
|
import pkg_resources
|
||||||
|
|
||||||
_user_data_env_var = "SPECKLE_USERDATA_PATH"
|
_user_data_env_var = "SPECKLE_USERDATA_PATH"
|
||||||
|
|
||||||
@@ -55,9 +57,7 @@ def user_application_data_path() -> Path:
|
|||||||
if sys.platform.startswith("win"):
|
if sys.platform.startswith("win"):
|
||||||
app_data_path = os.getenv("APPDATA")
|
app_data_path = os.getenv("APPDATA")
|
||||||
if not app_data_path:
|
if not app_data_path:
|
||||||
raise Exception(
|
raise Exception("Cannot get appdata path from environment.")
|
||||||
"Cannot get appdata path from environment."
|
|
||||||
)
|
|
||||||
return Path(app_data_path)
|
return Path(app_data_path)
|
||||||
else:
|
else:
|
||||||
# try getting the standard XDG_DATA_HOME value
|
# try getting the standard XDG_DATA_HOME value
|
||||||
@@ -68,9 +68,7 @@ def user_application_data_path() -> Path:
|
|||||||
else:
|
else:
|
||||||
return _ensure_folder_exists(Path.home(), ".config")
|
return _ensure_folder_exists(Path.home(), ".config")
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
raise Exception(
|
raise Exception("Failed to initialize user application data path.", ex)
|
||||||
"Failed to initialize user application data path.", ex
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def user_speckle_folder_path() -> Path:
|
def user_speckle_folder_path() -> Path:
|
||||||
@@ -90,19 +88,16 @@ def user_speckle_connector_installation_path(host_application: str) -> Path:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
print("Starting module dependency installation")
|
print("Starting module dependency installation")
|
||||||
print(sys.executable)
|
print(sys.executable)
|
||||||
|
|
||||||
PYTHON_PATH = sys.executable
|
PYTHON_PATH = sys.executable
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def connector_installation_path(host_application: str) -> Path:
|
def connector_installation_path(host_application: str) -> Path:
|
||||||
connector_installation_path = user_speckle_connector_installation_path(host_application)
|
connector_installation_path = user_speckle_connector_installation_path(
|
||||||
|
host_application
|
||||||
|
)
|
||||||
connector_installation_path.mkdir(exist_ok=True, parents=True)
|
connector_installation_path.mkdir(exist_ok=True, parents=True)
|
||||||
|
|
||||||
# set user modules path at beginning of paths for earlier hit
|
# set user modules path at beginning of paths for earlier hit
|
||||||
@@ -113,7 +108,6 @@ def connector_installation_path(host_application: str) -> Path:
|
|||||||
return connector_installation_path
|
return connector_installation_path
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def is_pip_available() -> bool:
|
def is_pip_available() -> bool:
|
||||||
try:
|
try:
|
||||||
import_module("pip") # noqa F401
|
import_module("pip") # noqa F401
|
||||||
@@ -132,13 +126,15 @@ def ensure_pip() -> None:
|
|||||||
if completed_process.returncode == 0:
|
if completed_process.returncode == 0:
|
||||||
print("Successfully installed pip")
|
print("Successfully installed pip")
|
||||||
else:
|
else:
|
||||||
raise Exception(f"Failed to install pip, got {completed_process.returncode} return code")
|
raise Exception(
|
||||||
|
f"Failed to install pip, got {completed_process.returncode} return code"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_requirements_path() -> Path:
|
def get_requirements_path() -> Path:
|
||||||
# we assume that a requirements.txt exists next to the __init__.py file
|
# we assume that a requirements.txt exists next to the __init__.py file
|
||||||
path = Path(Path(__file__).parent, "requirements.txt")
|
path = Path(__file__).parent.with_name("requirements.txt")
|
||||||
assert path.exists()
|
assert path.exists(), f"Can't find requirements file at {path}"
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
@@ -167,7 +163,10 @@ def install_requirements(host_application: str) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if completed_process.returncode != 0:
|
if completed_process.returncode != 0:
|
||||||
m = f"Failed to install dependenices through pip, got {completed_process.returncode} return code"
|
m = (
|
||||||
|
"Failed to install dependenices through pip, ",
|
||||||
|
f"got {completed_process.returncode} return code",
|
||||||
|
)
|
||||||
print(m)
|
print(m)
|
||||||
raise Exception(m)
|
raise Exception(m)
|
||||||
|
|
||||||
@@ -179,29 +178,25 @@ def install_dependencies(host_application: str) -> None:
|
|||||||
install_requirements(host_application)
|
install_requirements(host_application)
|
||||||
|
|
||||||
|
|
||||||
def _import_dependencies() -> None:
|
def _dependencies_installed() -> bool:
|
||||||
import_module("specklepy")
|
try:
|
||||||
# the code above doesn't work for now, it fails on importing graphql-core
|
pkg_resources.require(get_requirements_path().read_text())
|
||||||
# despite that, the connector seams to be working as expected
|
return True
|
||||||
# But it would be nice to make this solution work
|
except (pkg_resources.DistributionNotFound, pkg_resources.VersionConflict):
|
||||||
# it would ensure that all dependencies are fully loaded
|
return False
|
||||||
# requirements = get_requirements_path().read_text()
|
|
||||||
# reqs = [
|
|
||||||
# req.split(" ; ")[0].split("==")[0].split("[")[0].replace("-", "_")
|
|
||||||
# for req in requirements.split("\n")
|
|
||||||
# if req and not req.startswith(" ")
|
|
||||||
# ]
|
|
||||||
# for req in reqs:
|
|
||||||
# print(req)
|
|
||||||
# import_module("specklepy")
|
|
||||||
|
|
||||||
def ensure_dependencies(host_application: str) -> None:
|
def ensure_dependencies(host_application: str) -> None:
|
||||||
try:
|
if _dependencies_installed():
|
||||||
install_dependencies(host_application)
|
return
|
||||||
invalidate_caches()
|
|
||||||
_import_dependencies()
|
install_dependencies(host_application)
|
||||||
|
invalidate_caches()
|
||||||
|
if _dependencies_installed():
|
||||||
print("Successfully found dependencies")
|
print("Successfully found dependencies")
|
||||||
except ImportError:
|
return
|
||||||
raise Exception(f"Cannot automatically ensure Speckle dependencies. Please try restarting the host application {host_application}!")
|
|
||||||
|
|
||||||
|
|
||||||
|
raise Exception(
|
||||||
|
"Cannot automatically ensure Speckle dependencies. ",
|
||||||
|
f"Please try restarting the host application {host_application}!",
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user