Home | Trees | Indices | Help |
|
---|
|
All calls of external programs should be done via this class or subclasses.
Executor gets the necessary information about a program (binary, environment variables, etc) from ExeConfigCache, creates an input file or pipe from a template (if available) or an existing file, wrapps the program call into ssh and nice (if necessary), spawns an external process via subprocess.Popen, communicates the input file or string, waits for completion and collects the output file or string, and cleans up temporary files.Use Executor directly. An example is given in the __main__ section of this module. You first have to create an Executor instance with all the parameters, then call its run() method and collect the result.
In the most simple cases this can be combined into one line:>>> out, error, returncode = Executor('ls', strict=0).run()strict=0 means, ExeConfig does not insist on an existing exe_ls.dat file and instead looks for a program called 'ls' in the search path.
file_in=%(f_in)s file_out=%(f_out)sAt run time, Executor will create an input file or pipe from the template by replacing all place holders with values from its own fields. Let's assume, the above example is put into a file 'in.template'.
>>> x = Executor( 'ls', template='in.template', f_in='in.dat')... will then pass the following input to the ls program:
file_in=in.dat file_out=/tmp/tmp1HYOvOHowever, the following input template will raise an error:
file_in=%(f_in)s seed=%(seed)i...because Executor doesn't have a 'seed' field. You could provide one by overwriting Executor.__init__. Alternatively, you can provide seed as a keyword to the original Executor.__init__:
>>> x = Executor('ls', template='in.template',f_in='in.dat', seed=1.5)This works because Executor.__init__ puts all unknown key=value pairs into the object's name space and passes them on to the template.
Programs often expect scripts, commands or additional parameters from StdIn or from input files. Executor tries to support many scenarios -- which one is chosen mainly depends on the ExeConfig `pipes` setting in exe_<program>.dat and on the `template` parameter given to Executor.__init__. (Note: Executor loads the ExeConfig instance for the given program `name` into its `self.exe` field.)
Here is an overview over the different scenarios and how to activate them:no input (default behaviour)
The program only needs command line parameters
Condition:input pipe from STDIN (== ``myprogram | 'some input string'``)
Condition:`template` points to an existing file:
Executor reads the template file, completes it in memory, and pushes it directly to the program.`template` points to string that doesn't look like a file name:
Executor completes the string in memory (using `self.template % self.__dict__`) and pushes it directly to the program. This is the fastest option as it avoids file access alltogether.`template` == None but f_in points to an *existing* file:
Executor will read this file and push it unmodified to the program via StdIn. (kind of an exception, if used at all, f_in usual points to a *non-existing* file that will receive the completed input.)input from file (== ``myprogram < input_file``)
Condition:`template` points to an existing file:
Executor reads the template file, completes it in memory, saves the completed file to disc (creating or overriding self.f_in), opens the file and passes the file handle to the program (instead of STDIN).`template` points to string that doesn't look like a file name:
Same as 3.1, except that the template is not read from disc but directly taken from memory (see 2.2).input from file passed as argument to the program (== ``myprogram input_file``)
Condition:For this it is up to you to provide the correct program argument.
Setup:Use template completion:
The best option would be to set an explicit file name for `f_in` and include this file name into `args`, Example:exe = ExeConfigCache.get('myprogram') assert not exe.pipes x = Executor( 'myprogram', args='input.in', f_in='input.in', template='/somewhere/input.template', cwd='/tmp' )Executor create your input file on the fly which is then passed as first argument.
Without template completion:
Similar, just that you don't give a template:x = Executor( 'myprogram', args='input.in', f_in='input.in', cwd='/tmp' )It would then be up to you to provide the correct input file in `/tmp/input.in`. You could override the prepare() hook method for creating it.
|
|||
|
__init__(self,
name,
args='',
template=None,
f_in=None,
f_out=None,
f_err=None,
strict=1,
catch_out=1,
push_inp=1,
catch_err=0,
node=None,
nice=0,
cwd=None,
log=None,
debug=0,
verbose=None,
**kw) Create Executor. |
||
str
|
version(self) Version of class (at creation). |
||
str, str
|
communicate(self,
cmd,
inp,
bufsize=-1,
executable=None,
stdin=None,
stdout=None,
stderr=None,
shell=0,
env=None,
cwd=None) Start and communicate with the new process. |
||
int
|
execute(self,
inp=None) Run external command and block until it is finished. |
||
any
|
run(self,
inp_mirror=None) Run the callculation. |
||
str
|
command(self) Compose command string from binary, arguments, nice, and node. |
||
dict OR None
|
environment(self) Setup the environment for the process. |
||
|
prepare(self) called before running external program, override! |
||
|
postProcess(self) called directly after running the external program, override! |
||
|
cleanup(self) Clean up after external program has finished (failed or not). |
||
|
fail(self) Called if external program failed, override! |
||
|
finish(self) Called if external program finished successfully, override! |
||
|
isFailed(self) Detect whether external program failed, override! |
||
str
|
fillTemplate(self) Create complete input string from template with place holders. |
||
str
|
convertInput(self,
inp) Convert the input to a format used by the selected execution method. |
||
str
|
generateInp(self) Prepare the program input (file or string) from a template (if present, file or string). |
|
|||
|
f_in will be overridden by self.convertInput() |
||
|
log Log object for own program messages |
||
|
runTime time needed for last run |
||
|
output STDOUT returned by process |
||
|
error STDERR returned by process |
||
|
returncode int status returned by process |
||
|
pid process ID |
||
|
result set by self.finish() |
|
Create Executor. *name* must point to an existing program configuration unless *strict*=0. Executor will create a program input from the template and its own fields and put it into f_in. If f_in but no template is given, the unchanged f_in is used as input. If neither is given, the program is called without input. If a node is given, the process is wrapped in a ssh call. If *nice* != 0, the process is preceeded by nice. *cwd* specifies the working directory. By default, this setting is taken from the configuration file which defaults to the current working directory.
|
Version of class (at creation).
|
Start and communicate with the new process. Called by execute(). See subprocess.Popen() for a detailed description of the parameters! This method should work for pretty much any purpose but may fail for very long pipes (more than 100000 lines).
|
Run external command and block until it is finished. Called by run() .
|
Run the callculation. This calls (in that order):
|
Compose command string from binary, arguments, nice, and node. Override (perhaps).
|
Setup the environment for the process. Override if needed.
|
called before running external program, override! |
called directly after running the external program, override! |
Clean up after external program has finished (failed or not). Override, but call in child method! |
Called if external program failed, override! |
Called if external program finished successfully, override! |
Detect whether external program failed, override! |
Create complete input string from template with place holders.
|
Convert the input to a format used by the selected execution method.
|
Prepare the program input (file or string) from a template (if present, file or string).
|
|
f_inwill be overridden by self.convertInput() |
logLog object for own program messages |
runTimetime needed for last run |
outputSTDOUT returned by process |
errorSTDERR returned by process |
returncodeint status returned by process |
pidprocess ID |
resultset by self.finish() |
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0alpha3 on Tue May 1 22:34:54 2007 | http://epydoc.sourceforge.net |