pyyaks.shell

Utilities to run subprocesses

Functions

pyyaks.shell.bash_shell(cmdstr, logfile=None, importenv=False, getenv=False, env=None)

Run the command string cmdstr in a bash shell. It can have multiple lines. Each line is separately sent to the shell. The exit status is checked if the shell comes back with a PS1 prompt. Bash control structures like if or for use prompt PS2 and in this case status is not checked. At the end the printenv command may be used in order to find any changes to the environment that occurred as a result of the commands. If exit status is non-zero at any point then processing is terminated and a ShellError exception is raise.

Parameters:
  • cmdstr – command string
  • logfile – append output to the suppplied file object
  • importenv – import any environent changes back to python env
  • getenv – get the environent changes after running cmdstr
  • env – set environment using env dict prior to running commands
Returns:

(outlines, deltaenv)

pyyaks.shell.bash(cmdstr, importenv=False, env=None)

Render the input cmdstr and run in a bash shell. Output is logged at the VERBOSE level.

Parameters:
  • cmdstr – command string
  • importenv – import any environent changes back to python env
  • env – set environment using env dict prior to running commands
Returns:

bash output string

pyyaks.shell.getenv(cmdstr, importenv=False, env=None)

Run the cmdstr string in a bash shell and return the resulting update to the current python environment (os.environ).

Parameters:
  • cmdstr – command string
  • importenv – import any environent changes back to python env
  • env – set environment using env dict prior to running commands
Returns:

Dict of environment vars update produced by cmdstr

pyyaks.shell.importenv(cmdstr, env=None)

Run cmdstr in a bash shell and import the environment updates into the current python environment (os.environ).

Parameters:
  • cmdstr – command string
  • env – set environment using env dict prior to running commands
Returns:

Dict of environment vars update produced by cmdstr

Classes

class pyyaks.shell.Spawn(stdout=<open file '<stdout>', mode 'w' at 0x7f20ff9c31e0>, timeout=None, catch=False, stderr=-2, shell=False)

Bases: object

Provide methods to run subprocesses in a controlled and simple way. Features:
  • Uses the subprocess.Popen() class
  • Send stdout and/or stderr output to a file
  • Specify a job timeout
  • Catch exceptions and log warnings

Example usage:

>>> from pyyaks.shell import Spawn, bash, getenv, importenv
>>> 
>>> spawn = Spawn()
>>> status = spawn.run(['echo', 'hello'])
hello
>>> status
0
>>> 
>>> try:
...     spawn.run(['bad', 'command'])
... except Exception, error:
...     error
... 
OSError(2, 'No such file or directory')
>>> spawn.run(['bad', 'command'], catch=True)
Warning - OSError: [Errno 2] No such file or directory
>>> print spawn.exitstatus
None
>>> print spawn.outlines
['Warning - OSError: [Errno 2] No such file or directory\n']
>>> 
>>> spawn = Spawn(stdout=None, shell=True)
>>> spawn.run('echo hello')
0
>>> spawn.run('fail fail fail')
127
>>> 
>>> spawn = Spawn(stdout=None, shell=True, stderr=None)
>>> spawn.run('fail fail fail')
127
>>> print spawn.outlines
[]
Additional object attributes:
  • openfiles: List of file objects created during init corresponding

    to filenames supplied in outputs list

run(cmd, timeout=None, catch=None, shell=None)

Run the command cmd and abort if timeout is exceeded.

Attributes after run():
  • outlines: list of output lines from process
  • exitstatus: process exit status or None if an exception occurred
Parameters:
  • cmd – list of strings or a string(see Popen docs)
  • timeout – command timeout (default: self.timeout)
  • catch – catch exceptions (default: self.catch)
  • shell – run cmd in shell (default: self.shell)
Return type:

process exit value

Exceptions

class pyyaks.shell.RunTimeoutError
class pyyaks.shell.ShellError

Table Of Contents

Previous topic

pyyaks.logger

Next topic

pyyaks.task

This Page