2020-03-20 21:56:50 +08: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 22:43:12 +08:00
|
|
|
import collections
|
|
|
|
import logging
|
2019-07-01 21:39:01 +08:00
|
|
|
import os
|
2019-07-02 00:04:35 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2019-07-01 21:39:01 +08:00
|
|
|
from pathlib import Path
|
2020-06-18 04:11:20 +08:00
|
|
|
import socket
|
2019-07-02 00:04:35 +08:00
|
|
|
|
|
|
|
THIRD_PARTY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "libs")
|
2020-06-18 04:11:20 +08:00
|
|
|
DEFAULT_CACHE_DIR = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)), "cache")
|
2019-07-02 00:04:35 +08:00
|
|
|
PYTHON_PATH = None
|
|
|
|
SUBPROCESS_DIR = None
|
|
|
|
|
2019-07-03 21:32:00 +08:00
|
|
|
|
|
|
|
rtypes = []
|
2019-07-02 00:04:35 +08:00
|
|
|
|
2020-06-18 04:11:20 +08:00
|
|
|
|
2019-07-01 21:39:01 +08: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
|
2020-05-27 21:57:57 +08:00
|
|
|
subprocess.run([str(PYTHON_PATH), "-m", "ensurepip"])
|
2019-07-01 21:39:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def install_package(name):
|
2020-04-22 23:04:14 +08:00
|
|
|
logging.debug(f"Using {PYTHON_PATH} for installation")
|
|
|
|
subprocess.run([str(PYTHON_PATH), "-m", "pip", "install", name])
|
|
|
|
|
2019-07-02 00:04:35 +08:00
|
|
|
|
2020-07-11 00:00:44 +08:00
|
|
|
def upgrade_package(name):
|
|
|
|
logging.debug(f"Using {PYTHON_PATH} for update")
|
|
|
|
subprocess.run([str(PYTHON_PATH), "-m", "pip", "install", name, "--upgrade"])
|
|
|
|
|
2020-06-18 04:11:20 +08:00
|
|
|
def get_ip():
|
|
|
|
"""
|
|
|
|
Retrieve the main network interface IP.
|
|
|
|
|
|
|
|
"""
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
s.connect(("8.8.8.8", 80))
|
|
|
|
ip = s.getsockname()[0]
|
|
|
|
s.close()
|
|
|
|
return ip
|
|
|
|
|
|
|
|
|
2019-10-01 04:04:54 +08:00
|
|
|
def check_dir(dir):
|
|
|
|
if not os.path.exists(dir):
|
|
|
|
os.makedirs(dir)
|
2019-07-02 00:04:35 +08:00
|
|
|
|
2020-06-18 04:11:20 +08:00
|
|
|
|
2019-07-02 00:04:35 +08:00
|
|
|
def setup(dependencies, python_path):
|
|
|
|
global PYTHON_PATH, SUBPROCESS_DIR
|
|
|
|
|
|
|
|
PYTHON_PATH = Path(python_path)
|
|
|
|
SUBPROCESS_DIR = PYTHON_PATH.parent
|
2019-07-01 21:39:01 +08:00
|
|
|
|
|
|
|
if not module_can_be_imported("pip"):
|
|
|
|
install_pip()
|
|
|
|
|
2020-07-10 22:50:09 +08:00
|
|
|
for package_name in dependencies:
|
|
|
|
if not module_can_be_imported(package_name):
|
2019-08-27 05:14:30 +08:00
|
|
|
install_package(package_name)
|
2020-04-22 23:04:14 +08:00
|
|
|
module_can_be_imported(package_name)
|
2020-07-11 00:00:44 +08:00
|
|
|
else:
|
|
|
|
upgrade_package(package_name)
|