API Reference — Sarge 0.1.8.dev0 documentation (original) (raw)

This is the place where the functions and classes in sarge's public API are described.

Attributes

default_capture_timeout

This is the default timeout which will be used by Captureinstances when you don’t specify one in the Capture constructor. This is currently set to 0.02 seconds.

Functions

run(command, input=None, async_=False, **kwargs)[source]

This function is a convenience wrapper which constructs aPipeline instance from the passed parameters, and then invokesrun() and close() on that instance.

Parameters:

Returns:

The created Pipeline instance.

Changed in version 0.1.5: The async keyword parameter was changed to async_, as asyncis a keyword in Python 3.7 and later.

capture_stdout(command, input=None, async_=False, **kwargs)[source]

This function is a convenience wrapper which does the same as run()while capturing the stdout of the subprocess(es). This captured output is available through the stdout attribute of the return value from this function.

Parameters:

Returns:

As for run().

Changed in version 0.1.5: The async keyword parameter was changed to async_, as asyncis a keyword in Python 3.7 and later.

get_stdout(command, input=None, async_=False, **kwargs)[source]

This function is a convenience wrapper which does the same ascapture_stdout() but also returns the text captured. Use this when you know the output is not voluminous, so it doesn’t matter that it’s buffered in memory.

Parameters:

Returns:

The captured text.

New in version 0.1.1.

Changed in version 0.1.5: The async keyword parameter was changed to async_, as asyncis a keyword in Python 3.7 and later.

capture_stderr(command, input=None, async_=False, **kwargs)[source]

This function is a convenience wrapper which does the same as run()while capturing the stderr of the subprocess(es). This captured output is available through the stderr attribute of the return value from this function.

Parameters:

Returns:

As for run().

Changed in version 0.1.5: The async keyword parameter was changed to async_, as asyncis a keyword in Python 3.7 and later.

get_stderr(command, input=None, async_=False, **kwargs)[source]

This function is a convenience wrapper which does the same ascapture_stderr() but also returns the text captured. Use this when you know the output is not voluminous, so it doesn’t matter that it’s buffered in memory.

Parameters:

Returns:

The captured text.

New in version 0.1.1.

Changed in version 0.1.5: The async keyword parameter was changed to async_, as asyncis a keyword in Python 3.7 and later.

capture_both(command, input=None, async_=False, **kwargs)[source]

This function is a convenience wrapper which does the same as run()while capturing the stdout and the stderr of the subprocess(es). This captured output is available through the stdout andstderr attributes of the return value from this function.

Parameters:

Returns:

As for run().

Changed in version 0.1.5: The async keyword parameter was changed to async_, as asyncis a keyword in Python 3.7 and later.

get_both(command, input=None, async_=False, **kwargs)[source]

This function is a convenience wrapper which does the same ascapture_both() but also returns the text captured. Use this when you know the output is not voluminous, so it doesn’t matter that it’s buffered in memory.

Parameters:

Returns:

The captured text as a 2-element tuple, with the stdout text in the first element and the stderr text in the second.

New in version 0.1.1.

Changed in version 0.1.5: The async keyword parameter was changed to async_, as asyncis a keyword in Python 3.7 and later.

shell_quote(s)[source]

Quote text so that it is safe for POSIX command shells.

For example, “_.py” would be converted to “’_.py’”. If the text is considered safe it is returned unquoted.

Parameters:

s (str, or unicode on 2.x) – The value to quote

Returns:

A safe version of the input, from the point of view of POSIX command shells

shell_format(fmt, *args, **kwargs)[source]

Format a shell command with format placeholders and variables to fill those placeholders.

Note: you must specify positional parameters explicitly, i.e. as {0}, {1} instead of {}, {}. Requiring the formatter to maintain its own counter can lead to thread safety issues unless a thread local is used to maintain the counter. It’s not that hard to specify the values explicitly yourself :-)

Parameters:

Returns:

The formatted shell command, which should be safe for use in shells from the point of view of shell injection.

Return type:

The type of fmt.

Classes

class Command(args, **kwargs)[source]

This represents a single command to be spawned as a subprocess.

Parameters:

Attributes

process

The subprocess.Popen instance for the subprocess, once it has been created. It is initially None.

returncode

The subprocess returncode, when that is available. It is initially None.

exception

Any exception which occurred when trying to create the subprocess. Note that once a subprocess has been created, any exceptions in the subprocess can only be communicated via the returncode - this value is only for exceptions during subprocess creation.

New in version 0.1.8.

Methods

run(input=None, async_=False)[source]

Run the command.

Parameters:

Changed in version 0.1.5: The async keyword parameter was changed to async_, as asyncis a keyword in Python 3.7 and later.

wait(timeout=None)[source]

Wait for the command’s underlying sub-process to complete, with a specified timeout. If the timeout is reached, a subprocess.TimeoutExpired exception is raised. The timeout is ignored in versions of Python < 3.3.

Changed in version 0.1.6: The timeout parameter was added.

terminate()[source]

Terminate the command’s underlying sub-process by callingsubprocess.Popen.terminate() on it.

New in version 0.1.1.

kill()[source]

Kill the command’s underlying sub-process by callingsubprocess.Popen.kill() on it.

New in version 0.1.1.

poll()[source]

Poll the command’s underlying sub-process by callingsubprocess.Popen.poll() on it. Returns the result of that call.

New in version 0.1.1.

class Pipeline(source, posix=True, **kwargs)[source]

This represents a set of commands which need to be run as a unit.

Parameters:

Attributes

returncodes

A list of the return codes of all sub-processes which were actually run. This will internally poll the commands in the pipeline to find the latest known return codes.

returncode

The return code of the last sub-process which was actually run.

commands

The Command instances which were actually created.

exceptions

A list of any exceptions creating subprocesses. This should be of use in diagnosing problems with commands (e.g. typos, or executables correctly spelled but not found on the system path).

New in version 0.1.8.

Methods

run(input=None, async_=False)[source]

Run the pipeline.

Parameters:

Changed in version 0.1.5: The async keyword parameter was changed to async_, as asyncis a keyword in Python 3.7 and later.

wait(timeout=None)[source]

Wait for all command sub-processes to finish, with an optional timeout. If the timeout is reached, a subprocess.TimeoutExpired exception is raised. The timeout is ignored in versions of Python < 3.3. If applied, it is applied to each of the pipeline’s commands in turn, which means that the effective timeout might be cumulative.

Changed in version 0.1.6: The timeout parameter was added.

poll_last()[source]

Check if the last command in the pipeline has terminated, and return its exit code, if available, or else None. Note that :meth:~Pipeline.poll_all` should be called when you want to ensure that all commands in a pipeline have completed.

poll_all()[source]

Check if all commands to run have terminated. Return a list of exit codes, where available, or None values where return codes are not yet available.

close()[source]

Wait for all command sub-processes to finish, and close all opened streams.

class Capture(timeout=None, buffer_size=0)[source]

A class which allows an output stream from a sub-process to be captured.

Parameters:

Methods

read(size=-1, block=True, timeout=None)[source]

Like the read method of any file-like object.

Parameters:

readline(size=-1, block=True, timeout=None)[source]

Like the readline method of any file-like object.

Parameters:

readlines(sizehint=-1, block=True, timeout=None)[source]

Like the readlines method of any file-like object.

Parameters:

expect(string_or_pattern, timeout=None)[source]

This looks for a pattern in the captured output stream. If found, it returns immediately; otherwise, it will block until the timeout expires, waiting for a match as bytes from the captured stream continue to be read.

Parameters:

Returns:

A regular expression match instance, if a match was found within the specified timeout, or None if no match was found.

close(stop_threads=False):

Close the capture object. By default, this waits for the threads which read the captured streams to terminate (which may not happen unless the child process is killed, and the streams read to exhaustion). To ensure that the threads are stopped immediately, specify True for thestop_threads parameter, which will asks the threads to terminate immediately. This may lead to losing data from the captured streams which has not yet been read.

class Popen[source]

This is a subclass of subprocess.Popen which is provided mainly to allow a process’ stdout to be mapped to its stderr. The standard library version allows you to specify stderr=STDOUT to indicate that the standard error stream of the sub-process be the same as its standard output stream. However. there’s no facility in the standard library to do stdout=STDERR – but it is provided in this subclass.

In fact, the two streams can be swapped by doing stdout=STDERR, stderr=STDOUT in a call. The STDERR value is defined in sargeas an integer constant which is understood by sarge (much asSTDOUT is an integer constant which is understood by subprocess).

Shell syntax understood by sarge

Shell commands are parsed by sarge using a simple parser.

Command syntax

The sarge parser looks for commands which are separated by ; and &:

echo foo; echo bar & echo baz

which means to run echo foo, wait for its completion, and then run echo bar and then echo baz without waiting for echo bar to complete.

The commands which are separated by & and ; are conditional commands, of the form:

or:

Here, command b is executed only if a returns success (i.e. a return code of 0), whereas d is only executed if c returns failure, i.e. a return code other than 0. Of course, in practice all of a, b,c and d could have arguments, not shown above for simplicity’s sake.

Each operand on either side of && or || could also consist of a pipeline – a set of commands connected such that the output streams of one feed into the input stream of another. For example:

or:

where the use of | indicates that the standard output of echo foo is piped to the input of cat, whereas the standard error of command-a is piped to the input of command-b.

Redirections

The sarge parser also understands redirections such as are shown in the following examples:

command arg-1 arg-2 > stdout.txt command arg-1 arg-2 2> stderr.txt command arg-1 arg-2 2>&1 command arg-1 arg-2 >&2

In general, file descriptors other than 1 and 2 are not allowed, as the functionality needed to provided them (dup2) is not properly supported on Windows. However, an esoteric special case is recognised:

echo foo | tee stdout.log 3>&1 1>&2 2>&3 | tee stderr.log > /dev/null

This redirection construct will put foo in both stdout.log and stderr.log. The effect of this construct is to swap the standard output and standard error streams, using file descriptor 3 as a temporary as in the code analogue for swapping variables a and b using temporary variablec:

This is recognised by sarge and used to swap the two streams, though it doesn’t literally use file descriptor 3, instead using a cross-platform mechanism to fulfill the requirement.

You can see this post for a longer explanation of this somewhat esoteric usage of redirection.

Next steps

You might find it helpful to look at themailing list.