-
Notifications
You must be signed in to change notification settings - Fork 12
/
pip_importer.py
355 lines (281 loc) · 10.7 KB
/
pip_importer.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from typing import Optional
from dataclasses import dataclass
import bpy
from bpy.types import AddonPreferences, Panel, Menu
from bpy.types import Operator
import addon_utils
import sys
import subprocess
from pathlib import Path
PYPATH = sys.executable
# separate packages with spaces
pip_packages = []
# flag to indicate if the required packages were imported in this session
just_imported = False
@dataclass
class Package:
name: str
custom_module: Optional[str] = None
version: str = ""
install_manualy: bool = False
_registered: bool = False
_summary: str = ""
_home_page: str = ""
_author: str = ""
_license: str = ""
_location: str = ""
@property
def module(self) -> str:
if self.custom_module is None:
return self.name
return self.custom_module
def add_package(package):
pip_packages.append(package)
def auto_install_packages():
for package in pip_packages:
try:
check_module(package)
if not package._registered:
install_package(package)
except ModuleNotFoundError as e:
pass
def check_module(package):
# Note: Blender might be installed in a directory that needs admin rights and thus defaulting to a user installation.
# That path however might not be in sys.path....
import sys, site
p = site.USER_SITE
if p not in sys.path:
sys.path.append(p)
try:
module = sys.modules[package.module]
if hasattr(module, '__path__'):
package._location = module.__path__[0]
package._registered = True
except KeyError:
package._registered = False
try:
# check if the module can be properly imported..
__import__(package.module)
get_package_show(package)
except ModuleNotFoundError:
pass
return package._registered
def get_package_show(package):
try:
cmd = [PYPATH, "-m", "pip", "show", package.name]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
store_package_show(package, result)
except:
pass
def store_package_show(package, result):
# Initialize an empty dictionary
data = {}
# Split the output into lines and parse each line
for line in result.stdout.splitlines():
if ": " in line:
key, value = line.split(": ", 1) # Only split on the first occurrence
data[key.strip()] = value.strip()
if len(data) > 0:
# there seems to be a valid module installed
package._summary = data.get('Summary')
package._home_page = data.get('Home-page')
package._author = data.get('Author')
package._license = data.get('License')
package._location = data.get('Location')
package._registered = True
return True
return False
def check_modules():
# Note: Blender might be installed in a directory that needs admin rights and thus defaulting to a user installation.
# That path however might not be in sys.path....
for package in pip_packages:
check_module(package)
def get_prefs():
return bpy.context.preferences.addons[__package__].preferences
def install_pip():
cmd = [PYPATH, "-m", "ensurepip", "--upgrade"]
return not subprocess.call(cmd)
def update_pip():
cmd = [PYPATH, "-m", "pip", "install", "--upgrade", "pip"]
return not subprocess.call(cmd)
def install_package(package, file_path):
update_pip()
if package.install_manualy:
cmd = [PYPATH, "-m", "pip", "install", "--upgrade", file_path]
ok = subprocess.call(cmd) == 0
else:
cmd = [PYPATH, "-m", "pip", "install", "--upgrade", f"{package.name}{package.version}"]
ok = subprocess.call(cmd) == 0
return ok
def uninstall_package(package):
update_pip()
cmd = [PYPATH, "-m", "pip", "uninstall", "-y", package.name]
ok = subprocess.call(cmd) == 0
package._registered = False
return ok
def ensure_pip():
if subprocess.call([PYPATH, "-m", "pip", "--version"]):
return install_pip()
return True
def get_wheel():
p = Path(__file__).parent.absolute()
from sys import platform, version_info
if platform == "linux" or platform == "linux2":
# Linux
platform_strig = "linux"
elif platform == "darwin":
# OS X
platform_strig = "macosx"
elif platform == "win32":
# Windows
platform_strig = "win"
matches = list(
p.glob(
"**/*cp{}{}*{}*.whl".format(
version_info.major, version_info.minor, platform_strig
)
)
)
if matches:
match = matches[0]
return match.as_posix()
return ""
### Presets
def get_scale():
return bpy.context.preferences.system.ui_scale * get_prefs().entity_scale
def is_experimental():
return get_prefs().show_debug_settings
class PiPPreferences(AddonPreferences):
bl_idname = __package__
bl_label = "pip installer"
def draw(self, context):
layout = self.layout
layout.use_property_split = True
system = context.preferences.system
scene = context.scene
spout_addon_props = scene.spout_addon_props
allInstalled = True
if just_imported:
box = layout.box()
row = box.row()
box.label(text="Restart the addon to make it functional!", icon="ERROR")
# layout.label(text="Ideal setting for usage of texture sharing is: Single pass Anti-Aliasing")
# layout.prop(system, "viewport_aa")
for package in pip_packages:
box = layout.box()
box.label(text=package.name)
row = box.row().split(factor=0.2)
if package._registered:
row.label(text="Registered", icon="CHECKMARK")
row.label(text=package._location)
row.operator(
Pip_Uninstall_package.bl_idname,
text="uninstall",
).package_path=package.name
else:
allInstalled = False
row.label(text="Not installed", icon="CANCEL")
if package.install_manualy:
row.prop(spout_addon_props, 'my_file_path')
row.operator(
Pip_Install_packages.bl_idname,
text="install"
).package_path=package.name
# Refresh operator
class Pip_Update_package(Operator):
"""refresh module from local .whl file or from PyPi"""
bl_idname = "view3d.pip_refresh_package"
bl_label = "Install"
package_path: bpy.props.StringProperty(subtype="FILE_PATH")
def execute(self, context):
scene = context.scene
spout_addon_props = scene.spout_addon_props
package = {e.name: e for e in pip_packages}[str(self.package_path)]
if not self.package_path:
self.report({"WARNING"}, "Specify package to be installed")
return {"CANCELLED"}
if install_package(package, spout_addon_props.my_file_path):
self.report({"INFO"}, "Testing Package {} import..".format(package.module))
if check_module(package):
self.report({"INFO"}, "Package successfully installed")
else:
self.report({"WARNING"}, "Package should be available but cannot be found, check console for detailed info. Try restarting blender, otherwise get in contact.")
else:
self.report({"WARNING"}, "Cannot install package: {}".format(self.package_path))
return {"CANCELLED"}
return {"FINISHED"}
# Uninstall operator
class Pip_Uninstall_package(Operator):
"""uninstall module from local .whl file or from PyPi"""
bl_idname = "view3d.pip_uninstall_package"
bl_label = "Uninstall"
package_path: bpy.props.StringProperty(subtype="FILE_PATH")
def execute(self, context):
package = {e.name: e for e in pip_packages}[str(self.package_path)]
if not self.package_path:
self.report({"WARNING"}, "Specify package to be uninstalled")
return {"CANCELLED"}
if uninstall_package(package):
package._registered = False
self.report({"INFO"}, "Package successfully uninstalled")
else:
self.report({"WARNING"}, "Cannot uninstall package: {}".format(self.package_path))
return {"CANCELLED"}
return {"FINISHED"}
# installation operator
class Pip_Install_packages(Operator):
"""Install modules from local .whl file or from PyPi"""
bl_idname = "view3d.pip_install_packages"
bl_label = "Install"
package_path: bpy.props.StringProperty(subtype="FILE_PATH")
def execute(self, context):
scene = context.scene
spout_addon_props = scene.spout_addon_props
if not ensure_pip():
self.report(
{"WARNING"},
"PIP is not available and cannot be installed, please install PIP manually",
)
return {"CANCELLED"}
package = {e.name: e for e in pip_packages}[str(self.package_path)]
if install_package(package, spout_addon_props.my_file_path):
self.report({"INFO"}, "Testing Package {} import..".format(package.module))
if check_module(package):
self.report({"INFO"}, "Package successfully installed")
else:
self.report({"WARNING"}, "Package should be available but cannot be found, check console for detailed info. Try restarting blender, otherwise get in contact.")
else:
self.report({"WARNING"}, "Cannot install package: {}".format(self.package_path))
global just_imported
just_imported = True
return {"FINISHED"}
class SpoutAddonProperties(bpy.types.PropertyGroup):
# Define a StringProperty for the filepath
my_file_path: bpy.props.StringProperty(
name="Wheel file",
description="Path to the file",
default="",
maxlen=1024,
subtype='FILE_PATH' # This subtype turns the StringProperty into a file picker
)
classes = (
Pip_Update_package,
Pip_Uninstall_package,
Pip_Install_packages,
PiPPreferences,
SpoutAddonProperties
)
def register():
global pip_packages
pip_packages.clear()
global just_imported
just_imported = False
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.Scene.spout_addon_props = bpy.props.PointerProperty(type=SpoutAddonProperties)
def unregister():
del bpy.types.Scene.spout_addon_props
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)