-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmoduleutil.py
110 lines (94 loc) · 3.28 KB
/
moduleutil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# ##### 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 <http://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
import glob
import importlib
import logging
import os
import subprocess
import sys
def is_available(module):
"""
Checks if given module is available for use
Parameters:
module (str): Module name
Returns:
True if available; False otherwise
"""
return (module in locals() and
type(locals()[module]) is importlib.types.ModuleType)
def is_installed(module):
"""
Checks if given module is installed
Parameters:
module (str): Module name
Returns:
True if installed; False otherwise
"""
try:
importlib.import_module(module)
return True
except ImportError:
return False
def install(module: str, options: str = None):
"""
Installs given module with pip
Parameters:
module (str): Module name
options (str): Command line options for pip (e.g. '--no-deps -r')
Returns:
True if installation succeeded; False otherwise
"""
# Determine the path to Blender's Python interpreter.
executable = None
try:
for path in glob.glob('{}/bin/python*'.format(sys.exec_prefix)):
if os.access(path, os.X_OK) and not path.lower().endswith('dll'):
executable = path
logging.debug(
'Blender\'s Python interpreter: {}'.format(executable))
break
except Exception as e:
logging.error(e)
if executable is None:
logging.error('Unable to locate Blender\'s Python interpreter')
# Install Python package manager.
if is_installed('ensurepip'):
subprocess.call([executable, '-m', 'ensurepip'])
elif not is_installed('pip'):
url = 'https://bootstrap.pypa.io/get-pip.py'
filepath = '{}/get-pip.py'.format(os.getcwd())
try:
requests = importlib.import_module('requests')
response = requests.get(url)
with open(filepath, 'w') as f:
f.write(response.text)
subprocess.call([executable, filepath])
except Exception as e:
logging.error(e)
finally:
if os.path.isfile(filepath):
os.remove(filepath)
# Install given module.
if not is_installed(module) and executable is not None:
try:
if options is None or options.strip() == '':
subprocess.call([executable, '-m', 'pip', 'install', module])
else:
subprocess.call([executable, '-m', 'pip', 'install', options, module])
except Exception as e:
logging.error(e)
return is_installed(module)