This repository has been archived on 2023-02-07. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-package-manager-addon/bpkg_manager/__init__.py
Sybren A. Stüvel 337a5f352c Added bpkg_manager package that can download packages in a subprocess
Also contains a SubprocMixin mix-in class that can help to write
operators that run & monitor subprocesses.

Messages sent back & forth between Blender and the subprocess MUST subclass
either BlenderMessage or SubprocMessage.
2017-07-10 01:44:54 +02:00

269 lines
8.3 KiB
Python

"""
Blender Package manager
"""
bl_info = {
'name': 'Package Manager',
'author': 'Sybren A. Stüvel',
'version': (0, 1, 0),
'blender': (2, 79, 0),
'location': 'Addon Preferences panel',
'description': 'Add-on package manager.',
'category': 'System',
}
import logging
if 'bpy' in locals():
import importlib
subproc = importlib.reload(subproc)
else:
from . import subproc
import bpy
class SubprocMixin:
"""Mix-in class for things that need to be run in a subprocess."""
log = logging.getLogger(__name__ + '.SubprocMixin')
_state = 'INITIALIZING'
_abort_timeout = 0 # time at which we stop waiting for an abort response and just terminate the process
# Mapping from message type (see bpkg_manager.subproc) 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):
import multiprocessing
self.log.info('Starting')
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.
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 == 'ESC':
self.log.warning('Escape pressed, sending abort signal to subprocess')
self.abort()
return {'PASS_THROUGH'}
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() + 10
self._state = 'ABORTING'
self.pipe_blender.send(subproc.Abort())
def _finish(self, context):
import multiprocessing
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=10)
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.info('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 BPKG_OT_install(SubprocMixin, bpy.types.Operator):
bl_idname = 'bpkg.install'
bl_label = 'Install package'
bl_description = 'Downloads and installs a Blender add-on package'
bl_options = {'REGISTER'}
package_url = bpy.props.StringProperty(name='package_url', description='The URL of the file to download')
log = logging.getLogger(__name__ + '.BPKG_OT_install')
def invoke(self, context, event):
if not self.package_url:
self.report({'ERROR'}, 'Package URL 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
"""
import multiprocessing
self.msg_handlers = {
subproc.Progress: self._subproc_progress,
subproc.DownloadError: self._subproc_download_error,
subproc.Success: self._subproc_success,
subproc.Aborted: self._subproc_aborted,
}
proc = multiprocessing.Process(target=subproc.download_and_install,
args=(self.pipe_subproc, self.package_url,))
return proc
def _subproc_progress(self, progress: subproc.Progress):
self.log.info('Task progress at %i%%', progress.progress * 100)
def _subproc_download_error(self, error: subproc.DownloadError):
self.report({'ERROR'}, 'Unable to download package: %s' % error.description)
self.quit()
def _subproc_success(self, success: subproc.Success):
self.report({'INFO'}, 'Package downloaded successfully')
self.quit()
def _subproc_aborted(self, aborted: subproc.Aborted):
self.report({'ERROR'}, 'Package download 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 BPKG_OT_hang(SubprocMixin, bpy.types.Operator):
bl_idname = 'bpkg.hang'
bl_label = 'Hang (debug)'
bl_description = 'Starts a process that hangs for an hour, for debugging purposes'
bl_options = {'REGISTER'}
log = logging.getLogger(__name__ + '.BPKG_OT_install')
def create_subprocess(self):
"""Starts the download process.
Also registers the message handlers.
:rtype: multiprocessing.Process
"""
import multiprocessing
proc = multiprocessing.Process(target=subproc.debug_hang)
return proc
def report_process_died(self):
self.report({'ERROR'}, 'Process died, exit code %s' % self.process.exitcode)
class PackageManagerPreferences(bpy.types.AddonPreferences):
bl_idname = __package__
package_url = bpy.props.StringProperty(
name='Package URL',
description='Just a temporary place to store the URL of a package to download')
def draw(self, context):
layout = self.layout
temp_box = layout.box()
temp_box.label(text="Temporary stuff while we're developing")
temp_box.prop(self, 'package_url')
temp_box.operator(BPKG_OT_install.bl_idname).package_url = self.package_url
temp_box.operator(BPKG_OT_hang.bl_idname)
def register():
bpy.utils.register_class(BPKG_OT_install)
bpy.utils.register_class(BPKG_OT_hang)
bpy.utils.register_class(PackageManagerPreferences)
def unregister():
bpy.utils.unregister_class(BPKG_OT_install)
bpy.utils.unregister_class(BPKG_OT_hang)
bpy.utils.unregister_class(PackageManagerPreferences)