| # Gaudi Framework, version v21r7 |

Home |  | Generated: 22 Jan 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 47 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 51 of file GaudiTest.py.

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

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

    Set an environment variable recording the previous value.

Definition at line 61 of file GaudiTest.py.

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

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

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

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

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

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

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

Definition at line 104 of file GaudiTest.py.

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

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

    Revert all the changes done to the orignal environment.

Definition at line 111 of file GaudiTest.py.

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

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

    Revert the changes on destruction.

Definition at line 122 of file GaudiTest.py.

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

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

## Member Data Documentation

GaudiTest::TemporaryEnvironment::old_values

Definition at line 57 of file GaudiTest.py.

GaudiTest::TemporaryEnvironment::env

Definition at line 58 of file GaudiTest.py.

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

Definition at line 59 of file GaudiTest.py.

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

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