Cleanup: Move package download/install code out of subproc.py

Instead do such things in bpkg, and only handle interfacing between
blender and bpkg in subproc.py
This commit is contained in:
Ellwood Zwovic
2017-07-26 02:08:01 -07:00
parent 85a61cfead
commit 2f6357e40e
6 changed files with 296 additions and 382 deletions

View File

@@ -7,5 +7,5 @@ class InstallException(BpkgException):
class DownloadException(BpkgException):
"""Raised when there is an error downloading something"""
class BadRepository(BpkgException):
"""Raised when reading a repository results in an error"""
class BadRepositoryException(BpkgException):
"""Raised when there is an error while reading or manipulating a repository"""

View File

@@ -42,7 +42,7 @@ class Package:
if package_dict.get(attr) is not None:
setattr(self, attr, package_dict[attr])
# bl_info convenience getters
# bl_info convenience getters {{{
# required fields
@property
def name(self) -> str:
@@ -132,6 +132,7 @@ class Package:
return self.bl_info['tracker_url']
except KeyError:
return None
# }}}
# @classmethod
# def from_dict(cls, package_dict: dict):
@@ -168,6 +169,27 @@ class Package:
raise BadAddon("Module does not appear to be an addon; no bl_info attribute") from err
return pkg
def download(self, dest: Path, progress_callback=None) -> Path:
"""Downloads package to `dest`"""
if not self.url:
raise ValueError("Cannot download package without a URL")
return utils.download(self.url, dest, progress_callback)
def install(self, dest_dir: Path, cache_dir: Path, progress_callback=None):
"""Downloads package to `cache_dir`, then extracts/moves package to `dest_dir`"""
log = logging.getLogger('%s.install' % __name__)
downloaded = self.download(cache_dir, progress_callback)
if not downloaded:
log.debug('Download returned None, not going to install anything.')
return
utils.install(downloaded, dest_dir)
def __repr__(self) -> str:
# return self.name
return "Package('name': {}, 'version': {})".format(self.name, self.version)
@@ -241,7 +263,9 @@ class Repository:
repodict = resp.json()
except json.decoder.JSONDecodeError:
self.log.exception("Failed to parse downloaded repository")
raise exceptions.DownloadException("Could not parse repository downloaded from '%s'. Are you sure this is the correct URL?" % self.url)
raise exceptions.BadRepositoryException(
"Could not parse repository downloaded from '%s'. Are you sure this is the correct URL?" % self.url
)
repodict['_headers'] = resp_headers
self.set_from_dict(repodict)

View File

@@ -1,7 +1,77 @@
from pathlib import Path
from . import exceptions
import shutil
import logging
def download(url: str, destination: Path, progress_callback=None) -> Path:
"""
Downloads file at the given url, and if progress_callback is specified,
repeatedly calls progress_callback with an argument between 0 and 1, or infinity.
Raises DownloadException if an error occurs with the download.
:returns: path to the downloaded file, or None if not modified
"""
import requests
log = logging.getLogger('%s.download' % __name__)
if progress_callback is None:
# assing to do nothing function
progress_callback = lambda x: None
progress_callback(0)
# derive filename from url if `destination` is an existing directory, otherwise use `destination` directly
if destination.is_dir():
# TODO: get filename from Content-Disposition header, if available.
from urllib.parse import urlsplit, urlunsplit
parsed_url = urlsplit(url)
local_filename = Path(parsed_url.path).name or 'download.tmp'
local_fpath = destination / local_filename
else:
local_fpath = destination
log.info('Downloading %s -> %s', url, local_fpath)
try:
resp = requests.get(url, stream=True, verify=True)
except requests.exceptions.RequestException as err:
raise exceptions.DownloadException(err) from err
try:
resp.raise_for_status()
except requests.HTTPError as err:
raise exceptions.DownloadException(resp.status_code, str(err)) from err
if resp.status_code == requests.codes.not_modified:
log.info("Server responded 'Not Modified', not downloading")
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: check if there's enough disk space.
downloaded_length = 0
with local_fpath.open('wb') as outfile:
for chunk in resp.iter_content(chunk_size=1024 ** 2):
if not chunk: # filter out keep-alive new chunks
continue
outfile.write(chunk)
downloaded_length += len(chunk)
progress_callback(downloaded_length / content_length)
return local_fpath
def rm(path: Path):
"""Delete whatever is specified by `path`"""
if path.is_dir():
@@ -50,3 +120,70 @@ class InplaceBackup:
"""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")
if not src_file.is_file():
raise exceptions.InstallException("Package isn't a file")
if not dest_dir.is_dir():
raise exceptions.InstallException("Destination is not a directory")
# TODO: check to make sure addon/package isn't already installed elsewhere
# The following is adapted from `addon_install` in bl_operators/wm.py
# check to see if the file is in compressed format (.zip)
if zipfile.is_zipfile(str(src_file)):
log.debug("Package is zipfile")
try:
file_to_extract = zipfile.ZipFile(str(src_file), 'r')
except Exception as err:
raise exceptions.InstallException("Failed to read zip file: %s" % err) from err
def root_files(filelist: list) -> list:
"""Some string parsing to get a list of the root contents of a zip from its namelist"""
rootlist = []
for f in filelist:
# Get all names which have no path separators (root level files)
# or have a single path separator at the end (root level directories).
if len(f.rstrip('/').split('/')) == 1:
rootlist.append(f)
return rootlist
conflicts = [dest_dir / f for f in root_files(file_to_extract.namelist()) if (dest_dir / f).exists()]
backups = []
for conflict in conflicts:
log.debug("Creating backup of conflict %s", conflict)
backups.append(InplaceBackup(conflict))
try:
file_to_extract.extractall(str(dest_dir))
except Exception as err:
for backup in backups:
backup.restore()
raise exceptions.InstallException("Failed to extract zip file to '%s': %s" % (dest_dir, err)) from err
for backup in backups:
backup.remove()
else:
log.debug("Package is pyfile")
dest_file = (dest_dir / src_file.name)
if dest_file.exists():
backup = InplaceBackup(dest_file)
try:
shutil.copyfile(str(src_file), str(dest_file))
except Exception as err:
backup.restore()
raise exceptions.InstallException("Failed to copy file to '%s': %s" % (dest_dir, err)) from err
log.debug("Installation succeeded")