Welcome to mirror list, hosted at ThFree Co, Russian Federation.

package.py « bl_operators « startup « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d6e9e7891cdb48a08e9fd1a0c0b254fe2a5c61cb (plain)
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# HACK:
# due to lack of fork() on windows, multiprocessing will re-execute this module
# in a new process. In such cases we only need subproc, everything else is only
# used to spawn the subprocess in the first place.
try:
    import bpy
    from bpy.types import Operator
except ImportError:
    from bpkg import subproc
else:
    import logging
    import bpkg
    from bpkg import (
        subproc,
        messages,
    )
    from bpkg.types import (
        Package,
        ConsolidatedPackage,
    )
    from pathlib import Path
    from collections import OrderedDict
    import multiprocessing

    # Under windows, multiprocessing must start a new process entirely. It
    # expects sys.executable to point to python, but in blender sys.executable
    # points to blender's executable. We can override this with set_executable,
    # but this acts globally unless we make a special context.
    # Also see:
    # https://docs.python.org/3.6/library/multiprocessing.html#multiprocessing.set_executable
    mp_context = multiprocessing.get_context()
    mp_context.set_executable(bpy.app.binary_path_python)

    class SubprocMixin:
        """Mix-in class for things that need to be run in a subprocess."""

        log = logging.getLogger(__name__ + '.SubprocMixin')
        _state = 'INITIALIZING'
        # time at which we stop waiting for an abort response and just terminate the process
        _abort_timeout = 0
        # how long to wait (in seconds) to forcibly terminate subprocess after quit
        _abort_wait = 10

        # Mapping from message type (see bpkg.messages) to handler function.
        # Should be constructed before modal() gets called.
        msg_handlers = {}

        def execute(self, context):
            return self.invoke(context, None)

        def quit(self):
            """Signals the state machine to stop this operator from running."""
            self._state = 'QUIT'

        def invoke(self, context, event):
            self.pipe_blender, self.pipe_subproc = multiprocessing.Pipe()

            # The subprocess should just be terminated when Blender quits. Without this,
            # Blender would hang while closing, until the subprocess terminates itself.
            # TODO: Perhaps it would be better to fork when blender exits?
            self.process = self.create_subprocess()
            self.process.daemon = True
            self.process.start()

            self._state = 'RUNNING'

            wm = context.window_manager
            wm.modal_handler_add(self)
            self.timer = wm.event_timer_add(0.1, context.window)

            return {'RUNNING_MODAL'}

        def modal(self, context, event):
            import time

            if event.type != 'TIMER':
                return {'PASS_THROUGH'}

            if self._state == 'ABORTING' and time.time() > self._abort_timeout:
                self.log.error(
                    'No response from subprocess to abort request, terminating it.')
                self.report(
                    {'ERROR'}, 'No response from subprocess to abort request, terminating it.')
                self.process.terminate()
                self._finish(context)
                return {'CANCELLED'}

            while self.pipe_blender.poll():
                self.handle_received_data()

            if self._state == 'QUIT':
                self._finish(context)
                return {'FINISHED'}

            if not self.process.is_alive():
                self.report_process_died()
                self._finish(context)
                return {'CANCELLED'}

            return {'RUNNING_MODAL'}

        def abort(self):
            import time

            # Allow the subprocess 10 seconds to repsond to our abort message.
            self._abort_timeout = time.time() + self._abort_wait
            self._state = 'ABORTING'

            self.pipe_blender.send(messages.Abort())

        def _finish(self, context):
            try:
                self.cancel(context)
            except AttributeError:
                pass

            global bpkg_operation_running

            context.window_manager.event_timer_remove(self.timer)
            bpkg_operation_running = False

            if self.process and self.process.is_alive():
                self.log.debug('Waiting for subprocess to quit')
                try:
                    self.process.join(timeout=self._abort_wait)
                except multiprocessing.TimeoutError:
                    self.log.warning(
                        'Subprocess is hanging, terminating it forcefully.')
                    self.process.terminate()
                else:
                    self.log.debug(
                        'Subprocess stopped with exit code %i', self.process.exitcode)

        def handle_received_data(self):
            recvd = self.pipe_blender.recv()

            self.log.debug('Received message from subprocess: %s', recvd)
            try:
                handler = self.msg_handlers[type(recvd)]
            except KeyError:
                self.log.error('Unable to handle received message %s', recvd)
                # Maybe we shouldn't show this to the user?
                self.report(
                    {'WARNING'}, 'Unable to handle received message %s' % recvd)
                return

            handler(recvd)

        def create_subprocess(self):
            """Implement this in a subclass.

            :rtype: multiprocessing.Process
            """
            raise NotImplementedError()

        def report_process_died(self):
            """Provides the user with sensible information when the process has died.

            Implement this in a subclass.
            """
            raise NotImplementedError()

    class PACKAGE_OT_install(SubprocMixin, Operator):
        bl_idname = 'package.install'
        bl_label = 'Install package'
        bl_description = 'Downloads and installs a Blender add-on package'
        bl_options = {'REGISTER'}

        package_name = bpy.props.StringProperty(
            name='package_name',
            description='The name of the package to install'
        )

        log = logging.getLogger(__name__ + '.PACKAGE_OT_install')

        def invoke(self, context, event):
            if not self.package_name:
                self.report({'ERROR'}, 'Package name not given')
                return {'CANCELLED'}

            return super().invoke(context, event)

        def create_subprocess(self):
            """Starts the download process.

            Also registers the message handlers.

            :rtype: multiprocessing.Process
            """

            self.msg_handlers = {
                messages.Progress: self._subproc_progress,
                messages.DownloadError: self._subproc_download_error,
                messages.InstallError: self._subproc_install_error,
                messages.Success: self._subproc_success,
                messages.Aborted: self._subproc_aborted,
            }

            package = bpkg.packages[self.package_name].get_latest_version()

            import pathlib

            # TODO: We need other paths besides this one on subprocess end, so it might be better to pass them all at once.
            # For now, just pass this one.
            install_path = pathlib.Path(
                bpy.utils.user_resource('SCRIPTS', 'addons', create=True))
            self.log.debug("Using %s as install path", install_path)

            import addon_utils
            proc = mp_context.Process(target=subproc.download_and_install_package,
                                      args=(self.pipe_subproc, package, install_path))
            return proc

        def _subproc_progress(self, progress: messages.Progress):
            self.log.info('Task progress at %i%%', progress.progress * 100)

        def _subproc_download_error(self, error: messages.DownloadError):
            self.report({'ERROR'}, 'Unable to download package: %s' %
                        error.message)
            self.quit()

        def _subproc_install_error(self, error: messages.InstallError):
            self.report({'ERROR'}, 'Unable to install package: %s' %
                        error.message)
            self.quit()

        def _subproc_success(self, success: messages.Success):
            self.report({'INFO'}, 'Package installed successfully')
            bpkg.refresh_packages()
            bpy.context.area.tag_redraw()
            self.quit()

        def _subproc_aborted(self, aborted: messages.Aborted):
            self.report(
                {'ERROR'}, 'Package installation aborted per your request')
            self.quit()

        def report_process_died(self):
            if self.process.exitcode:
                self.log.error(
                    'Process died without telling us! Exit code was %i', self.process.exitcode)
                self.report(
                    {'ERROR'}, 'Error downloading package, exit code %i' % self.process.exitcode)
            else:
                self.log.error(
                    'Process died without telling us! Exit code was 0 though')
                self.report(
                    {'WARNING'}, 'Error downloading package, but process finished OK. This is weird.')

    class PACKAGE_OT_uninstall(SubprocMixin, Operator):
        bl_idname = 'package.uninstall'
        bl_label = 'Install package'
        bl_description = "Remove installed package files from filesystem"
        bl_options = {'REGISTER'}

        package_name = bpy.props.StringProperty(
            name='package_name', description='The name of the package to uninstall')

        log = logging.getLogger(__name__ + '.PACKAGE_OT_uninstall')

        def invoke(self, context, event):
            if not self.package_name:
                self.report({'ERROR'}, 'Package name not given')
                return {'CANCELLED'}

            return super().invoke(context, event)

        def create_subprocess(self):
            """Starts the uninstall process and registers the message handlers.
            :rtype: multiprocessing.Process
            """

            self.msg_handlers = {
                messages.UninstallError: self._subproc_uninstall_error,
                messages.Success: self._subproc_success,
            }

            import pathlib
            install_path = pathlib.Path(
                bpy.utils.user_resource('SCRIPTS', 'addons', create=True))

            package = bpkg.packages[self.package_name].get_latest_version()

            proc = mp_context.Process(target=subproc.uninstall_package,
                                      args=(self.pipe_subproc, package, install_path))
            return proc

        def _subproc_uninstall_error(self, error: messages.InstallError):
            self.report({'ERROR'}, error.message)
            self.quit()

        def _subproc_success(self, success: messages.Success):
            self.report({'INFO'}, 'Package uninstalled successfully')
            bpkg.refresh_packages()
            bpy.context.area.tag_redraw()
            self.quit()

        def report_process_died(self):
            if self.process.exitcode:
                self.log.error(
                    'Process died without telling us! Exit code was %i', self.process.exitcode)
                self.report(
                    {'ERROR'}, 'Error downloading package, exit code %i' % self.process.exitcode)
            else:
                self.log.error(
                    'Process died without telling us! Exit code was 0 though')
                self.report(
                    {'WARNING'}, 'Error downloading package, but process finished OK. This is weird.')

    class PACKAGE_OT_refresh(SubprocMixin, Operator):
        bl_idname = "package.refresh"
        bl_label = "Refresh"
        bl_description = 'Check repositories for new and updated packages'
        bl_options = {'REGISTER'}

        log = logging.getLogger(__name__ + ".PACKAGE_OT_refresh")
        _running = False

        def invoke(self, context, event):
            wm = context.window_manager
            self.repositories = wm.package_repositories
            if not self.repositories:
                bpkg.refresh_packages()
                return {'FINISHED'}

            PACKAGE_OT_refresh._running = True
            return super().invoke(context, event)

        @classmethod
        def poll(cls, context):
            return not cls._running

        def _finish(self, context):
            super()._finish(context)
            PACKAGE_OT_refresh._running = False
            context.area.tag_redraw()

        def create_subprocess(self):
            """Starts the download process.

            Also registers the message handlers.

            :rtype: multiprocessing.Process
            """

            self.msg_handlers = {
                messages.Progress: self._subproc_progress,
                messages.SubprocError: self._subproc_error,
                messages.DownloadError: self._subproc_download_error,
                messages.Success: self._subproc_success,
                messages.BadRepositoryError: self._subproc_repository_error,
                messages.Aborted: self._subproc_aborted,
            }

            import pathlib

            storage_path = pathlib.Path(bpy.utils.user_resource(
                'CONFIG', 'repositories', create=True))
            repository_urls = [repo.url for repo in self.repositories]
            self.log.debug("Repository urls %s", repository_urls)

            proc = mp_context.Process(target=subproc.refresh_repositories,
                                      args=(self.pipe_subproc, storage_path, repository_urls))
            return proc

        def _subproc_progress(self, progress: messages.Progress):
            self.log.info('Task progress at %i%%', progress.progress * 100)

        def _subproc_error(self, error: messages.SubprocError):
            self.report(
                {'ERROR'}, 'Unable to refresh package list: %s' % error.message)
            self.quit()

        def _subproc_download_error(self, error: messages.DownloadError):
            self.report(
                {'ERROR'}, 'Unable to download package list: %s' % error.message)
            self.quit()

        def _subproc_repository_error(self, error: messages.BadRepositoryError):
            self.report({'ERROR'}, str(error.message))
            self.quit()

        def _subproc_success(self, success: messages.Success):
            self.report({'INFO'}, 'Finished refreshing lists')
            bpkg.refresh_repository_props()
            bpkg.refresh_packages()
            self.quit()

        def _subproc_aborted(self, aborted: messages.Aborted):
            self.report(
                {'ERROR'}, 'Package list retrieval aborted per your request')
            self.quit()

        def report_process_died(self):
            if self.process.exitcode:
                self.log.error(
                    'Refresh process died without telling us! Exit code was %i', self.process.exitcode)
                self.report(
                    {'ERROR'}, 'Error refreshing package lists, exit code %i' % self.process.exitcode)
            else:
                self.log.error(
                    'Refresh process died without telling us! Exit code was 0 though')
                self.report(
                    {'WARNING'}, 'Error refreshing package lists, but process finished OK. This is weird.')

    class PACKAGE_UL_repositories(bpy.types.UIList):
        def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
            layout.alignment = 'LEFT'
            layout.prop(item, "enabled", text="")
            if len(item.name) == 0:
                layout.label(item['url'])
            else:
                layout.label(item.name)

    class PACKAGE_OT_add_repository(Operator):
        bl_idname = "package.add_repository"
        bl_label = "Add Repository"

        url = bpy.props.StringProperty(name="Repository URL")

        def invoke(self, context, event):
            wm = context.window_manager
            return wm.invoke_props_dialog(self)

        def execute(self, context):
            wm = context.window_manager

            if not self.url:
                self.report({'ERROR'}, "Repository URL not specified")
                return {'CANCELLED'}

            for repo in wm.package_repositories:
                if repo['url'] == self.url:
                    self.report({'ERROR'}, "Repository already added")
                    return {'CANCELLED'}

            repo = wm.package_repositories.add()
            repo.url = bpkg.utils.sanitize_repository_url(self.url)

            context.area.tag_redraw()
            return {'FINISHED'}

    class PACKAGE_OT_remove_repository(Operator):
        bl_idname = "package.remove_repository"
        bl_label = "Remove Repository"

        def execute(self, context):
            wm = context.window_manager
            try:
                repo = wm.package_repositories[wm.package_active_repository]
            except AttributeError:
                return {'CANCELLED'}

            try:
                filepath = Path(repo['filepath'])
            except KeyError:
                pass
            else:
                if not filepath.exists():
                    raise ValueError("Failed find repository file")
                filepath.unlink()

            wm.package_repositories.remove(wm.package_active_repository)
            context.area.tag_redraw()
            return {'FINISHED'}

    class PACKAGE_OT_edit_repositories(Operator):
        bl_idname = "package.edit_repositories"
        bl_label = "Edit Repositories"

        def check(self, context):
            # TODO: always refresh settings for now
            return True

        def execute(self, context):
            bpy.ops.package.refresh()
            return {'FINISHED'}

        def invoke(self, context, event):
            wm = context.window_manager
            return wm.invoke_props_dialog(self, width=500, height=300)

        def draw(self, context):
            layout = self.layout
            wm = context.window_manager

            row = layout.row()
            row.template_list("PACKAGE_UL_repositories", "", wm,
                              "package_repositories", wm, "package_active_repository")
            col = row.column(align=True)
            col.operator("package.add_repository", text="", icon='ZOOMIN')
            col.operator("package.remove_repository", text="", icon='ZOOMOUT')

    class WM_OT_package_toggle_expand(Operator):
        bl_idname = "wm.package_toggle_expand"
        bl_label = ""
        bl_description = "Toggle display of extended information for given package (hold shift to collapse all other packages)"
        bl_options = {'INTERNAL'}

        log = logging.getLogger(__name__ + ".WM_OT_package_toggle_expand")

        package_name = bpy.props.StringProperty(
            name="Package Name",
            description="Name of package to expand/collapse",
        )

        def invoke(self, context, event):
            if event.shift:
                bpkg.display.expanded_packages.clear()
            if self.package_name in bpkg.display.expanded_packages:
                bpkg.display.expanded_packages.remove(self.package_name)
            else:
                bpkg.display.expanded_packages.append(self.package_name)

            return {'FINISHED'}

    class WM_OT_package_toggle_preferences(Operator):
        bl_idname = "wm.package_toggle_preferences"
        bl_label = ""
        bl_description = "Toggle display of package preferences"
        bl_options = {'INTERNAL'}

        package_name = bpy.props.StringProperty(
            name="Package Name",
            description="Name of package whos preferences to display",
        )

        def invoke(self, context, event):
            if USERPREF_PT_packages.preference_package == self.package_name:
                USERPREF_PT_packages.preference_package = None
            else:
                USERPREF_PT_packages.preference_package = self.package_name
            return {'FINISHED'}

    class PACKAGE_OT_toggle_enabled(Operator):
        bl_idname = "package.toggle_enabled"
        bl_label = ""
        bl_description = "Enable given package if it's disabled, and vice versa if it's enabled"

        log = logging.getLogger(__name__ + ".PACKAGE_OT_toggle_enabled")

        package_name = bpy.props.StringProperty(
            name="Package Name",
            description="Name of package to enable",
        )

        def execute(self, context):
            import addon_utils
            metapkg = bpkg.packages[self.package_name]

            if not metapkg.installed:
                self.report(
                    {'ERROR'}, "Can't enable package which isn't installed")
                return {'CANCELLED'}

            pkg = metapkg.get_latest_installed_version()
            if pkg.enabled:
                pkg.disable()
            else:
                pkg.enable()

            return {'FINISHED'}

    class PACKAGE_OT_disable(Operator):
        bl_idname = "package.disable"
        bl_label = ""
        bl_description = "Disable given package"

        log = logging.getLogger(__name__ + ".PACKAGE_OT_disable")

        package_name = bpy.props.StringProperty(
            name="Package Name",
            description="Name of package to disable",
        )

        def execute(self, context):
            package = bpkg.packages[self.package_name].get_display_version()

            if not package.module_name:
                self.log.error("Can't disable package without a module name")
                return {'CANCELLED'}

            ret = bpy.ops.wm.addon_disable(package.module_name)
            if ret == {'FINISHED'}:
                bpkg.packages[self.package_name].enabled = False
            return ret

classes = (
    PACKAGE_OT_install,
    PACKAGE_OT_uninstall,
    PACKAGE_OT_toggle_enabled,
    PACKAGE_OT_refresh,
    WM_OT_package_toggle_expand,
    WM_OT_package_toggle_preferences,
    PACKAGE_OT_add_repository,
    PACKAGE_OT_remove_repository,
    PACKAGE_OT_edit_repositories,
    PACKAGE_UL_repositories,
)