Package epyunit :: Module SystemCalls :: Class SystemCalls
[hide private]
[frames] | no frames]

Class SystemCalls

source code


Wraps system calls for the execution of subprocesses. For supported parameters refer to 'epyunit.SystemCalls.setkargs()'.
Instance Methods [hide private]
 
__init__(self, **kargs)
Prepares the interface for subprocess calls with output cache.
source code
 
callit(self, cmdcall, **kargs)
Executes a prepared 'cmdcall' synchronous by the a preset function pointer 'self.myexe'.
source code
 
cancel(self, proc=None)
Cancels a running process.
source code
 
create(self, proc=None)
Executes a prepared 'cmdcall' with a variety of parameters.
source code
 
displayit(self, ret, **kargs)
Streams result list 'ret' in selected format to selected outtarget.
source code
 
get_proceed(self, s)
Verifies valid proceed type.
source code
 
_mode_batch_postproc(self, cmdcall, **kargs)
Postprocess
source code
 
_mode_batch_posix(self, cmdcall, **kargs)
Creates and calls a process instance on POSIX based systems.
source code
 
_mode_batch_win(self, cmdcall, **kargs)
Creates and calls a process instance on Windows based systems.
source code
 
_mode_dialogue(self, cmdcall, **kargs)
Internal call reference for processing an interactive dialogue call.
source code
 
setkargs(self, **kargs)
Sets provided parameters for the subprocess call context.
source code
 
splitLines(self, oldres)
Converts the raw string fields including ' ' of a return value into line arrays.
source code
 
__str__(self)
Prints preset call parameters.
source code
 
__repr__(self)
Prints the current representation of call parameters for subprocesses.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Instance Variables [hide private]
  console
console type for output
  bufsize
size of cache for output received from subprocess
  emptyiserr
The subprocess call for an empty string is 'success', thus this has to be checked independently for eventually erroneously missing call string
  errasexcept
raises in case of errors an exception
  out
output format/stream
  outtarget
output format/stream
  proceed
what to do...
  exectype
Type of execution
  synctype
Type of synchronization
  forcecmdcall
Force type of option passing
  tsig
Termination signal
  tmax
timeout until tsig emission
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, **kargs)
(Constructor)

source code 

Prepares the interface for subprocess calls with output cache. The initial setup includes the preparation of the result cache for the response data from stdout and stderr.

Args:
**kargs:
Parameters specific for the operations, passed through to SystemCalls.setkargs [see setkargs].
Returns:

When successful returns 'True', else returns either 'False', or raises an exception. Success is the complete addition only, thus one failure returns False.

REMARK
When errors occur in buffered mode the output from the called process is possibly not completely cached. In that case switch to 'dialogue' mode in order to get an unbuffered console display.
Raises:
passed through exceptions:
Overrides: object.__init__

callit(self, cmdcall, **kargs)

source code 

Executes a prepared 'cmdcall' synchronous by the a preset function pointer 'self.myexe'. For the full scope of call parameters including multiple subprocesses use the method [see create].

Args:
cmdcall:
A prepared call, either shell-style call-string, or a list in accordance to the call convention of 'subprocess' package. The 'shell' parameter is by default set in accordance to the provided type.
**kargs:

Parameters specific for the operations, passed through to SystemCalls.setkargs [see setkargs].

The following are also evaluated instantly within the call prologue:

proceed:
Changes predefined value and dispatches to the subcall.
raw:
Suppress the split of lines for stdout and stderr.
Returns:

Result of call, the format is:

ret[0]::= exit value

ret[1]::= STDOUT as non-processed string.

ret[2]::= STDERR as non-processed string.

REMARK
When errors occur in buffered mode the output from the called process is possibly not completely cached, e.g. in case of exeptions. In that case try switching to 'dialogue' mode in order to get an unbuffered console display. This will deliver - hopefully - the complete output.
Raises:
SystemCallsTimeout:
Rised in case of timeout by tmax/tsig. Terminates also the outstanding subprocess.

passed through exceptions:

create(self, proc=None)

source code 

Executes a prepared 'cmdcall' with a variety of parameters.

For now a placeholder only - implementation is going to follow soon.

displayit(self, ret, **kargs)

source code 

Streams result list 'ret' in selected format to selected outtarget.

Args:

ret: Data to be displayed.

**kargs:

outtarget: The target of display:

str: return as formatted string

stdout: print to sys.stdout

stderr: print to sys.stderr

out: Output for display, valid:

csv : CSV with sparse records

pass : pass through STDOUT and STDERR from subprocess

repr : Python 'repr()'

str : Python 'str()'

xml : XML

default:=self.out

Returns:

When successful returns

outtarget=='str': returns a printable formatted string

outtarget in ('stdout', 'stderr',):

'True': for success

'False': for failure

Raises:
passed through exceptions:

_mode_batch_posix(self, cmdcall, **kargs)

source code 
Creates and calls a process instance on POSIX based systems.

The IO by stdout/stdin/stderr are redirected. Supports
shell-style parameters only.

Args:
    cmdcall:
        A prepared shell-style call.

    **kargs:

        mode:
            Execution mode for the created process instance.
            * batch:
                Proceeds headless, collects reponses from STDOUT
                and STDERR.

            * dialogue
                Proeceeds interactive.

        env:
            Inhereted environment, by default the current.

        tmax:
            Timeout in seconds.

        tsig:
            Termination signal, default is KILL.

Returns:
    When successful returns 'True', else returns either 'False', or
    raises an exception.

Raises:
    for parameter 'errasexcept': SystemCallsExceptionSubprocessError
    passed through exceptions:

For further information refer to 'console' option of constructor/setkargs.

_mode_batch_win(self, cmdcall, **kargs)

source code 
Creates and calls a process instance on Windows based systems.

The IO by stdout/stdin/stderr are redirected. Supports
shell-style parameters only.

Args:
    cmdcall:
        A prepared shell-style call.

    **kargs:

        mode:
            Execution mode for the created process instance.
            * batch:
                Proceeds headless, collects reponses from STDOUT
                and STDERR.

            * dialogue
                Proeceeds interactive.

        env:
            Current environment.

        tmax:
            Timeout in seconds.

        tsig:
            Termination signal, default is KILL.

Returns:
    When successful returns 'True', else returns either 'False', or
    raises an exception.

Raises:
    for parameter 'errasexcept': SystemCallsExceptionSubprocessError
    passed through exceptions:

For further information refer to 'console' option of constructor/setkargs.

_mode_dialogue(self, cmdcall, **kargs)

source code 

Internal call reference for processing an interactive dialogue call.

Supports shell-style parameters only.

For further information refer to 'console' option of constructor/setkargs.

setkargs(self, **kargs)

source code 
Sets provided parameters for the subprocess call context.

Applicable for the initial call of self.__init__(),
and later modification. Called for each start of
a subprocess in order to update optional the specific
call context modification.

Args:
    **kargs: Parameter specific for the operation,


        bufsize:
            The size of the output buffer for the called
            subprocess, refer to **subprocess.Popen**.
            Default value is -1.

        console ('cli','dialogue')
            ffs ('batch','ui','gtk', 'qt')

            cli:
                Works in batch mode, particularly the
                stdin, stdout, and stderr streams are
                caught into a string buffer by the
                calling process via a pipe. The content
                is passed after termination of the
                called sub-process.

            dialogue:
                Works without buffered io streams.
                Thus allows for interaction, but not
                post-processing.

            verbose:
                Verbose.

        debug:
            Sets debug for rule data flow.

        emptyiserr:
            Treats passed empty call strings as error.
            The applied 'subprocess.Popen()' treats them as
            success, which may cover errors in generated
            call strings, particularly in loops.

            default := False

        env:
            Environment to be passed through to the subprocess.
            
            Default := current

        errasexcept:
            Passes errors as exceptions, transforms the resuls from
            subprocesses into Exceptions data. Exits the process.

            default := False

        exectype:
            Type of execution.
            * inproc:
                Calls 'Popen' directly from within the process.

            * bythread:
                Starts an intermediate thread within current
                process and executes 'Popen'.

            * byfork
                Starts an intermediate process by fork and
                executes 'Popen'.

        forcecmdcall:
            Forces type of the command call option passed to 'Popen'.
            * shell
            * list

        out: Output for display. Supported types are:

            csv:
                CSV seperated by ';', with sparse records

            pass:
                Pass through STDOUT and STDERR from
                subprocess

            repr:
                Python 'repr()'

            str:
                Python 'str()'

            xml:
                XML

        passerr:
            Passes errors from subprocesses transparently
            through by stdout, stderr, and exit code. Exits
            the process.

            default := False

        proceed ('print','trace', 'doit')
            print - trace only

            trace - execute and trace

            doit  - execute

        raw:
            Pass through STDOUT and STDERR.

        rules:
            Sets the rules object.

        synctype:
            Type of call synchronization.
            * async:
                Executes the subprocess asynchronously, this e.g. enables
                in current implementation for platform independent timeouts.

            * sync:
                Executes synchronously, thus blocks any other execution.

        tsig:
            Termination signal, default is KILL.

        tmax:
            Timeout in seconds.

        useexit:
            Use exit code for error detection of subprocess.

            default := True

        usestderr:
            Use 'sys.stderr' output for error detection of subprocess.
            When set to 'True', the presence of a string is treated as error
            condition.

            default := False

        verbose:
            Sets verbose for rule data flow.

Returns:
    When successful returns 'True', else returns either 'False', or
    raises an exception.
    Success is the complete addition only, thus one failure returns
    False.

Raises:
    passed through exceptions:

splitLines(self, oldres)

source code 

Converts the raw string fields including ' ' of a return value into line arrays.

Args:
oldres: The raw result of a previous call.
Returns:

Result of call, the format is:

ret[0]::=exit value

ret[1]::=list of lines from former stdout output, each as
a partial non-processed string.
ret[2]::=list of lines from former stderr output, each as
a partial non-processed string.
Raises:
passed through exceptions:

__str__(self)
(Informal representation operator)

source code 
Prints preset call parameters.
Overrides: object.__str__

__repr__(self)
(Representation operator)

source code 
Prints the current representation of call parameters for subprocesses.
Overrides: object.__repr__