multi-user/multi_user/environment.py

71 lines
2.0 KiB
Python
Raw Normal View History

2020-03-20 14:56:50 +01:00
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
2019-09-30 16:43:12 +02:00
import collections
import logging
import os
2019-07-01 18:04:35 +02:00
import subprocess
import sys
from pathlib import Path
2019-07-01 18:04:35 +02:00
THIRD_PARTY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "libs")
DEFAULT_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
rtypes = []
2019-07-01 18:04:35 +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)
def install_package(name):
2020-04-22 17:04:14 +02:00
logging.debug(f"Using {PYTHON_PATH} for installation")
subprocess.run([str(PYTHON_PATH), "-m", "pip", "install", name])
2019-07-01 18:04:35 +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
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)
2020-04-22 17:04:14 +02:00
module_can_be_imported(package_name)