2019-09-30 16:43:12 +02:00
|
|
|
import collections
|
|
|
|
import logging
|
2019-07-01 15:39:01 +02:00
|
|
|
import os
|
2019-07-01 18:04:35 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2019-07-01 15:39:01 +02:00
|
|
|
from pathlib import Path
|
2019-07-01 18:04:35 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2020-02-07 00:48:34 +01:00
|
|
|
logger.setLevel(logging.WARNING)
|
2019-07-01 18:04:35 +02:00
|
|
|
|
|
|
|
CONFIG_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config")
|
|
|
|
CONFIG = os.path.join(CONFIG_DIR, "app.yaml")
|
|
|
|
|
|
|
|
THIRD_PARTY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "libs")
|
2019-07-02 18:05:18 +02:00
|
|
|
CACHE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "cache")
|
2019-07-01 18:04:35 +02:00
|
|
|
PYTHON_PATH = None
|
|
|
|
SUBPROCESS_DIR = None
|
|
|
|
|
2019-07-03 15:32:00 +02:00
|
|
|
|
|
|
|
rtypes = []
|
2019-07-01 18:04:35 +02:00
|
|
|
|
2019-08-23 12:28:57 +02:00
|
|
|
|
2019-07-01 18:04:35 +02:00
|
|
|
def load_config():
|
2019-07-08 14:57:30 +02:00
|
|
|
import yaml
|
|
|
|
|
2019-07-01 18:14:48 +02:00
|
|
|
try:
|
|
|
|
with open(CONFIG, 'r') as config_file:
|
|
|
|
return yaml.safe_load(config_file)
|
|
|
|
except FileNotFoundError:
|
|
|
|
logger.info("no config")
|
2019-09-30 16:43:12 +02:00
|
|
|
return {}
|
|
|
|
|
2019-07-02 17:44:59 +02:00
|
|
|
|
2019-07-01 18:04:35 +02:00
|
|
|
def save_config(config):
|
2019-07-08 14:57:30 +02:00
|
|
|
import yaml
|
|
|
|
|
2019-07-01 18:04:35 +02:00
|
|
|
logger.info("saving config")
|
2019-09-30 22:04:54 +02:00
|
|
|
|
2019-07-01 18:04:35 +02:00
|
|
|
with open(CONFIG, 'w') as outfile:
|
|
|
|
yaml.dump(config, outfile, default_flow_style=False)
|
2019-07-01 15:39:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def module_can_be_imported(name):
|
|
|
|
try:
|
|
|
|
__import__(name)
|
|
|
|
return True
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def install_pip():
|
|
|
|
# pip can not necessarily be imported into Blender after this
|
|
|
|
get_pip_path = Path(__file__).parent / "libs" / "get-pip.py"
|
2019-07-01 15:59:51 +02:00
|
|
|
subprocess.run([str(PYTHON_PATH), str(get_pip_path)], cwd=SUBPROCESS_DIR)
|
2019-07-01 15:39:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def install_package(name):
|
2019-07-01 15:59:51 +02:00
|
|
|
subprocess.run([str(PYTHON_PATH), "-m", "pip", "install",
|
2020-02-19 13:12:09 +01:00
|
|
|
name], cwd=SUBPROCESS_DIR)
|
2019-07-01 18:04:35 +02:00
|
|
|
|
2019-09-30 22:04:54 +02:00
|
|
|
def check_dir(dir):
|
|
|
|
if not os.path.exists(dir):
|
|
|
|
os.makedirs(dir)
|
2019-07-01 18:04:35 +02:00
|
|
|
|
|
|
|
def setup(dependencies, python_path):
|
|
|
|
global PYTHON_PATH, SUBPROCESS_DIR
|
|
|
|
|
|
|
|
PYTHON_PATH = Path(python_path)
|
|
|
|
SUBPROCESS_DIR = PYTHON_PATH.parent
|
2019-07-01 15:39:01 +02:00
|
|
|
|
2019-09-30 22:04:54 +02:00
|
|
|
check_dir(CACHE_DIR)
|
|
|
|
check_dir(CONFIG_DIR)
|
|
|
|
|
2019-07-01 15:39:01 +02:00
|
|
|
if not module_can_be_imported("pip"):
|
|
|
|
install_pip()
|
|
|
|
|
2019-09-30 16:43:12 +02:00
|
|
|
for module_name, package_name in dependencies:
|
2019-08-26 23:14:30 +02:00
|
|
|
if not module_can_be_imported(module_name):
|
|
|
|
install_package(package_name)
|