gbp :: command_wrappers :: Command :: Class Command
[hide private]
[frames] | no frames]

Class Command

object --+
         |
        Command
Known Subclasses:

Wraps a shell command, so we don't have to store any kind of command line options in one of the git-buildpackage commands

Instance Methods [hide private]
 
__init__(self, cmd, args=[], shell=False, extra_env=None, cwd=None, capture_stderr=False, capture_stdout=False)
x.__init__(...) initializes x; see help(type(x)) for signature
 
_reset_state(self)
 
__call(self, args)
Wraps subprocess.call so we can be verbose and fix Python's SIGPIPE handling
 
_log_err(self)
Log an error message
 
_format_err(self)
Log an error message
 
__call__(self, args=[], quiet=False)
Run the command and raise exception on errors
int
>>> Command("/bin/true").call(["foo", "bar"])
0
>>> Command("/foo/bar").call(["foo", "bar"]) # doctest:+ELLIPSIS
Traceback (most recent call last):
...
CommandExecFailed: execution failed: ...
>>> c = Command("/bin/true", capture_stdout=True,
...             extra_env={'LC_ALL': 'C'})
>>> c.call(["--version"])
0
>>> c.stdout.decode('utf-8').startswith('true')
True
>>> c = Command("/bin/false", capture_stdout=True,
...             extra_env={'LC_ALL': 'C'})
>>> c.call(["--help"])
1
>>> c.stdout.decode('utf-8').startswith('Usage:')
True
call(self, args, quiet=True)
Like __call__ but let the caller handle the return status.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, cmd, args=[], shell=False, extra_env=None, cwd=None, capture_stderr=False, capture_stdout=False)
(Constructor)

 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)

_format_err(self)

 

Log an error message

This allows to replace stdout, stderr and err_reason in the self.run_error.

__call__(self, args=[], quiet=False)
(Call operator)

 

Run the command and raise exception on errors

If run quietly it will not print an error message via the gbp.log logging API.

Whether the command prints anything to stdout/stderr depends on the capture_stderr, capture_stdout instance variables.

All errors will be reported as subclass of the CommandExecFailed exception including a non zero exit status of the run command.

Parameters:
  • args (list of strings) - additional command line arguments
  • quiet (bool
    >>> Command("/bin/true")(["foo", "bar"])
    >>> Command("/foo/bar")(quiet=True)
    Traceback (most recent call last):
    ...
    CommandExecFailed: '/foo/bar' failed: execution failed: [Errno 2] No such file or directory
    ) - don't log failed execution to stderr. Mostly useful during unit testing

call(self, args, quiet=True)

 

Like __call__ but let the caller handle the return status.

Only raise CommandExecFailed if we failed to launch the command at all (i.e. if it does not exist) not if the command returned nonzero.

Logs errors using gbp.log by default.

Parameters:
  • args (list of strings) - additional command line arguments
  • quiet (bool) - don't log failed execution to stderr. Mostly useful during unit testing
Returns: int
>>> Command("/bin/true").call(["foo", "bar"])
0
>>> Command("/foo/bar").call(["foo", "bar"]) # doctest:+ELLIPSIS
Traceback (most recent call last):
...
CommandExecFailed: execution failed: ...
>>> c = Command("/bin/true", capture_stdout=True,
...             extra_env={'LC_ALL': 'C'})
>>> c.call(["--version"])
0
>>> c.stdout.decode('utf-8').startswith('true')
True
>>> c = Command("/bin/false", capture_stdout=True,
...             extra_env={'LC_ALL': 'C'})
>>> c.call(["--help"])
1
>>> c.stdout.decode('utf-8').startswith('Usage:')
True
the exit status of the run command