| # Gaudi Framework, version v21r11 |

Home |  | Generated: 30 Sep 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 46 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 50 of file GaudiTest.py.

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

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

    Set an environment variable recording the previous value.

Definition at line 60 of file GaudiTest.py.

    00060                                    :
    00061         """
    00062         Set an environment variable recording the previous value.
    00063         """
    00064         if key not in self.old_values :
    00065             if key in self.env :
    00066                 if not self._keep_same or self.env[key] != value:
    00067                     self.old_values[key] = self.env[key]
    00068             else:
    00069                 self.old_values[key] = None
    00070         self.env[key] = value
    00071
        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 72 of file GaudiTest.py.

    00072                              :
    00073         """
    00074         Get an environment variable.
    00075         Needed to provide the same interface as os.environ.
    00076         """
    00077         return self.env[key]
    00078
        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 79 of file GaudiTest.py.

    00079                              :
    00080         """
    00081         Unset an environment variable.
    00082         Needed to provide the same interface as os.environ.
    00083         """
    00084         if key not in self.env :
    00085             raise KeyError(key)
    00086         self.old_values[key] = self.env[key]
    00087         del self.env[key]
    00088
        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 89 of file GaudiTest.py.

    00089                   :
    00090         """
    00091         Return the list of defined environment variables.
    00092         Needed to provide the same interface as os.environ.
    00093         """
    00094         return self.env.keys()
    00095
        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 96 of file GaudiTest.py.

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

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

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

Definition at line 103 of file GaudiTest.py.

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

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

    Revert all the changes done to the orignal environment.

Definition at line 110 of file GaudiTest.py.

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

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

    Revert the changes on destruction.

Definition at line 121 of file GaudiTest.py.

    00121                      :
    00122         """
    00123         Revert the changes on destruction.
    00124         """
    00125         #print "Restoring the environment"
    00126         self.restore()
    00127
        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 128 of file GaudiTest.py.

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

## Member Data Documentation

GaudiTest::TemporaryEnvironment::old_values

Definition at line 56 of file GaudiTest.py.

GaudiTest::TemporaryEnvironment::env

Definition at line 57 of file GaudiTest.py.

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

Definition at line 58 of file GaudiTest.py.

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

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