Package pysys :: Package process :: Module helper :: Class ProcessWrapper
[hide private]
[frames] | no frames]

Class ProcessWrapper

source code

Process wrapper for process execution and management.

The process wrapper provides the ability to start and stop an external process, setting the process environment, working directory and state i.e. a foreground process in which case a call to the start method will not return until the process has exited, or a background process in which case the process is started in a separate thread allowing concurrent execution within the testcase. Processes started in the foreground can have a timeout associated with them, such that should the timeout be exceeded, the process will be terminated and control passed back to the caller of the method. The wrapper additionally allows control over logging of the process stdout and stderr to file, and writing to the process stdin.

Usage of the class is to first create an instance, setting all runtime parameters of the process as data attributes to the class instance via the constructor. The process can then be started and stopped via the start and stop methods of the class, as well as interrogated for its executing status via the running method, and waited for its completion via the wait method. During process execution the self.pid and seld.exitStatus data attributes are set within the class instance, and these values can be accessed directly via it's object reference.

Instance Methods [hide private]
 
__init__(self, command, arguments, environs, workingDir, state, timeout, stdout=None, stderr=None)
Create an instance of the process wrapper.
source code
 
__stringToUnicode(self, s)
Converts a unicode string or a utf-8 bit string into a unicode string.
source code
 
__writeStdin(self, hStdin)
Private method to write to the process stdin pipe.
source code
 
__quotePath(self, input)
Private method to sanitise a windows path.
source code
 
__startBackgroundProcess(self)
Private method to start a process running in the background.
source code
 
__startForegroundProcess(self)
Private method to start a process running in the foreground.
source code
 
__setExitStatus(self)
Private method to set the exit status of the process.
source code
 
write(self, data, addNewLine=True)
Write data to the stdin of the process.
source code
integer
running(self)
Check to see if a process is running, returning true if running.
source code
 
wait(self, timeout)
Wait for a process to complete execution.
source code
 
stop(self, timeout=10)
Stop a process running.
source code
 
signal(self, signal)
Send a signal to a running process.
source code
 
start(self)
Start a process using the runtime parameters set at instantiation.
source code
Instance Variables [hide private]
integer exitStatus
The process exit status for a completed process
integer pid
The process id for a running or complete process (as set by the OS)
Method Details [hide private]

__init__(self, command, arguments, environs, workingDir, state, timeout, stdout=None, stderr=None)
(Constructor)

source code 

Create an instance of the process wrapper.

Parameters:
  • command - The full path to the command to execute
  • arguments - A list of arguments to the command
  • environs - A dictionary of environment variables (key, value) for the process context execution
  • workingDir - The working directory for the process
  • state - The state of the process (pysys.constants.FOREGROUND or pysys.constants.BACKGROUND
  • timeout - The timeout in seconds to be applied to the process
  • stdout - The full path to the filename to write the stdout of the process
  • stderr - The full path to the filename to write the sdterr of the process

write(self, data, addNewLine=True)

source code 

Write data to the stdin of the process.

Note that when the addNewLine argument is set to true, if a new line does not terminate the input data string, a newline character will be added. If one already exists a new line character will not be added. Should you explicitly require to add data without the method appending a new line charater set addNewLine to false.

Parameters:
  • data - The data to write to the process stdout
  • addNewLine - True if a new line character is to be added to the end of the data string

running(self)

source code 

Check to see if a process is running, returning true if running.

Returns: integer
The running status (True / False)

wait(self, timeout)

source code 

Wait for a process to complete execution.

The method will block until either the process is no longer running, or the timeout is exceeded. Note that the method will not terminate the process if the timeout is exceeded.

Parameters:
  • timeout - The timeout to wait in seconds
Raises:

stop(self, timeout=10)

source code 

Stop a process running.

Raises:
  • ProcessError - Raised if an error occurred whilst trying to stop the process

signal(self, signal)

source code 

Send a signal to a running process.

Note that this method is not implemented for win32 processes, and calling this on a win32 OS will raise a NotImplementedError.

Parameters:
  • signal - The integer signal to send to the process
Raises:
  • ProcessError - Raised if an error occurred whilst trying to signal the process

start(self)

source code 

Start a process using the runtime parameters set at instantiation.

Raises:
  • ProcessError - Raised if there is an error creating the process
  • ProcessTimeout - Raised in the process timed out (foreground process only)