Some odd tweaks and repo management code

This commit is contained in:
Ellwood Zwovic
2017-07-18 15:10:11 -07:00
parent c0d2419bea
commit 6193437636
2 changed files with 118 additions and 21 deletions

View File

@@ -178,8 +178,6 @@ class Repository:
if url is None:
url = ""
self.set_from_dict({'url': url})
self.log.debug("Initializing repository: %s", self.to_dict())
self.log.debug("Own URL is '%s'", self.url)
# def cleanse_packagelist(self):
# """Remove empty packages (no bl_info), packages with no name"""
@@ -255,7 +253,6 @@ class Repository:
if ids:
for pkg in packages:
self.log.debug(pkg['url'], pkg['bl_info']['name'], self.name, self.url)
# hash may be too big for a C int
pkg['id'] = str(hash(pkg['url'] + pkg['bl_info']['name'] + self.name + self.url))
@@ -389,6 +386,22 @@ def _download(pipe_to_blender, package_url: str, download_dir: pathlib.Path) ->
return local_fpath
def _add_to_installed(storage_path: pathlib.Path, pkg: Package):
"""Add pkg to local repository"""
repo_path = storage_path / 'local.json'
if repo_path.exists():
repo = Repository.from_file(repo_path)
else:
repo = Repository()
repo.packages.append(pkg)
repo.to_file(repo_path)
def _remove_from_installed(storage_path: pathlib.Path, pkg: Package):
"""Remove pkg from local repository"""
repo = Repository.from_file(storage_path / 'local.json')
#TODO: this won't work, compare by name? (watch out for conflicts though)
repo.packages.remove(pkg)
def _install(pipe_to_blender, pkgpath: pathlib.Path, dest: pathlib.Path, searchpaths: list):
"""Extracts/moves package at `pkgpath` to `dest`"""
import zipfile
@@ -466,7 +479,7 @@ def _install(pipe_to_blender, pkgpath: pathlib.Path, dest: pathlib.Path, searchp
return
def download_and_install(pipe_to_blender, package_url: str, install_path: pathlib.Path, search_paths: list):
def download_and_install(pipe_to_blender, package: dict, install_path: pathlib.Path, repo_path: pathlib.Path, search_paths: list):
"""Downloads and installs the given package."""
from . import cache
@@ -474,16 +487,15 @@ def download_and_install(pipe_to_blender, package_url: str, install_path: pathli
log = logging.getLogger('%s.download_and_install' % __name__)
cache_dir = cache.cache_directory('downloads')
downloaded = _download(pipe_to_blender, package_url, cache_dir)
downloaded = _download(pipe_to_blender, package.url, cache_dir)
if not downloaded:
log.debug('Download failed/aborted, not going to install anything.')
return
# Only send success if _install doesn't throw an exception
# Maybe not the best way to do this? (will also catch exceptions which occur during message sending)
try:
_install(pipe_to_blender, downloaded, install_path, search_paths)
_add_to_installed(repo_path, Package.from_dict(package))
pipe_to_blender.send(Success())
except InstallException as err:
log.exception("Failed to install package: %s", err)
@@ -526,9 +538,13 @@ def load(pipe_to_blender, storage_path: pathlib.Path):
try:
repo = _load_repo(storage_path)
pipe_to_blender.send(RepositoryResult(repo.to_dict(sort=True, ids=True)))
pipe_to_blender.send(Success())
return repo
except BadRepository as err:
pipe_to_blender.send(SubprocError("Failed to read repository: %s" % err))
# def load_local(pipe_to_blender
def debug_hang():
"""Hangs for an hour. For testing purposes only."""