[Bf-blender-cvs] [339c51ab179] soc-2017-package_manager: Move install/download code out of utils
gandalf3
noreply at git.blender.org
Mon Aug 28 09:47:04 CEST 2017
Commit: 339c51ab17924d54b666fe37c6fc909f0db87e4c
Author: gandalf3
Date: Sun Aug 27 16:26:08 2017 -0700
Branches: soc-2017-package_manager
https://developer.blender.org/rB339c51ab17924d54b666fe37c6fc909f0db87e4c
Move install/download code out of utils
===================================================================
M release/scripts/modules/bpkg/__init__.py
A release/scripts/modules/bpkg/actions.py
M release/scripts/modules/bpkg/subproc.py
M release/scripts/modules/bpkg/types.py
M release/scripts/modules/bpkg/utils.py
M release/scripts/startup/bl_operators/package.py
===================================================================
diff --git a/release/scripts/modules/bpkg/__init__.py b/release/scripts/modules/bpkg/__init__.py
index 89c30a342f7..d61a4627909 100644
--- a/release/scripts/modules/bpkg/__init__.py
+++ b/release/scripts/modules/bpkg/__init__.py
@@ -9,8 +9,8 @@ from pathlib import Path
from collections import OrderedDict
import logging
-tag_reindex = True
-packages = {}
+_tag_reindex = True
+_packages = {}
def get_repo_storage_path() -> Path:
"""Return Path to the directory in which downloaded repository indices are
@@ -27,22 +27,23 @@ def get_repositories() -> list:
repos = utils.load_repositories(storage_path)
return repos
+def get_installed_packages(refresh=False) -> list:
+ """Get list of packages installed on disk"""
+ import addon_utils
+ installed_pkgs = []
+ #TODO: just use addon_utils for now
+ for mod in addon_utils.modules(refresh=refresh):
+ pkg = types.Package.from_module(mod)
+ pkg.installed = True
+ installed_pkgs.append(pkg)
+ return installed_pkgs
+
def _build_packagelist() -> dict: # {{{
"""Return a dict of ConsolidatedPackages from known repositories and
installed packages, keyed by package name"""
log = logging.getLogger(__name__ + "._build_packagelist")
- def get_installed_packages(refresh=False) -> list:
- """Get list of packages installed on disk"""
- import addon_utils
- installed_pkgs = []
- for mod in addon_utils.modules(refresh=refresh):
- pkg = types.Package.from_module(mod)
- pkg.installed = True
- installed_pkgs.append(pkg)
- return installed_pkgs
-
masterlist = {}
installed_packages = get_installed_packages(refresh=True)
known_repositories = get_repositories()
@@ -66,13 +67,18 @@ def _build_packagelist() -> dict: # {{{
return masterlist
# }}}
+def tag_reindex():
+ """Set flag for rebuilding package list"""
+ global _tag_reindex
+ _tag_reindex = True
def list_packages():
"""Return same dict as _build_packagelist, but only re-build it when tag_reindex == True"""
- global packages
- global tag_reindex
- if tag_reindex:
- packages = _build_packagelist()
- tag_reindex = False
+ global _packages
+ global _tag_reindex
+ if _tag_reindex:
+ _packages = _build_packagelist()
+ print("rebuilding")
+ _tag_reindex = False
- return packages
+ return _packages
diff --git a/release/scripts/modules/bpkg/utils.py b/release/scripts/modules/bpkg/actions.py
similarity index 56%
copy from release/scripts/modules/bpkg/utils.py
copy to release/scripts/modules/bpkg/actions.py
index 3b57208f607..15ad2b6fdc1 100644
--- a/release/scripts/modules/bpkg/utils.py
+++ b/release/scripts/modules/bpkg/actions.py
@@ -1,48 +1,9 @@
from pathlib import Path
from . import exceptions
-from .types import (
- Repository,
-)
+from . import utils
import shutil
import logging
-
-def format_filename(s: str, ext=None) -> str:
- """Take a string and turn it into a reasonable filename"""
- import string
- if ext is None:
- ext = ""
- valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
- filename = ''.join(char for char in s if char in valid_chars)
- filename = filename.replace(' ','_')
- filename.lower()
- filename += ext
- return filename
-
-def sanitize_repository_url(url: str) -> str:
- """Sanitize repository url"""
- from urllib.parse import urlsplit, urlunsplit
- parsed_url = urlsplit(url)
- # new_path = parsed_url.path.rstrip("repo.json")
- new_path = parsed_url.path
- return urlunsplit((parsed_url.scheme, parsed_url.netloc, new_path, parsed_url.query, parsed_url.fragment))
-
-def add_repojson_to_url(url: str) -> str:
- """Add `repo.json` to the path component of a url"""
- from urllib.parse import urlsplit, urlunsplit
- parsed_url = urlsplit(url)
- new_path = str(Path(parsed_url.path) / "repo.json")
- return urlunsplit((parsed_url.scheme, parsed_url.netloc, new_path, parsed_url.query, parsed_url.fragment))
-
-def load_repositories(repo_storage_path: Path) -> list:
- repositories = []
- for repofile in repo_storage_path.glob('*.json'):
- # try
- repo = Repository.from_file(repofile)
- # except
- repositories.append(repo)
- return repositories
-
def download(url: str, destination: Path, progress_callback=None) -> Path:
"""
Downloads file at the given url, and if progress_callback is specified,
@@ -109,63 +70,13 @@ def download(url: str, destination: Path, progress_callback=None) -> Path:
return local_fpath
-
-def rm(path: Path):
- """Delete whatever is specified by `path`"""
- if path.is_dir():
- shutil.rmtree(str(path))
- else:
- path.unlink()
-
-class InplaceBackup:
- """Utility class for moving a file out of the way by appending a '~'"""
-
- log = logging.getLogger('%s.inplace-backup' % __name__)
-
- def __init__(self, path: Path):
- self.path = path
- self.backup()
-
- def backup(self):
- """Move 'path' to 'path~'"""
- if not self.path.exists():
- raise FileNotFoundError("Can't backup path which doesn't exist")
-
- self.backup_path = Path(str(self.path) + '~')
- if self.backup_path.exists():
- self.log.warning("Overwriting existing backup '{}'".format(self.backup_path))
- rm(self.backup_path)
-
- shutil.move(str(self.path), str(self.backup_path))
-
- def restore(self):
- """Move 'path~' to 'path'"""
- try:
- getattr(self, 'backup_path')
- except AttributeError as err:
- raise RuntimeError("Can't restore file before backing it up") from err
-
- if not self.backup_path.exists():
- raise FileNotFoundError("Can't restore backup which doesn't exist")
-
- if self.path.exists():
- self.log.warning("Overwriting '{0}' with backup file".format(self.path))
- rm(self.path)
-
- shutil.move(str(self.backup_path), str(self.path))
-
- def remove(self):
- """Remove 'path~'"""
- rm(self.backup_path)
-
-
def install(src_file: Path, dest_dir: Path):
"""Extracts/moves package at `src_file` to `dest_dir`"""
import zipfile
log = logging.getLogger('%s.install' % __name__)
- log.debug("Starting installation")
+ log.error("Starting installation")
if not src_file.is_file():
raise exceptions.InstallException("Package isn't a file")
@@ -199,7 +110,7 @@ def install(src_file: Path, dest_dir: Path):
backups = []
for conflict in conflicts:
log.debug("Creating backup of conflict %s", conflict)
- backups.append(InplaceBackup(conflict))
+ backups.append(utils.InplaceBackup(conflict))
try:
file_to_extract.extractall(str(dest_dir))
@@ -214,9 +125,10 @@ def install(src_file: Path, dest_dir: Path):
else:
log.debug("Package is pyfile")
dest_file = (dest_dir / src_file.name)
+ backup = None
if dest_file.exists():
- backup = InplaceBackup(dest_file)
+ backup = utils.InplaceBackup(dest_file)
try:
shutil.copyfile(str(src_file), str(dest_file))
@@ -224,22 +136,7 @@ def install(src_file: Path, dest_dir: Path):
backup.restore()
raise exceptions.InstallException("Failed to copy file to '%s': %s" % (dest_dir, err)) from err
- log.debug("Installation succeeded")
-
-
-# def load_repository(repo_storage_path: Path, repo_name: str) -> Repository:
-# """Loads <repo_name>.json from <repo_storage_path>"""
-# pass
-#
-# def download_repository(repo_storage_path: Path, repo_name: str):
-# """Loads <repo_name>.json from <repo_storage_path>"""
-# pass
-# this is done in Repository
-
+ if backup:
+ backup.remove()
-def add_repojson_to_url(url: str) -> str:
- """Add `repo.json` to the path component of a url"""
- from urllib.parse import urlsplit, urlunsplit
- parsed_url = urlsplit(url)
- new_path = parsed_url.path + "/repo.json"
- return urlunsplit((parsed_url.scheme, parsed_url.netloc, new_path, parsed_url.query, parsed_url.fragment))
+ log.debug("Installation succeeded")
diff --git a/release/scripts/modules/bpkg/subproc.py b/release/scripts/modules/bpkg/subproc.py
index 44f8e5da750..5c8b9cfb0c8 100644
--- a/release/scripts/modules/bpkg/subproc.py
+++ b/release/scripts/modules/bpkg/subproc.py
@@ -40,13 +40,20 @@ def uninstall_package(pipe_to_blender, package: 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
+ log = logging.getLogger(__name__ + ".uninstall_package")
+
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]:
- utils.rm(pkgfile)
+ try:
+ for pkgfile in [install_path / Path(p) for p in package.files]:
+ utils.rm(pkgfile)
+ except Exception as err:
+ pipe_to_blender.send(messages.UninstallError(err))
+ log.exception(err)
+ return
pipe_to_blender.send(messages.Success())
diff --git a/release/scripts/modules/bpkg/types.py b/release/scripts/modules/bpkg/types.py
index 7ed99b2d997..48a362c5c37 100644
--- a/release/scripts/modules/bpkg/types.py
+++ b/release/scripts/modules/bpkg/types.py
@@ -3,6 +3,7 @@ import json
from pathlib import Path
from . import exceptions
from . import utils
+from . import actions
class Package:
"""
@@ -189,7 +190,7 @@ class Package:
if not self.url:
raise ValueError("Cannot download package without a URL")
- return utils.download(self.url, dest, progress_callback)
+ return actions.download(self.url, dest, progress_callback)
def instal
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list