Package: jk-simpleexec
jk_simpleexecIntroductionThis python module provides a convenient interface to execute commands and catch their output. Additionally it provides convenient ways of running and killing other processes.Information about this module can be found here:github.orgpypi.python.orgHow to use this moduleImportTo import this module use the following statement:importjk_simpleexecInvoking a program: ExampleHere is an example how to invoke a command:cmdResult=jk_simpleexec.invokeCmd1(cmdPath="/usr/bin/ls",cmdArgs=["-la",],dataToPipeAsStdIn=None,# this is the default; listed here only for completeness;workingDirectory="/",)cmdResult.dump()This will internally use Python'ssubprocess.Popen(..)to run the specified program and receive it's output.The data returned byinvokeCmd(..)is a data container for the result.
For simplicity acmdResult.dump()is invoked here in order to write all received information to STDOUT.
(In a real world scenario you will likely want to process some of that data.)NOTE: There exists an older version ofjk_simpleexec.invokeCmd1(..)namedjk_simpleexec.invokeCmd(..). ThisinvokeCmd1(..)was implemented as a step to
overcome limitations of theinvokeCmd(..)API. In the future please use the more recent versioninvokeCmd1(..)instead ofinvokeCmd(..).
LikelyinvokeCmd(..)will be removed in future versions.APITheinvokeCmd1(..)function## Synchroneously invokes the specified command on the local machine. Output of STDOUT and STDERR is collected and returned by the <c>CommandResult</c> return object.## @param		string cmdPath								(required) The (absolute) path to the program to invoke.# @param		string[] cmdArgs							(required) A list of arguments. Specify <c>None</c> if you do not want to have any arguments.#															Please note that there is no shell to interprete these commands.# @param		str|bytes[] dataToPipeAsStdIn				(optional) Either a string or binary data (or None) that should be passed on to the application invoked usint STDIN.#															If string data is presented it is automatically encoded using UTF-8# @param		str workingDirectory						(optional) If you specify a working directory here this function will change to this working directory#															specified in <c>workingDirector</c> and return to the previous one after the command has been completed.# @param		TextDataProcessingPolicy stdOutProcessing	(optional) If specified you can override defaults of the STDOUT preprocessing that can already be done by this function.# @param		TextDataProcessingPolicy stdErrProcessing	(optional) If specified you can override defaults of the STDERR preprocessing that can already be done by this function.## @return		CommandOutput								Returns an object that contains the exit status, (preprocessed) STDOUT and (preprocessed) STDERR data.#definvokeCmd1(cmdPath:str,cmdArgs:list,dataToPipeAsStdIn:typing.Union[str,bytes,bytearray]=None,workingDirectory:str=None,stdOutProcessing:TextDataProcessingPolicy=None,stdErrProcessing:TextDataProcessingPolicy=None,)->CommandResultTheCommandResultobject returnedCommandResultobjects are returned if commands have been executed successfully. Classes of that type will provide a set of properties and methods.NOTE: I use signatures similiar to C-style here as this way the required types for arguments can be understood more easily.Propertiesstr commandPath: Returns the path used for invoking the command.str commandArguments: Returns the arguments used for invoking the command.int returnCode: The return code of the command after completion.list stdOutLines: The STDOUT output of the command as a list of text lines.list stdErrLines: The STDERR output of the command as a list of text lines.str stdOutStr: The STDOUT output of the command as a single string.str stdErrStr: The STDERR output of the command as a single string.TextData stdOut: Direct access to the internalTextDataobject that manages the STDOUT output.TextData stdErr: Direct access to the internalTextDataobject that manages the STDERR output.bool isError: ReturnsTrueif either the return code is non-zero orSTDERRcontains some data.Methodsvoid dump(str prefix = None, callable printFunc = None): Write debugging data to STDOUT (ifprintFuncisNone, or use the specified callable as a replacement forprint(..)).dict getStdOutAsJSON(): Interpret the text data as JSON data and return it.ElementTree getStdOutAsXML(): Interpret the text data as XML and return an ElemenTree object.* getStdOutAsLXML(): Interpret the text data as an LXML tree object and return it. (Requireslxmlto be installed.)void raiseExceptionOnError(exceptionMessage:str, bDumpStatusOnError:bool = False): If the return code is no-zero or STDERR contains data an exception is thrown using the specified exception message.dict toJSON(): Convert the whole object to a JSON dictionary.Contact InformationThis is Open Source code. That not only gives you the possibility of freely using this code it also
allows you to contribute. Feel free to contact the author(s) of this software listed below, either
for comments, collaboration requests, suggestions for improvement or reporting bugs:Jürgen Knauth:pubsrc@binary-overflow.deLicenseThis software is provided under the following license:Apache Software License 2.0