COT.helpers.helper module

Interface for providers of non-Python helper programs.

Provides the ability to install the program if not already present, and the ability to run the program as well.

exception HelperError[source]

Bases: exceptions.EnvironmentError

A helper program exited with non-zero return code.

exception HelperNotFoundError[source]

Bases: exceptions.OSError

A helper program cannot be located.

class Helper(name, version_args=None, version_regexp='([0-9.]+')[source]

Bases: object

A provider of a non-Python helper program.

Class Properties

PACKAGE_MANAGERS Class-level lookup for package manager executables.

Class Methods

apt_install Try to use apt-get to install a package.
port_install Try to use port to install a package.
yum_install Try to use yum to install a package.
download_and_expand Context manager for downloading and expanding a .tar.gz file.
find_executable Wrapper for distutils.spawn.find_executable().

Instance Properties

name Name of the helper program.
path Discovered path to the helper.
version Release version of the associated helper program.

Instance Methods

call_helper Call the helper program with the given arguments.
install_helper Install the helper program (abstract method).
__init__(name, version_args=None, version_regexp='([0-9.]+')[source]

Initializer.

Parameters:
  • name – Name of helper executable
  • version_args (list) – Args to pass to the helper to get its version. Defaults to ['--version'] if unset.
  • version_regexp – Regexp to get the version number from the output of the command.
classmethod apt_install(package)[source]

Try to use apt-get to install a package.

call_helper(args, capture_output=True, require_success=True)[source]

Call the helper program with the given arguments.

Parameters:
  • args (list) – List of arguments to the helper program.
  • capture_output (boolean) – If True, stdout/stderr will be redirected to a buffer and returned, instead of being displayed to the user.
  • require_success (boolean) – if True, an exception will be raised if the helper exits with a non-zero status code.
Returns:

Captured stdout/stderr (if capture_output), else None.

classmethod download_and_expand(*args, **kwds)[source]

Context manager for downloading and expanding a .tar.gz file.

Creates a temporary directory, downloads the specified URL into the directory, unzips and untars the file into this directory, then yields to the given block. When the block exits, the temporary directory and its contents are deleted.

with self.download_and_expand("http://example.com/foo.tgz") as d:
  # archive contents have been extracted to 'd'
  ...
# d is automatically cleaned up.
Parameters:url (str) – URL of a .tgz or .tar.gz file to download.
classmethod find_executable(name)[source]

Wrapper for distutils.spawn.find_executable().

install_helper()[source]

Install the helper program (abstract method).

Raise:NotImplementedError as this method must be implemented by a concrete subclass.
classmethod port_install(package)[source]

Try to use port to install a package.

classmethod yum_install(package)[source]

Try to use yum to install a package.

PACKAGE_MANAGERS = {'apt-get': '/usr/bin/apt-get', 'yum': None, 'port': None}

Class-level lookup for package manager executables.

name

Name of the helper program.

path

Discovered path to the helper.

version

Release version of the associated helper program.