~joetalbott/tarmac/tarmac-test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Copyright 2009 Paul Hummer
# Copyright 2009 Canonical Ltd.
#
# This file is part of Tarmac.
#
# Tarmac is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by
# the Free Software Foundation.
#
# Tarmac is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tarmac.  If not, see <http://www.gnu.org/licenses/>.

'''Configuration handler.'''
# pylint: disable-msg=C0103
__metaclass__ = type

import os
from ConfigParser import SafeConfigParser as ConfigParser

from tarmac.xdgdirs import xdg_config_home, xdg_cache_home


class TarmacConfig(ConfigParser):
    '''A class for handling configuration.'''

    def __init__(self):
        DEFAULTS = {
            'log_file': os.path.join(self.CONFIG_HOME, 'tarmac.log'),
            }

        ConfigParser.__init__(self)

        self._check_config_dirs()
        self.read(self.CONFIG_FILE)

        if not self.has_section('Tarmac'):
            self.add_section('Tarmac')

        if not self.has_option('Tarmac', 'log_file'):
            self.set('Tarmac', 'log_file', DEFAULTS['log_file'])

        for key, val in self.items('Tarmac'):
            setattr(self, key, val)

    def set(self, section, option, value):
        """Wrap the set method, so we can tweak our attrs."""
        ConfigParser.set(self, section, option, str(value))
        if section == 'Tarmac':
            setattr(self, option, value)

    def remove_option(self, section, option):
        """Wrap the remove_option method so we can tweak our attrs."""
        ConfigParser.remove_option(self, section, option)
        if section == 'Tarmac':
            delattr(self, option)

    @property
    def CONFIG_HOME(self):
        '''Return the base dir for the config.'''
        try:
            return os.environ['TARMAC_CONFIG_HOME']
        except KeyError:
            return os.path.join(xdg_config_home, 'tarmac')

    @property
    def CACHE_HOME(self):
        '''Return the base dir for cache.'''
        try:
            return os.environ['TARMAC_CACHE_HOME']
        except KeyError:
            return os.path.join(xdg_cache_home, 'tarmac')

    @property
    def PID_FILE(self):
        '''Return the path to the pid file.'''
        try:
            return os.environ['TARMAC_PID_FILE']
        except KeyError:
            return os.path.join(self.CACHE_HOME, 'tarmac.pid')

    @property
    def CREDENTIALS(self):
        '''Return the path to the credentials.'''
        try:
            return os.environ['TARMAC_CREDENTIALS']
        except KeyError:
            return os.path.join(self.CONFIG_HOME, 'credentials')

    @property
    def CONFIG_FILE(self):
        '''Return the path to the config file itself.'''
        return os.path.join(self.CONFIG_HOME, 'tarmac.conf')

    @property
    def branches(self):
        '''Return all the branches in the config.'''
        return [section for section in self.sections() if
                section.startswith('lp:')]

    def _check_config_dirs(self):
        '''Create the configuration directory if it does not exist.'''
        if not os.path.exists(self.CONFIG_HOME):
            os.makedirs(self.CONFIG_HOME)
        if not os.path.exists(self.CACHE_HOME):
            os.makedirs(self.CACHE_HOME)
        pid_dir = os.path.dirname(self.PID_FILE)
        if not os.path.exists(pid_dir):
            os.makedirs(pid_dir)


class BranchConfig:
    '''A Branch specific config.

    Instead of providing the whole config for branches, it is better to provide
    it with only its specific config vars.
    '''

    def __init__(self, branch_name, config):
        if config.has_section(branch_name):
            for key, val in config.items(branch_name):
                setattr(self, key, val)

    def get(self, attr, default=None):
        '''A convenient method for getting a config key that may be missing.

        Defaults to None if the key is not set.
        '''
        return getattr(self, attr, default)