Module g_exec
source code
Run a Linux command and capture the result. This module is
thread-safe.
Other than the *_raw functions, these functions ignore comments lines
(e.g. ones starting with '#'), and if they see a line beginning with an
error prefix, they raise an ExecError
exception. The default error prefix is ERR, but you can
set it for each call. Likewise, the default comment prefix is COMMENT, but
you can also set it with the comment
argument of any
call.
The basic idea is that
-
you start a subprocess.
-
you feed in a bunch of strings (one at a time) to its standard input.
(These come from the
input
argument. You need to know,
in advance, how much data to feed it.) Note that newlines are not
added to the input
entries, so if you want the strings
to be lines, they ought to end in a newline.
-
then loop over this:
-
if
perline
is not None, it feeds the subprocess's
standard input a string, taken from perline
. (Note that
newlines are not added; you need to include them if you want them,
and you probably want them.)
-
it produces an output line
-
repeat
-
keep reading until the subprocess closes its standard output.
When perline
is empty, the subprocess's standard input is
closed. These functions continue reading the subprocess's output as long
as it keeps producing.
So, you can do this:
get(None, ['pwd']) # "/home/gpk/whatever"
getall(None, ['ls']) # Yields a listing of your directory
get(None, ['sum'], input=['wurgle']) # Same as typing "echo wurgle | sum" to bash.
getall(None, ['cat'], input=['wurgle
'], perline=['biffle ']) == ['wurgle ', 'biffle ']
a generator of strings.
|
getiter_raw(s,
argv,
input=None,
perline=None,
debug=False)
Read a list of lines from a subprocess. |
source code
|
|
a generator of strings.
|
getiter(s,
argv,
input=None,
perline=None,
debug=False,
err=' ERR: ' ,
comment=' # ' )
Read a list of lines from a subprocess, after dropping junk like
comments. |
source code
|
|
str
|
get(s,
argv=None,
input=None,
perline=None,
debug=False,
err=' ERR: ' ,
comment=' # ' )
Read a single line from a subprocess, after dropping junk like
comments. |
source code
|
|
str
|
get_raw(s,
argv=None,
input=None,
perline=False,
debug=False)
Read a single line from a subprocess. |
source code
|
|
str
|
getlast(s,
argv=None,
input=None,
perline=False,
debug=False,
err=' ERR: ' ,
comment=' # ' )
Read the last line from a subprocess, after dropping junk like
comments. |
source code
|
|
list(str)
|
getall(s,
argv,
input=None,
perline=None,
debug=False,
err=' ERR: ' ,
comment=' # ' )
Read the text lines produced by a subprocess, after dropping junk
like comments. |
source code
|
|
|
|
|
ERR = ' ERR: '
This is the default for the prefix for error messages from the
subprocess.
|
|
COMMENT = ' # '
This is the default for the prefix for comments from the subprocess.
|
|
__package__ = ' gmisclib '
|
Imports:
g_pipe
getiter_raw(s,
argv,
input=None,
perline=None,
debug=False)
| source code
|
Read a list of lines from a subprocess.
- Parameters:
s (str or None ) - the name of the program to execute. (Or None , in which case, it is taken from
argv[0] ).
argv (list(str) ) - an array of argument to execute.
input (array, sequence or iterator, containing strings) - strings to feed to the subprocess on startup, before the first
output is read. These are sent to the subprocess's standard
input.
perline (array, sequence or iterator, containing strings) - is a sequence/iterator of strings to feed in, one at a time, as
the subprocess is producing data. (These are fed to the
subprocess's standard input, one after each output line that it
produces.)
debug (bool) - Should it print some debugging information?
- Returns: a generator of strings.
- a sequence of the lines of output that the program produced.
(Newlines are not removed.)
- Raises:
Note:
If input or perline is badly chosen, one can produce a locked loop
of pipes. (Locked loops happen when you have too much stuff
waiting to be processed.)
|
getiter(s,
argv,
input=None,
perline=None,
debug=False,
err=' ERR: ' ,
comment=' # ' )
| source code
|
Read a list of lines from a subprocess, after dropping junk like
comments. (Comment lines begin with the comment argument).
Raises an exception if the subprocess returns an error line (the
beginning of an error line is specified in the err
argument).
- Parameters:
s (str or None ) - the name of the program to execute. (Or None , in which case, it is taken from
argv[0] ).
argv (list(str) ) - an array of argument to execute.
input (array, sequence or iterator, containing strings) - strings to feed to the subprocess on startup, before the first
output is read. These are sent to the subprocess's standard
input.
perline (array, sequence or iterator, containing strings) - is a sequence/iterator of strings to feed in, one at a time, as
the subprocess is producing data. (These are fed to the
subprocess's standard input, one after each output line that it
produces.)
debug (bool) - Should it print some debugging information?
- Returns: a generator of strings.
- a sequence of the lines of output that the program produced.
(Newlines are not removed.)
- Raises:
Note:
If input or perline is badly chosen, one can produce a locked loop
of pipes. (Locked loops happen when you have too much stuff
waiting to be processed.)
|
get(s,
argv=None,
input=None,
perline=None,
debug=False,
err=' ERR: ' ,
comment=' # ' )
| source code
|
Read a single line from a subprocess, after dropping junk like
comments. Raises an exception if the subprocess produces an error line
(see getiter, ERR) or no output. See getiter for
arguments.
- Returns:
str
- the subprocess's first output line (after dropping comment
lines).
- Raises:
ExecError - if the subprocess produces no output, or an error line.
|
get_raw(s,
argv=None,
input=None,
perline=False,
debug=False)
| source code
|
Read a single line from a subprocess. (One normally uses this for
processes that are always supposed to produce a single line of output.)
See getiter_raw for arguments.
- Returns:
str
- the subprocess's first output line.
- Raises:
ExecError - if the subprocess produces no output.
|
getlast(s,
argv=None,
input=None,
perline=False,
debug=False,
err=' ERR: ' ,
comment=' # ' )
| source code
|
Read the last line from a subprocess, after dropping junk like
comments. Raises an exception if the subprocess produces an error line
(see getiter, ERR) or no output. See getiter for
arguments.
- Returns:
str
- the subprocess's first output line (after dropping comment
lines).
- Raises:
ExecError - if the subprocess produces no output, or an error line.
|
getall(s,
argv,
input=None,
perline=None,
debug=False,
err=' ERR: ' ,
comment=' # ' )
| source code
|
Read the text lines produced by a subprocess, after dropping junk like
comments. Raises an exception if the process produces an error line (see
getiter,
ERR). See getiter for
arguments.
- Returns:
list(str)
- the subprocess's first output line (after dropping comment
lines).
- Raises:
ExecError - if the process produces an error line.
|