~ubuntu-branches/ubuntu/saucy/drizzle/saucy

« back to all changes in this revision

Viewing changes to tests/lib/sys_mgmt/environment_management.py

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.2.11) (2.1.16 sid)
  • Revision ID: package-import@ubuntu.com-20120619104649-9ij634mxm4x8pp4l
Tags: 1:7.1.36-stable-1ubuntu1
* Merge from Debian unstable. (LP: #987575)
  Remaining changes:
  - Added upstart script.
* debian/drizzle.upstart: dropped logger since upstart logs job
  output now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# -*- mode: python; indent-tabs-mode: nil; -*-
 
3
# vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
4
#
 
5
# Copyright (C) 2011 Patrick Crews
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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
 
20
 
 
21
"""environment_management
 
22
 
 
23
   code we use to tweak the environment during test execution
 
24
 
 
25
"""
 
26
 
 
27
# imports
 
28
import os
 
29
import sys
 
30
import copy
 
31
 
 
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 = ':'
 
38
        
 
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
 
42
 
 
43
        """
 
44
 
 
45
        return self.env_var_delimiter.join(value_list)
 
46
 
 
47
    def set_env_var(self, var_name, var_value, quiet=0):
 
48
        """Set an environment variable.  We really just abstract
 
49
           voodoo on os.environ
 
50
 
 
51
        """
 
52
        if not quiet:
 
53
            self.logging.debug("Setting env var: %s" %(var_name))
 
54
        try:
 
55
            os.environ[var_name]=var_value
 
56
        except Exception, e:
 
57
            self.logging.error("Issue setting environment variable %s to value %s" %(var_name, var_value))
 
58
            self.logging.error("%s" %(e))
 
59
            sys.exit(1)
 
60
 
 
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
 
67
 
 
68
        """
 
69
 
 
70
        if not working_environment:
 
71
            working_environment = os.environ
 
72
        working_environment.update(desired_vars)
 
73
        return working_environment
 
74
 
 
75
    def create_working_environment(self, desired_vars):
 
76
        """ We return a copy of os.environ updated with desired_vars """
 
77
 
 
78
        working_copy = copy.deepcopy(os.environ)
 
79
        return self.update_environment_vars( desired_vars
 
80
                                    , working_environment = working_copy )
 
81
 
 
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
 
86
 
 
87
        """
 
88
        new_var_value = ""
 
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 ]
 
93
            else:
 
94
                new_var_values = [ append_string, cur_var_value ]
 
95
            new_var_value = self.env_var_delimiter.join(new_var_values)
 
96
        else:
 
97
            # No existing variable value
 
98
            new_var_value = append_string
 
99
        return new_var_value