""" All the stuff that needs to run in a subprocess. """ from pathlib import Path from . import utils from . import bpkg from . import messages from .bpkg import exceptions as bpkg_exs import logging def download_and_install_package(pipe_to_blender, package: bpkg.Package, install_path: Path): """Downloads and installs the given package.""" log = logging.getLogger(__name__ + '.download_and_install') from . import cache cache_dir = cache.cache_directory('downloads') try: package.install(install_path, cache_dir) except bpkg_exs.DownloadException as err: pipe_to_blender.send(messages.DownloadError(err)) raise except bpkg_exs.InstallException as err: pipe_to_blender.send(messages.InstallError(err)) raise pipe_to_blender.send(Success()) def uninstall_package(pipe_to_blender, package: bpkg.Package, install_path: Path): """Deletes the given package's files from the install directory""" #TODO: move package to cache and present an "undo" button to user, to give nicer UX on misclicks for pkgfile in [install_path / Path(p) for p in package.files]: if not pkgfile.exists(): pipe_to_blender.send(messages.UninstallError("Could not find file owned by package: '%s'. Refusing to uninstall." % pkgfile)) return None for pkgfile in [install_path / Path(p) for p in package.files]: bpkg.utils.rm(pkgfile) pipe_to_blender.send(Success()) def refresh_repository(pipe_to_blender, repo_storage_path: Path, repository_url: str): """Retrieves and stores the given repository""" log = logging.getLogger(__name__ + '.refresh') repository_url = utils.add_repojson_to_url(repository_url) repo_path = repo_storage_path / 'repo.json' if repo_path.exists(): repo = bpkg.Repository.from_file(repo_path) if repo.url != repository_url: # We're getting a new repository repo = bpkg.Repository(repository_url) else: repo = bpkg.Repository(repository_url) try: repo.refresh() except bpkg_exs.DownloadException as err: pipe_to_blender.send(messages.DownloadError(err)) raise except bpkg_exs.BadRepositoryException as err: pipe_to_blender.send(messages.BadRepositoryError(err)) raise repo.to_file(repo_path) # TODO: this always writes even if repo wasn't changed pipe_to_blender.send(messages.RepositoryResult(repo)) pipe_to_blender.send(messages.Success())