From 4390523b7f854cefcb91843e1df3ca7575d43fea Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Sun, 21 Dec 2008 17:12:00 -0700 Subject: Improved Plugin.call() method and added its unit test --- ipalib/errors.py | 8 ++++++++ ipalib/plugable.py | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'ipalib') diff --git a/ipalib/errors.py b/ipalib/errors.py index 6dd6eb01..7191ff40 100644 --- a/ipalib/errors.py +++ b/ipalib/errors.py @@ -124,6 +124,14 @@ def _(text): return text +class SubprocessError(StandardError): + def __init__(self, returncode, argv): + self.returncode = returncode + self.argv = argv + StandardError.__init__(self, + 'return code %d from %r' % (returncode, argv) + ) + class HandledError(StandardError): """ Base class for errors that can be raised across a remote procedure call. diff --git a/ipalib/plugable.py b/ipalib/plugable.py index e6b5c1ac..f3b35d30 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -367,15 +367,22 @@ class Plugin(ReadOnly): assert not hasattr(self, name) setattr(self, name, getattr(api, name)) - def call(self, *args): + def call(self, executable, *args): """ - Call an external command via ``subprocess.call``. + Call ``executable`` with ``args`` using subprocess.call(). - Returns the exit status of the call. + If the call exits with a non-zero exit status, + `ipalib.errors.SubprocessError` is raised, from which you can retrieve + the exit code by checking the SubprocessError.returncode attribute. + + This method does *not* return what ``executable`` sent to stdout... for + that, use `Plugin.callread()`. """ - if hasattr(self, 'log'): - self.log.debug('Calling %r', args) - return subprocess.call(args) + argv = (executable,) + args + self.debug('Calling %r', argv) + returncode = subprocess.call(argv) + if returncode != 0: + raise errors.SubprocessError(returncode, argv) def __repr__(self): """ -- cgit