| # Gaudi Framework, version v21r9 |

Home |  | Generated: 3 May 2010

# GaudiTest::TemporaryEnvironment Class Reference

List of all members.

## Public Member Functions
def | __init__
def | __setitem__
def | __getitem__
def | __delitem__
def | keys
def | items
def | __contains__
def | restore
def | __del__
def | gen_script
## Public Attributes
| old_values
| env
## Private Attributes
| _keep_same

## Detailed Description

    Class to changes the environment temporarily.

Definition at line 48 of file GaudiTest.py.

## Member Function Documentation

def GaudiTest::TemporaryEnvironment::__init__ | ( |  | self,
|  |  | orig = `os.environ`,
|  |  | keep_same = `False` |
| ) |  |  |

    Create a temporary environment on top of the one specified
    (it can be another TemporaryEnvironment instance).

Definition at line 52 of file GaudiTest.py.

    00052                                                             :
    00053         """
    00054         Create a temporary environment on top of the one specified
    00055         (it can be another TemporaryEnvironment instance).
    00056         """
    00057         #print "New environment"
    00058         self.old_values = {}
    00059         self.env = orig
    00060         self._keep_same = keep_same
    00061
        def __setitem__(self,key,value):

def GaudiTest::TemporaryEnvironment::__setitem__ | ( |  | self,
|  |  | key,
|  |  | value |
| ) |  |  |

    Set an environment variable recording the previous value.

Definition at line 62 of file GaudiTest.py.

    00062                                    :
    00063         """
    00064         Set an environment variable recording the previous value.
    00065         """
    00066         if key not in self.old_values :
    00067             if key in self.env :
    00068                 if not self._keep_same or self.env[key] != value:
    00069                     self.old_values[key] = self.env[key]
    00070             else:
    00071                 self.old_values[key] = None
    00072         self.env[key] = value
    00073
        def __getitem__(self,key):

def GaudiTest::TemporaryEnvironment::__getitem__ | ( |  | self,
|  |  | key |
| ) |  |  |

    Get an environment variable.
    Needed to provide the same interface as os.environ.

Definition at line 74 of file GaudiTest.py.

    00074                              :
    00075         """
    00076         Get an environment variable.
    00077         Needed to provide the same interface as os.environ.
    00078         """
    00079         return self.env[key]
    00080
        def __delitem__(self,key):

def GaudiTest::TemporaryEnvironment::__delitem__ | ( |  | self,
|  |  | key |
| ) |  |  |

    Unset an environment variable.
    Needed to provide the same interface as os.environ.

Definition at line 81 of file GaudiTest.py.

    00081                              :
    00082         """
    00083         Unset an environment variable.
    00084         Needed to provide the same interface as os.environ.
    00085         """
    00086         if key not in self.env :
    00087             raise KeyError(key)
    00088         self.old_values[key] = self.env[key]
    00089         del self.env[key]
    00090
        def keys(self):

def GaudiTest::TemporaryEnvironment::keys | ( |  | self | ) |

    Return the list of defined environment variables.
    Needed to provide the same interface as os.environ.

Definition at line 91 of file GaudiTest.py.

    00091                   :
    00092         """
    00093         Return the list of defined environment variables.
    00094         Needed to provide the same interface as os.environ.
    00095         """
    00096         return self.env.keys()
    00097
        def items(self):

def GaudiTest::TemporaryEnvironment::items | ( |  | self | ) |

    Return the list of (name,value) pairs for the defined environment variables.
    Needed to provide the same interface as os.environ.

Definition at line 98 of file GaudiTest.py.

    00098                    :
    00099         """
    00100         Return the list of (name,value) pairs for the defined environment variables.
    00101         Needed to provide the same interface as os.environ.
    00102         """
    00103         return self.env.items()
    00104
        def __contains__(self,key):

def GaudiTest::TemporaryEnvironment::__contains__ | ( |  | self,
|  |  | key |
| ) |  |  |

    Operator 'in'.
    Needed to provide the same interface as os.environ.

Definition at line 105 of file GaudiTest.py.

    00105                               :
    00106         """
    00107         Operator 'in'.
    00108         Needed to provide the same interface as os.environ.
    00109         """
    00110         return key in self.env
    00111
        def restore(self):

def GaudiTest::TemporaryEnvironment::restore | ( |  | self | ) |

    Revert all the changes done to the orignal environment.

Definition at line 112 of file GaudiTest.py.

    00112                      :
    00113         """
    00114         Revert all the changes done to the orignal environment.
    00115         """
    00116         for key,value in self.old_values.items():
    00117             if value is None:
    00118                 del self.env[key]
    00119             else:
    00120                 self.env[key] = value
    00121         self.old_values = {}
    00122
        def __del__(self):

def GaudiTest::TemporaryEnvironment::__del__ | ( |  | self | ) |

    Revert the changes on destruction.

Definition at line 123 of file GaudiTest.py.

    00123                      :
    00124         """
    00125         Revert the changes on destruction.
    00126         """
    00127         #print "Restoring the environment"
    00128         self.restore()
    00129
        def gen_script(self,shell_type):

def GaudiTest::TemporaryEnvironment::gen_script | ( |  | self,
|  |  | shell_type |
| ) |  |  |

    Generate a shell script to reproduce the changes in the environment.

Definition at line 130 of file GaudiTest.py.

    00130                                    :
    00131         """
    00132         Generate a shell script to reproduce the changes in the environment.
    00133         """
    00134         shells = [ 'csh', 'sh', 'bat' ]
    00135         if shell_type not in shells:
    00136             raise RuntimeError("Shell type '%s' unknown. Available: %s"%(shell_type,shells))
    00137         out = ""
    00138         for key,value in self.old_values.items():
    00139             if key not in self.env:
    00140                 # unset variable
    00141                 if shell_type == 'csh':
    00142                     out += 'unsetenv %s\n'%key
    00143                 elif shell_type == 'sh':
    00144                     out += 'unset %s\n'%key
    00145                 elif shell_type == 'bat':
    00146                     out += 'set %s=\n'%key
    00147             else:
    00148                 # set variable
    00149                 if shell_type == 'csh':
    00150                     out += 'setenv %s "%s"\n'%(key,self.env[key])
    00151                 elif shell_type == 'sh':
    00152                     out += 'export %s="%s"\n'%(key,self.env[key])
    00153                 elif shell_type == 'bat':
    00154                     out += 'set %s=%s\n'%(key,self.env[key])
    00155         return out
    00156
    class TempDir:

## Member Data Documentation

GaudiTest::TemporaryEnvironment::old_values

Definition at line 58 of file GaudiTest.py.

GaudiTest::TemporaryEnvironment::env

Definition at line 59 of file GaudiTest.py.

GaudiTest::TemporaryEnvironment::_keep_same`[private]`

Definition at line 60 of file GaudiTest.py.

The documentation for this class was generated from the following file:

- /afs/cern.ch/sw/Gaudi/releases/GAUDI/GAUDI_v21r9/GaudiPolicy/qmtest_classes/GaudiTest.py