Skip to content

Commit

Permalink
Improve messages when helpers cannot be installed. Fixes #57
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmatthews committed Jan 18, 2017
1 parent 354a3f0 commit 69b0d1c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ This project adheres to `Semantic Versioning`_.

- Support for Python 3.6

**Fixed**

- Improved messaging when COT is unable to install a helper program (`#57`_).

`1.8.2`_ - 2017-01-18
---------------------

Expand Down Expand Up @@ -601,6 +605,7 @@ Initial public release.
.. _#52: https://github.com/glennmatthews/cot/issues/52
.. _#53: https://github.com/glennmatthews/cot/issues/53
.. _#54: https://github.com/glennmatthews/cot/issues/54
.. _#57: https://github.com/glennmatthews/cot/issues/57
.. _#58: https://github.com/glennmatthews/cot/issues/58
.. _#59: https://github.com/glennmatthews/cot/issues/59

Expand Down
4 changes: 2 additions & 2 deletions COT/helpers/fatdisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# fatdisk.py - Helper for 'fatdisk'
#
# February 2015, Glenn F. Matthews
# Copyright (c) 2013-2016 the COT project developers.
# Copyright (c) 2013-2017 the COT project developers.
# See the COPYRIGHT.txt file at the top-level directory of this distribution
# and at https://github.com/glennmatthews/cot/blob/master/COPYRIGHT.txt.
#
Expand Down Expand Up @@ -54,7 +54,7 @@ def _install(self):
helpers['port'].install_package('fatdisk')
return
elif platform.system() != 'Linux':
self.unsure_how_to_install()
raise self.unsure_how_to_install()

# Fatdisk installation requires make
helpers['make'].install()
Expand Down
49 changes: 36 additions & 13 deletions COT/helpers/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# helper.py - Abstract provider of a non-Python helper program.
#
# February 2015, Glenn F. Matthews
# Copyright (c) 2015-2016 the COT project developers.
# Copyright (c) 2015-2017 the COT project developers.
# See the COPYRIGHT.txt file at the top-level directory of this distribution
# and at https://github.com/glennmatthews/cot/blob/master/COPYRIGHT.txt.
#
Expand Down Expand Up @@ -51,6 +51,7 @@
import os.path
import contextlib
import errno
import platform
import re
import shutil
import subprocess
Expand Down Expand Up @@ -324,7 +325,7 @@ def install(self):
if self.installed:
return
if not self.installable:
self.unsure_how_to_install()
raise self.unsure_how_to_install()
logger.info("Installing '%s'...", self.name)
# Call the subclass implementation
self._install()
Expand All @@ -338,11 +339,27 @@ def install(self):
logger.info("Successfully installed '%s'", self.name)

def unsure_how_to_install(self):
"""Raise a NotImplementedError about missing install logic."""
"""Return a RuntimeError or NotImplementedError for install trouble."""
msg = "Unsure how to install {0}.".format(self.name)
if self.info_uri:
msg += "\nRefer to {0} for information".format(self.info_uri)
raise NotImplementedError(msg)

if platform.system() == 'Darwin' and (
'port' in self._provider_package and not helpers['port']):
msg += ("\nCOT can use MacPorts (https://www.macports.org/),"
" if available on your system, to install {0} and other"
" helpers for you.".format(self.name))
return RuntimeError(msg)
elif platform.system() == 'Linux' and (
('apt-get' in self._provider_package or
'yum' in self._provider_package) and
not (helpers['apt-get'] or helpers['yum'])):
msg += ("\nCOT can use package managers 'yum' or 'apt-get' to"
" install helpers on your system, but it appears that"
" you have neither of these package managers?")
return RuntimeError(msg)
else:
return NotImplementedError(msg)

def _install(self):
"""Subclass-specific implementation of installation logic."""
Expand All @@ -352,7 +369,7 @@ def _install(self):
helpers[pm_name].install_package(package)
return
# We shouldn't get here under normal call flow and logic.
self.unsure_how_to_install()
raise self.unsure_how_to_install()

@staticmethod
@contextlib.contextmanager
Expand Down Expand Up @@ -602,7 +619,7 @@ def helper_select(choices):
Returns:
Helper: The selected helper class instance.
"""
for choice in choices:
def _name_min_ver_from_choice(choice):
if isinstance(choice, str):
# Helper name only, no version constraints
name = choice
Expand All @@ -611,21 +628,27 @@ def helper_select(choices):
# Tuple of (name, version)
(name, vers) = choice
min_version = StrictVersion(vers)

return (name, min_version)

for choice in choices:
name, min_version = _name_min_ver_from_choice(choice)
if helpers[name]:
if min_version is None or helpers[name].version >= min_version:
return helpers[name]

# OK, nothing yet installed. So what can we install?
for choice in choices:
if isinstance(choice, str):
name = choice
min_version = None
else:
(name, vers) = choice
min_version = StrictVersion(vers)
name, min_version = _name_min_ver_from_choice(choice)
if helpers[name].installable:
helpers[name].install()
if min_version is None or helpers[name].version >= min_version:
return helpers[name]

raise HelperNotFoundError("No helper available or installable!")
msg = "No helper in list {0} is available or installable!".format(choices)

for choice in choices:
name, _ = _name_min_ver_from_choice(choice)
msg += "\n" + str(helpers[name].unsure_how_to_install())

raise HelperNotFoundError(msg)
6 changes: 3 additions & 3 deletions COT/helpers/ovftool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# ovftool.py - Helper for 'ovftool'
#
# February 2015, Glenn F. Matthews
# Copyright (c) 2013-2016 the COT project developers.
# Copyright (c) 2013-2017 the COT project developers.
# See the COPYRIGHT.txt file at the top-level directory of this distribution
# and at https://github.com/glennmatthews/cot/blob/master/COPYRIGHT.txt.
#
Expand Down Expand Up @@ -41,12 +41,12 @@ def installable(self):
return False

def unsure_how_to_install(self):
"""Raise a NotImplementedError about missing install logic.
"""Return a NotImplementedError about missing install logic.
We override the default install implementation to raise a more
detailed error message for ovftool.
"""
raise NotImplementedError(
return NotImplementedError(
"No support for automated installation of ovftool, as VMware "
"requires a site login to download it. See "
"https://www.vmware.com/support/developer/ovf/"
Expand Down
4 changes: 2 additions & 2 deletions COT/helpers/vmdktool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# vmdktool.py - Helper for 'vmdktool'
#
# February 2015, Glenn F. Matthews
# Copyright (c) 2013-2016 the COT project developers.
# Copyright (c) 2013-2017 the COT project developers.
# See the COPYRIGHT.txt file at the top-level directory of this distribution
# and at https://github.com/glennmatthews/cot/blob/master/COPYRIGHT.txt.
#
Expand Down Expand Up @@ -54,7 +54,7 @@ def _install(self):
helpers['port'].install_package('vmdktool')
return
elif platform.system() != 'Linux':
self.unsure_how_to_install()
raise self.unsure_how_to_install()

# We don't have vmdktool in apt or yum yet,
# but we can build it manually:
Expand Down

0 comments on commit 69b0d1c

Please sign in to comment.