Repository navigation
Expand file tree
/
Copy pathdependencies.py
More file actions
538 lines (433 loc) · 17.4 KB
/
Copy pathdependencies.py
File metadata and controls
538 lines (433 loc) · 17.4 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
import os
import sys
import textwrap
from collections.abc import Sequence
from enum import Enum, auto
from pathlib import Path
from typing import NamedTuple
import bpy
from bpy.props import (
BoolVectorProperty,
)
from bpy.types import (
AddonPreferences,
Operator,
Panel,
UILayout,
)
class Dependency(NamedTuple):
name: str
ui_label: str
required: bool
supported: bool
license_url: str
description: str
safe_to_reload: bool
extra_index_url: str
version: str
hashes: tuple[str, ...]
DEPENDENCIES = (
Dependency(
"szio",
"szio",
True,
True,
"",
"Core functionality for import/export of asset files.",
True,
"",
"1.1.0",
("ee6fd4300e48c2ce7b70f569878c615e1f642974787006ebafe40a4f3823a2b2",),
),
Dependency(
"pymateria",
"PyMateria",
False,
sys.platform == "win32",
"https://static.cfx.re/PyMateria-License-Agreement.pdf",
"Allows direct binary assets import/export and automatic vehicle shattermap generation using the Materia library.",
False,
"https://static.cfx.re/whl/",
"0.1.1",
(
{
# win amd64
(3, 10): "830867304a8986d89cfe4dc49f26c8e014f2c2558dd4e208ba3247588cf6ba16",
(3, 11): "c204fafc411dfd85992565c8fe1c16af0ee4e7af47620e8403e25d5103103795",
(3, 12): "24bfd244b95e2fa519116b62dcb1f49387e67da8032c1c9934769956d19f4135",
(3, 13): "232052dd8c6942fa8a96af8bbaa5133708640e6008035ae3a32be285d52f297d",
(3, 14): "91d384a543521089b0f4abb214e64b1839ef6afbedfa6574ee90be826944dbad",
}.get(sys.version_info[:2], "unknown"),
),
),
)
DEPENDENCIES_REQUIRED = tuple(d for d in DEPENDENCIES if d.required)
DEPENDENCIES_OPTIONAL = tuple(d for d in DEPENDENCIES if not d.required)
class DependencyState(Enum):
UNINSTALLED = auto()
INSTALLED = auto()
INSTALLED_OUTDATED = auto()
def get_module_version(name: str) -> str | None:
import importlib.metadata
try:
return importlib.metadata.version(name)
except importlib.metadata.PackageNotFoundError:
return None
def check_module(name: str, version: str) -> DependencyState:
import importlib.util
if importlib.util.find_spec(name) is not None:
if get_module_version(name) != version:
return DependencyState.INSTALLED_OUTDATED
return DependencyState.INSTALLED
return DependencyState.UNINSTALLED
def dependencies_available_state() -> dict[str, DependencyState]:
return {
dep.name: check_module(dep.name, dep.version) if dep.supported else DependencyState.UNINSTALLED
for dep in DEPENDENCIES
}
def site_packages_path() -> Path:
from .known_paths import data_directory_path
ver_major, ver_minor = sys.version_info[:2]
return Path(data_directory_path()) / "lib" / f"python{ver_major}.{ver_minor}" / "site-packages"
def requirements_path() -> Path:
from .known_paths import data_directory_path
data_dir = Path(data_directory_path())
data_dir.mkdir(parents=True, exist_ok=True)
return data_dir / "requirements.txt"
def offline_index_path() -> Path:
return Path(__file__).parent / "offline_index"
def has_online_access() -> bool:
return bpy.app.version < (4, 2, 0) or bpy.app.online_access
_needs_restart = False
def has_required_dependencies() -> bool:
return (
not _needs_restart
and IS_SZIO_AVAILABLE
and all(s != DependencyState.INSTALLED_OUTDATED for s in dependencies_available_state().values())
)
def generate_requirements_file_contents(
dependencies: Sequence[Dependency],
offline_index: str | None,
) -> str:
r = ""
if offline_index:
r += "--no-index\n"
r += f"--find-links {offline_index}\n"
else:
for dep in dependencies:
if not dep.extra_index_url:
continue
r += f"--extra-index-url {dep.extra_index_url}\n"
for dep in dependencies:
assert dep.name
assert dep.version
assert dep.hashes
r += f"{dep.name}=={dep.version} "
r += " ".join(f"--hash=sha256:{h}" for h in dep.hashes)
r += "\n"
return r
def filter_dependencies_to_install(
dependencies: Sequence[Dependency],
optional_dependencies_to_install: set[str],
) -> list[Dependency]:
return [d for d in dependencies if d.supported and (d.required or d.name in optional_dependencies_to_install)]
def build_install_dependencies_command(
requirements_file: str,
site_packages: str,
) -> list[str]:
return [
sys.executable,
"-m",
"pip",
"install",
"-v",
"--target",
site_packages,
"--no-deps",
"--require-hashes",
"-r",
requirements_file,
]
def install_dependencies(online_access_override: bool = False, optional_dependencies_to_install: set[str] = set()):
import subprocess
site_packages = site_packages_path()
offline_index = offline_index_path()
has_offline_index = offline_index.is_dir()
if not online_access_override and not has_online_access() and not has_offline_index:
return False
if site_packages.is_dir():
# Rename existing site-packages instead of directly delete it because .pyd files may still be in use and
# wouldn't be possible to delete them
site_packages_old = site_packages.with_name("site-packages.old")
if site_packages_old.is_dir():
import shutil
shutil.rmtree(site_packages_old)
site_packages.rename(site_packages_old)
dependencies_to_install = filter_dependencies_to_install(DEPENDENCIES, optional_dependencies_to_install)
requirements_contents = generate_requirements_file_contents(
dependencies_to_install, str(offline_index) if has_offline_index else None
)
requirements_file = requirements_path()
requirements_file.write_text(requirements_contents)
cmd = build_install_dependencies_command(
str(requirements_file),
str(site_packages),
)
print(f"{cmd=}")
retcode = subprocess.call(cmd)
return retcode == 0
def mount_dependencies():
def _add_site_packages():
site_packages = site_packages_path()
if site_packages.is_dir():
site_packages = str(site_packages)
if site_packages not in sys.path:
sys.path.append(site_packages)
_add_site_packages()
state = dependencies_available_state()
if os.getenv("CI"):
# On CI, force install the dependencies
any_missing_dependencies = any(s != DependencyState.INSTALLED for s in state.values())
if any_missing_dependencies:
unmount_dependencies()
install_dependencies(online_access_override=True, optional_dependencies_to_install={"pymateria"})
_add_site_packages()
state = dependencies_available_state()
global IS_SZIO_AVAILABLE, IS_SZIO_NATIVE_AVAILABLE, PYMATERIA_REQUIRED_MSG
IS_SZIO_AVAILABLE = state.get("szio", DependencyState.UNINSTALLED) == DependencyState.INSTALLED
IS_SZIO_NATIVE_AVAILABLE = (
IS_SZIO_AVAILABLE and state.get("pymateria", DependencyState.UNINSTALLED) == DependencyState.INSTALLED
)
PYMATERIA_REQUIRED_MSG = (
""
if IS_SZIO_NATIVE_AVAILABLE
else PYMATERIA_UNAVAILABLE_MSG
if IS_SZIO_NATIVE_SUPPORTED
else PYMATERIA_UNSUPPORTED_MSG
)
if not has_required_dependencies():
def _ask_to_install_dependencies():
bpy.ops.sollumz.install_dependencies("INVOKE_DEFAULT")
bpy.app.timers.register(_ask_to_install_dependencies, first_interval=0.5, persistent=True)
def reload_dependencies():
module_names = list(sys.modules.keys())
for dep in DEPENDENCIES:
if not dep.safe_to_reload:
continue
dep_module_prefix = f"{dep.name}."
for name in module_names:
if name == dep.name or name.startswith(dep_module_prefix):
del sys.modules[name]
def unmount_dependencies():
site_packages = str(site_packages_path())
if site_packages in sys.path:
sys.path.remove(site_packages)
reload_dependencies()
IS_SZIO_AVAILABLE = False
IS_SZIO_NATIVE_AVAILABLE = False
IS_SZIO_NATIVE_SUPPORTED = DEPENDENCIES_OPTIONAL[0].supported # pymateria
PYMATERIA_UNAVAILABLE_MSG = "Binary resource formats require PyMateria which is not available."
PYMATERIA_UNSUPPORTED_MSG = "Binary resource formats require PyMateria which is not supported on this platform."
PYMATERIA_REQUIRED_MSG = ""
OFFLINE_MSG = (
'Internet access is required to download the missing dependencies but it is currently disabled in "System" '
'preferences. Clicking "Install" will override this preference and proceed with the installation.'
)
_minimal_classes = []
def register_minimal():
"""When missing required dependencies, only this will be registered instead of the whole add-on."""
if has_required_dependencies():
return
# Defines these classes here as in regular usage these won't be used at all, more hidden this way.
class SOLLUMZ_OT_install_dependencies(Operator):
bl_idname = "sollumz.install_dependencies"
bl_label = "Sollumz: Install Dependencies?"
bl_description = "Install dependencies"
optional_dependencies: BoolVectorProperty(
size=len(DEPENDENCIES_OPTIONAL), default=(True,) * len(DEPENDENCIES_OPTIONAL)
)
def execute(self, context):
optional_dependencies_to_install = {
DEPENDENCIES_OPTIONAL[i].name for i, enabled in enumerate(self.optional_dependencies) if enabled
}
state = dependencies_available_state()
should_restart = any(
(not dep.safe_to_reload and state[dep.name] == DependencyState.INSTALLED_OUTDATED)
for dep in DEPENDENCIES_OPTIONAL
)
if install_dependencies(
online_access_override=True, optional_dependencies_to_install=optional_dependencies_to_install
):
global _needs_restart
_needs_restart = should_restart
if _needs_restart:
self.report({"INFO"}, "Successfully installed dependencies. Please, restart Blender.")
else:
self.report({"INFO"}, "Successfully installed dependencies. Reloading add-ons...")
bpy.ops.script.reload()
return {"FINISHED"}
else:
self.report({"ERROR"}, "Failed to install dependencies.")
return {"CANCELLED"}
def invoke(self, context: bpy.types.Context, event):
if bpy.app.version >= (4, 1, 0):
return context.window_manager.invoke_props_dialog(
self,
width=450,
title="Sollumz: Install Dependencies?",
confirm_text="Install",
)
else:
return context.window_manager.invoke_props_dialog(self, width=450)
def draw(self, context):
layout = self.layout
main_col = layout.column()
_draw_wrapped_text(
context,
main_col,
"These dependencies are needed for Sollumz to work correctly.",
width=550,
width_percent=1.0,
dim=False,
)
state = dependencies_available_state()
def _is_outdated(dep: Dependency) -> bool:
return state.get(dep.name, DependencyState.UNINSTALLED) == DependencyState.INSTALLED_OUTDATED
col = main_col.column()
col.label(text="Required:")
for dep in DEPENDENCIES_REQUIRED:
label = f"{dep.ui_label} {dep.version}"
if _is_outdated(dep) and (installed_version := get_module_version(dep.name)):
label += f" (installed {installed_version})"
col.label(text=label, icon="DOT")
_draw_wrapped_text(
context,
col,
dep.description,
width=400,
width_percent=1.0,
dim=False,
icon="BLANK1",
char_pixels=6,
)
col = main_col.column()
col.label(text="Optional:")
for i, dep in enumerate(DEPENDENCIES_OPTIONAL):
label = f" {dep.ui_label} {dep.version}"
if _is_outdated(dep) and (installed_version := get_module_version(dep.name)):
label += f" (installed {installed_version})"
row = col.row()
row.alignment = "LEFT"
subrow = row.row()
subrow.alignment = "LEFT"
subrow.enabled = dep.supported
subrow.prop(self, "optional_dependencies", index=i, text=label)
subrow = row.row()
subrow.alignment = "LEFT"
subrow.emboss = "PULLDOWN_MENU"
subrow.operator("wm.url_open", text="License", icon="URL").url = dep.license_url
if not dep.supported:
_draw_wrapped_text(
context,
col,
f"{dep} is not supported on this platform.",
width=400,
width_percent=1.0,
dim=False,
icon="ERROR",
char_pixels=6,
)
_draw_wrapped_text(
context,
col,
dep.description,
width=400,
width_percent=1.0,
dim=False,
icon="BLANK1",
char_pixels=6,
)
if not has_online_access():
main_col.separator()
main_col.separator()
main_col.separator()
_draw_wrapped_text(
context, main_col, OFFLINE_MSG, width=450, width_percent=1.0, dim=False, icon="ERROR", char_pixels=6
)
class SOLLUMZ_PT_deps_minimal(Panel):
bl_label = "Missing Dependencies"
bl_idname = "SOLLUMZ_PT_deps_minimal"
bl_category = "Sollumz Tools"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_options = set()
bl_order = 0
def draw_header(self, context):
self.layout.label(text="", icon="ERROR")
def draw(self, context):
layout = self.layout
if _needs_restart:
layout.label(text="Dependencies upgraded. Restart Blender.")
else:
layout.operator(SOLLUMZ_OT_install_dependencies.bl_idname, text="Install Dependencies")
class SollumzDepsMinimalAddonPreferences(AddonPreferences):
bl_idname = __package__
def draw(self, context):
layout = self.layout
# Based on CenterAlignMixIn from Blender's scripts/startup/bl_ui/space_userpref.py
width = context.region.width
ui_scale = context.preferences.system.ui_scale
# No horizontal margin if region is rather small.
is_wide = width > (550 * ui_scale)
layout.use_property_split = True
layout.use_property_decorate = False # No animation.
row = layout.row()
if is_wide:
row.label() # Needed so col below is centered.
col = row.column()
col.ui_units_x = 95
self._draw_install_dependencies(context, layout)
if is_wide:
row.label() # Needed so col above is centered.
def _draw_install_dependencies(self, context, layout: UILayout):
row = layout.row()
if _needs_restart:
row.label(text="Dependencies upgraded. Restart Blender.")
else:
row.label(text="Missing dependencies", icon="ERROR")
row.operator(SOLLUMZ_OT_install_dependencies.bl_idname, text="Install")
def _draw_wrapped_text(
context,
layout: UILayout,
text: str,
width_percent: float = 0.99,
dim: bool = True,
width: int | None = None,
icon: str | None = None,
char_pixels: int = 7,
):
chars = int((width or context.region.width) * width_percent / char_pixels)
col = layout.column()
col.active = False if dim else True
col.scale_y = 0.65
has_icon = bool(icon)
icon = icon or "NONE"
for text_line in text.splitlines():
first_indent = len(text_line) - len(text_line.lstrip(" "))
wrapper = textwrap.TextWrapper(width=chars, subsequent_indent=" " * first_indent)
for wrapped_text_line in wrapper.wrap(text=text_line):
col.label(text=wrapped_text_line, icon=icon)
if has_icon:
icon = "BLANK1"
_minimal_classes.extend(
[
SollumzDepsMinimalAddonPreferences,
SOLLUMZ_OT_install_dependencies,
SOLLUMZ_PT_deps_minimal,
]
)
for cls in _minimal_classes:
bpy.utils.register_class(cls)
def unregister_minimal():
for cls in reversed(_minimal_classes):
bpy.utils.unregister_class(cls)