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
|
2020-07-13 21:12:15 +08:00
|
|
|
import re
|
2021-06-10 00:16:43 +08:00
|
|
|
import bpy
|
2020-07-13 21:12:15 +08:00
|
|
|
|
2020-11-21 05:06:08 +08:00
|
|
|
VERSION_EXPR = re.compile('\d+.\d+.\d+')
|
2020-06-18 04:11:20 +08:00
|
|
|
DEFAULT_CACHE_DIR = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)), "cache")
|
2021-06-10 00:16:43 +08:00
|
|
|
REPLICATION_DEPENDENCIES = {
|
|
|
|
"zmq",
|
2023-04-06 15:26:29 +08:00
|
|
|
"deepdiff"
|
2021-06-10 00:16:43 +08:00
|
|
|
}
|
|
|
|
LIBS = os.path.join(os.path.dirname(os.path.abspath(__file__)), "libs")
|
|
|
|
REPLICATION = os.path.join(LIBS,"replication")
|
|
|
|
|
2019-07-02 00:04:35 +08:00
|
|
|
|
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
|
|
|
|
2021-06-10 00:16:43 +08:00
|
|
|
def module_can_be_imported(name: str) -> bool:
|
2019-07-01 21:39:01 +08:00
|
|
|
try:
|
|
|
|
__import__(name)
|
|
|
|
return True
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-04-06 15:26:29 +08:00
|
|
|
def install_pip(python_path):
|
2019-07-01 21:39:01 +08:00
|
|
|
# pip can not necessarily be imported into Blender after this
|
2023-04-06 15:26:29 +08:00
|
|
|
subprocess.run([str(python_path), "-m", "ensurepip"])
|
2019-07-01 21:39:01 +08:00
|
|
|
|
|
|
|
|
2023-04-06 15:26:29 +08:00
|
|
|
def install_requirements(python_path:str, module_requirement: str, install_dir: str):
|
|
|
|
logging.info(f"Installing {module_requirement} dependencies in {install_dir}")
|
2020-08-28 23:23:25 +08:00
|
|
|
env = os.environ
|
|
|
|
if "PIP_REQUIRE_VIRTUALENV" in env:
|
2020-08-29 00:12:01 +08:00
|
|
|
# PIP_REQUIRE_VIRTUALENV is an env var to ensure pip cannot install packages outside a virtual env
|
|
|
|
# https://docs.python-guide.org/dev/pip-virtualenv/
|
|
|
|
# But since Blender's pip is outside of a virtual env, it can block our packages installation, so we unset the
|
|
|
|
# env var for the subprocess.
|
2020-08-28 23:23:25 +08:00
|
|
|
env = os.environ.copy()
|
|
|
|
del env["PIP_REQUIRE_VIRTUALENV"]
|
2023-04-06 15:26:29 +08:00
|
|
|
subprocess.run([str(python_path), "-m", "pip", "install", "-r", f"{install_dir}/{module_requirement}/requirements.txt", "-t", install_dir], env=env)
|
2020-07-11 00:00:44 +08:00
|
|
|
|
2021-06-10 00:16:43 +08:00
|
|
|
|
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
|
|
|
|
2021-06-10 00:16:43 +08:00
|
|
|
def setup_paths(paths: list):
|
|
|
|
""" Add missing path to sys.path
|
|
|
|
"""
|
|
|
|
for path in paths:
|
|
|
|
if path not in sys.path:
|
|
|
|
logging.debug(f"Adding {path} dir to the path.")
|
|
|
|
sys.path.insert(0, path)
|
|
|
|
|
|
|
|
|
|
|
|
def remove_paths(paths: list):
|
|
|
|
""" Remove list of path from sys.path
|
|
|
|
"""
|
|
|
|
for path in paths:
|
|
|
|
if path in sys.path:
|
|
|
|
logging.debug(f"Removing {path} dir from the path.")
|
|
|
|
sys.path.remove(path)
|
2023-04-06 15:26:29 +08:00
|
|
|
|
2021-06-10 00:16:43 +08:00
|
|
|
|
|
|
|
def register():
|
2021-11-05 22:39:46 +08:00
|
|
|
if bpy.app.version >= (2,91,0):
|
2021-06-10 00:16:43 +08:00
|
|
|
python_binary_path = sys.executable
|
|
|
|
else:
|
|
|
|
python_binary_path = bpy.app.binary_path_python
|
|
|
|
|
2023-04-06 15:26:29 +08:00
|
|
|
python_path = Path(python_binary_path)
|
|
|
|
|
2021-06-10 00:16:43 +08:00
|
|
|
for module_name in list(sys.modules.keys()):
|
|
|
|
if 'replication' in module_name:
|
|
|
|
del sys.modules[module_name]
|
|
|
|
|
|
|
|
setup_paths([LIBS, REPLICATION])
|
2023-04-06 15:26:29 +08:00
|
|
|
|
|
|
|
if not module_can_be_imported("pip"):
|
|
|
|
install_pip(python_path)
|
|
|
|
|
|
|
|
deps_not_installed = [package_name for package_name in REPLICATION_DEPENDENCIES if not module_can_be_imported(package_name)]
|
|
|
|
if any(deps_not_installed):
|
|
|
|
install_requirements(python_path, module_requirement='replication', install_dir=LIBS)
|
|
|
|
|
2021-06-10 00:16:43 +08:00
|
|
|
|
|
|
|
def unregister():
|
|
|
|
remove_paths([REPLICATION, LIBS])
|