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/subproc.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

135 lines
4.0 KiB
Python

"""
All the stuff that needs to run in a subprocess.
"""
import logging
import pathlib
class Message:
"""Superclass for all message sent over pipes."""
class BlenderMessage(Message):
"""Superclass for all messages sent from Blender to the subprocess."""
class SubprocMessage(Message):
"""Superclass for all messages sent from the subprocess to Blender."""
class Abort(BlenderMessage):
"""Sent when the user requests abortion of a task."""
class Progress(SubprocMessage):
"""Send from subprocess to Blender to report progress.
:ivar progress: the progress percentage, from 0-1.
"""
def __init__(self, progress: float):
self.progress = progress
class DownloadError(SubprocMessage):
"""Sent when there was an error downloading something."""
def __init__(self, status_code: int, description: str):
self.status_code = status_code
self.description = description
class Success(SubprocMessage):
"""Sent when an operation finished sucessfully."""
class Aborted(SubprocMessage):
"""Sent as response to Abort message."""
def _download(pipe_to_blender, package_url: str, download_dir: pathlib.Path) -> pathlib.Path:
"""Downloads the given package
:returns: path to the downloaded file, or None in case of error.
"""
import requests
log = logging.getLogger('%s.download' % __name__)
log.info('Going to download %s to %s', package_url, download_dir)
pipe_to_blender.send(Progress(0.0))
log.info('Downloading %s', package_url)
resp = requests.get(package_url, stream=True, verify=True)
try:
resp.raise_for_status()
except requests.HTTPError as ex:
log.error('Error downloading %s: %s', package_url, ex)
pipe_to_blender.send(DownloadError(resp.status_code, str(ex)))
return None
try:
# Use float so that we can also use infinity
content_length = float(resp.headers['content-length'])
except KeyError:
log.warning('Server did not send content length, cannot report progress.')
content_length = float('inf')
# TODO: get filename from Content-Disposition header, if available.
# TODO: use urllib.parse to parse the URL.
local_filename = package_url.split('/')[-1] or 'download.tmp'
local_fpath = download_dir / local_filename
downloaded_length = 0
with local_fpath.open('wb') as outfile:
for chunk in resp.iter_content(chunk_size=1024 ** 2):
# Handle abort messages from Blender
while pipe_to_blender.poll():
recvd = pipe_to_blender.recv()
if isinstance(recvd, Abort):
log.warning('Aborting download of %s by request', package_url)
pipe_to_blender.send(Aborted())
return None
log.warning('Unknown message %s received, ignoring', recvd)
if not chunk: # filter out keep-alive new chunks
continue
outfile.write(chunk)
downloaded_length += len(chunk)
# TODO: use multiplier for progress, so that we can count up to 70% and
# leave 30% "progress" for installation of the package.
pipe_to_blender.send(Progress(downloaded_length / content_length))
pipe_to_blender.send(Success())
return local_fpath
def download_and_install(pipe_to_blender, package_url: str):
"""Downloads and installs the given package."""
from . import cache
log = logging.getLogger('%s.download_and_install' % __name__)
cache_dir = cache.cache_directory('downloads')
downloaded = _download(pipe_to_blender, package_url, cache_dir)
if not downloaded:
log.debug('Download failed/aborted, not going to install anything.')
return
# TODO: actually install the package
log.warning('Installing is not actually implemented, install %s yourself.', downloaded)
def debug_hang():
"""Hangs for an hour. For testing purposes only."""
import time
time.sleep(3600)