Skip to main content

Python module to run command line programs in a convenient way and retrieve their output after they terminated.

Project description

jk_simpleexec

Introduction

This 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:

How to use this module

Import

To import this module use the following statement:

import jk_simpleexec

Invoking a program: Example

Here 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's subprocess.Popen(..) to run the specified program and receive it's output.

The data returned by invokeCmd(..) is a data container for the result. For simplicity a cmdResult.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 of jk_simpleexec.invokeCmd1(..) named jk_simpleexec.invokeCmd(..). This invokeCmd1(..) was implemented as a step to overcome limitations of the invokeCmd(..) API. In the future please use the more recent version invokeCmd1(..) instead of invokeCmd(..). Likely invokeCmd(..) will be removed in future versions.

API

The invokeCmd1(..) 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.
#
def invokeCmd1(
		cmdPath:str,
		cmdArgs:list,
		dataToPipeAsStdIn:typing.Union[str,bytes,bytearray] = None,
		workingDirectory:str = None,
		stdOutProcessing:TextDataProcessingPolicy = None,
		stdErrProcessing:TextDataProcessingPolicy = None,
	) -> CommandResult

The CommandResult object returned

CommandResult objects 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.

Properties

  • str 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 internal TextData object that manages the STDOUT output.
  • TextData stdErr : Direct access to the internal TextData object that manages the STDERR output.
  • bool isError : Returns True if either the return code is non-zero or STDERR contains some data.

Methods

  • void dump(str prefix = None, callable printFunc = None) : Write debugging data to STDOUT (if printFunc is None, or use the specified callable as a replacement for print(..)).
  • 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. (Requires lxml to 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 Information

This 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:

License

This software is provided under the following license:

  • Apache Software License 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jk_simpleexec-0.2024.8.11.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

jk_simpleexec-0.2024.8.11-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file jk_simpleexec-0.2024.8.11.tar.gz.

File metadata

  • Download URL: jk_simpleexec-0.2024.8.11.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for jk_simpleexec-0.2024.8.11.tar.gz
Algorithm Hash digest
SHA256 a072eb4ec21ae8618aec8d7cb0c5f578876fc4fb0c831294d1d35a88e1abdd7d
MD5 94250c96af72d9cb58897491e8a1440f
BLAKE2b-256 353c638d9b420055d4e7966929e84651b86b4d11a30cadd4406141a8c3960343

See more details on using hashes here.

File details

Details for the file jk_simpleexec-0.2024.8.11-py3-none-any.whl.

File metadata

File hashes

Hashes for jk_simpleexec-0.2024.8.11-py3-none-any.whl
Algorithm Hash digest
SHA256 4ec3f33f19cf826ebd6a78446d66c8ff7896ec94e4547f21fa0cd7ba9ed02563
MD5 15f24707809aff364576a91f32997674
BLAKE2b-256 a4f42839695007f67f74a55ca9fb1b891d28464cdc75481049568a5230397ad7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page