-
Notifications
You must be signed in to change notification settings - Fork 5
/
blender_pip.py
148 lines (127 loc) · 4.61 KB
/
blender_pip.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding:utf-8 -*-
# ##### 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 2
# 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# ----------------------------------------------------------
# Author: Stephen Leger (s-leger)
#
# ----------------------------------------------------------
import bpy
import subprocess
if bpy.app.version > (2,92,0):
import sys
PYPATH = sys.executable
else:
PYPATH = bpy.app.binary_path_python
class Pip:
def __init__(self):
self._ensure_user_site_package()
self._ensurepip()
@staticmethod
def _ensure_user_site_package():
import os
import site
import sys
site_package = site.getusersitepackages()
'''
if not os.path.exists(site_package):
site_package = bpy.utils.user_resource('SCRIPTS', "site_package", create=True)
site.addsitedir(site_package)
'''
if site_package not in sys.path:
sys.path.append(site_package)
def _cmd(self, action, options, module):
if options is not None and "--user" in options:
self._ensure_user_site_package()
cmd = [PYPATH, "-m", "pip", action]
if options is not None:
cmd.extend(options.split(" "))
cmd.append(module)
return self._run(cmd)
def _popen(self, cmd):
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
for stdout_line in iter(popen.stdout.readline, ""):
yield stdout_line
popen.stdout.close()
popen.wait()
def _run(self, cmd):
res = False
status = ""
for line in self._popen(cmd):
if "ERROR:" in line:
status = line.strip()
if "Error:" in line:
status = line.strip()
print(line)
if "Successfully" in line:
status = line.strip()
res = True
return res, status
def _ensurepip(self):
pip_not_found = False
try:
import pip
except ImportError:
pip_not_found = True
pass
if pip_not_found:
self._run([PYPATH, "-m", "ensurepip", "--default-pip"])
@staticmethod
def upgrade_pip():
return Pip()._cmd("install", "--upgrade", "pip")
@staticmethod
def uninstall(module, options=None):
"""
:param module: string module name with requirements see:[1]
:param options: string command line options see:[2]
:return: True on uninstall, False if already removed, raise on Error
[1] https://pip.pypa.io/en/stable/reference/pip_install/#id29
[2] https://pip.pypa.io/en/stable/reference/pip_install/#id47
"""
if options is None or options.strip() == "":
# force confirm
options = "-y"
return Pip()._cmd("uninstall", options, module)
@staticmethod
def install(module, options=None):
"""
:param module: string module name with requirements see:[1]
:param options: string command line options see:[2]
:return: True on install, False if already there, raise on Error
[1] https://pip.pypa.io/en/stable/reference/pip_install/#id29
[2] https://pip.pypa.io/en/stable/reference/pip_install/#id47
"""
if options is None or options.strip() == "":
# store in user writable directory, use wheel, without deps
options = "--user --only-binary all --no-deps"
#options = "--only-binary all --no-deps"
return Pip()._cmd("install", options, module)
@staticmethod
def blender_version():
"""
:return: blender version tuple
"""
return bpy.app.version
@staticmethod
def python_version():
"""
:return: python version object
"""
import sys
# version.major, version.minor, version.micro
return sys.version_info