2
# -*- mode: python; indent-tabs-mode: nil; -*-
3
# vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5
# Copyright (C) 2011 Patrick Crews
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
"""environment_management
23
code we use to tweak the environment during test execution
32
class environmentManager():
33
""" Handles updating / managing environment stuff """
34
def __init__(self, system_manager, variables):
35
self.system_manager = system_manager
36
self.logging = self.system_manager.logging
37
self.env_var_delimiter = ':'
39
def join_env_var_values(self, value_list):
40
""" Utility to join multiple values into a nice string
41
for setting an env var to
45
return self.env_var_delimiter.join(value_list)
47
def set_env_var(self, var_name, var_value, quiet=0):
48
"""Set an environment variable. We really just abstract
53
self.logging.debug("Setting env var: %s" %(var_name))
55
os.environ[var_name]=var_value
57
self.logging.error("Issue setting environment variable %s to value %s" %(var_name, var_value))
58
self.logging.error("%s" %(e))
61
def update_environment_vars(self, desired_vars, working_environment=None):
62
""" We update the environment vars with desired_vars
63
The expectation is that you know what you are asking for ; )
64
If working_environment is provided, we will update that with
65
desired_vars. We operate directly on os.environ by default
66
We return our updated environ dictionary
70
if not working_environment:
71
working_environment = os.environ
72
working_environment.update(desired_vars)
73
return working_environment
75
def create_working_environment(self, desired_vars):
76
""" We return a copy of os.environ updated with desired_vars """
78
working_copy = copy.deepcopy(os.environ)
79
return self.update_environment_vars( desired_vars
80
, working_environment = working_copy )
82
def append_env_var(self, var_name, append_string, suffix=1, quiet=0):
83
""" We add the values in var_values to the environment variable
84
var_name. Depending on suffix value, we either append or prepend
85
we return a string suitable for os.putenv
89
if var_name in os.environ:
90
cur_var_value = os.environ[var_name]
91
if suffix: # We add new values to end of existing value
92
new_var_values = [ cur_var_value, append_string ]
94
new_var_values = [ append_string, cur_var_value ]
95
new_var_value = self.env_var_delimiter.join(new_var_values)
97
# No existing variable value
98
new_var_value = append_string