shell_proc.py

shell_proc.py#

Some basic shell utilities, used for ExternalCodeComp mostly.

exception openmdao.utils.shell_proc.CalledProcessError(returncode, cmd, errormsg)[source]

Bases: CalledProcessError

subprocess.CalledProcessError plus errormsg attribute.

Parameters:
returncodeint

Error code for this error.

cmdstr or list

If a string, then this is the command line to execute, and the subprocess.Popen shell argument is set True. Otherwise, this is a list of arguments; the first is the command to execute.

errormsgstr

Error message for this error.

Attributes:
errormsgstr

Error message saved for string access.

__init__(returncode, cmd, errormsg)[source]

Initialize.

add_note()

Exception.add_note(note) – add a note to the exception

args
property stdout

Alias for output attribute, to match stderr

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class openmdao.utils.shell_proc.ShellProc(args, stdin=None, stdout=None, stderr=None, env=None, universal_newlines=False)[source]

Bases: Popen

A slight modification to subprocess.Popen.

If args is a string, then the shell argument is set True. Updates a copy of os.environ with env and opens files for any stream which is a str.

Parameters:
argsstr or list

If a string, then this is the command line to execute and the subprocess.Popen shell argument is set True. Otherwise, this is a list of arguments; the first is the command to execute.

stdinstr, file, or int

Specify handling of stdin stream. If a string, a file of that name is opened. Otherwise, see the subprocess documentation.

stdoutstr, file, or int

Specify handling of stdout stream. If a string, a file of that name is opened. Otherwise, see the subprocess documentation.

stderrstr, file, or int

Specify handling of stderr stream. If a string, a file of that name is opened. Otherwise, see the subprocess documentation.

envdict

Environment variables for the command.

universal_newlinesbool

Set to True to turn on universal newlines.

Attributes:
_stdin_argstr, file, or int

Save handle to make closing easier.

_stdout_argstr, file, or int

Save handle to make closing easier.

_stderr_argstr, file, or int

Save handle to make closing easier.

_inpstr, file, or int

Save handle to make closing easier.

_outstr, file, or int

Save handle to make closing easier.

_errstr, file, or int

Save handle to make closing easier.

__init__(args, stdin=None, stdout=None, stderr=None, env=None, universal_newlines=False)[source]

Initialize.

close_files()[source]

Close files that were implicitly opened.

communicate(input=None, timeout=None)

Interact with process: Send data to stdin and close it. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.

The optional “input” argument should be data to be sent to the child process, or None, if no data should be sent to the child. communicate() returns a tuple (stdout, stderr).

By default, all communication is in bytes, and therefore any “input” should be bytes, and the (stdout, stderr) will be bytes. If in text mode (indicated by self.text_mode), any “input” should be a string, and (stdout, stderr) will be strings decoded according to locale encoding, or by “encoding” if set. Text mode is triggered by setting any of text, encoding, errors or universal_newlines.

error_message(return_code)[source]

Return error message for return_code.

The error messages are derived from the operating system definitions. Some programs don’t necessarily return exit codes conforming to these definitions.

Parameters:
return_codeint

Return code from poll().

Returns:
str

Error Message string.

kill()

Kill the process with SIGKILL

poll()

Check if child process has terminated. Set and return returncode attribute.

send_signal(sig)

Send a signal to the process.

terminate(timeout=None)[source]

Stop child process.

If timeout is specified, then wait() will be called to wait for the process to terminate.

Parameters:
timeoutfloat (seconds)

Maximum time to wait for the process to stop. A value of zero implies an infinite maximum wait.

Returns:
int

Return Code.

str

Error Message.

property universal_newlines
wait(poll_delay=0.0, timeout=0.0)[source]

Poll for command completion or timeout.

Closes any files implicitly opened.

Parameters:
poll_delayfloat (seconds)

Time to delay between polling for command completion. A value of zero uses an internal default.

timeoutfloat (seconds)

Maximum time to wait for command completion. A value of zero implies an infinite maximum wait.

Returns:
int

Return Code.

str

Error Message.

openmdao.utils.shell_proc.call(args, stdin=None, stdout=None, stderr=None, env=None, poll_delay=0.0, timeout=0.0)[source]

Run command with arguments.

Parameters:
argsstr or list

If a string, then this is the command line to execute and the subprocess.Popen shell argument is set True. Otherwise, this is a list of arguments; the first is the command to execute.

stdinstr, file, or int

Specify handling of stdin stream. If a string, a file of that name is opened. Otherwise, see the subprocess documentation.

stdoutstr, file, or int

Specify handling of stdout stream. If a string, a file of that name is opened. Otherwise, see the subprocess documentation.

stderrstr, file, or int

Specify handling of stderr stream. If a string, a file of that name is opened. Otherwise, see the subprocess documentation.

envdict

Environment variables for the command.

poll_delayfloat (seconds)

Time to delay between polling for command completion. A value of zero uses an internal default.

timeoutfloat (seconds)

Maximum time to wait for command completion. A value of zero implies an infinite maximum wait.

Returns:
int

Return Code.

str

Error Message.

openmdao.utils.shell_proc.check_call(args, stdin=None, stdout=None, stderr=None, env=None, poll_delay=0.0, timeout=0.0)[source]

Run command with arguments.

Raises CalledProcessError if process returns an error code.

Parameters:
argsstr or list

If a string, then this is the command line to execute, and the subprocess.Popen shell argument is set True. Otherwise, this is a list of arguments; the first is the command to execute.

stdinstr, file, or int

Specify handling of stdin stream. If a string, a file of that name is opened. Otherwise, see the subprocess documentation.

stdoutstr, file, or int

Specify handling of stdout stream. If a string, a file of that name is opened. Otherwise, see the subprocess documentation.

stderrstr, file, or int

Specify handling of stderr stream. If a string, a file of that name is opened. Otherwise, see the subprocess documentation.

envdict

Environment variables for the command.

poll_delayfloat (seconds)

Time to delay between polling for command completion. A value of zero uses an internal default.

timeoutfloat (seconds)

Maximum time to wait for command completion. A value of zero implies an infinite maximum wait.