config.py 3.9 KB
Newer Older
M
Mark Hymers 已提交
1 2
#!/usr/bin/env python

3 4 5 6 7 8 9
"""
Config access class

@contact: Debian FTPMaster <ftpmaster@debian.org>
@copyright: 2008  Mark Hymers <mhy@debian.org>
@license: GNU General Public License version 2 or later
"""
M
Mark Hymers 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

################################################################################

# <NCommander> mhy, how about "Now with 20% more monty python references"

################################################################################

31
import os
M
Mark Hymers 已提交
32 33 34 35 36
import apt_pkg
import socket

################################################################################

37
default_config = "/etc/dak/dak.conf" #: default dak config, defines host properties
M
Mark Hymers 已提交
38

39
def which_conf_file():
C
Chris Lamb 已提交
40
    return os.getenv("DAK_CONFIG", default_config)
M
Mark Hymers 已提交
41

42
class Config(object):
M
Mark Hymers 已提交
43 44 45 46
    """
    A Config object is a singleton containing
    information about the DAK configuration
    """
47 48 49

    __shared_state = {}

M
Mark Hymers 已提交
50
    def __init__(self, *args, **kwargs):
51 52 53 54 55
        self.__dict__ = self.__shared_state

        if not getattr(self, 'initialised', False):
            self.initialised = True
            self._readconf()
56
            self._setup_routines()
M
Mark Hymers 已提交
57 58 59 60 61 62

    def _readconf(self):
        apt_pkg.init()

        self.Cnf = apt_pkg.newConfiguration()

63
        apt_pkg.ReadConfigFileISC(self.Cnf, which_conf_file())
M
Mark Hymers 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76

        # Check whether our dak.conf was the real one or
        # just a pointer to our main one
        res = socket.gethostbyaddr(socket.gethostname())
        conffile = self.Cnf.get("Config::" + res[0] + "::DakConfig")
        if conffile:
            apt_pkg.ReadConfigFileISC(self.Cnf, conffile)

        # Rebind some functions
        # TODO: Clean this up
        self.get = self.Cnf.get
        self.SubTree = self.Cnf.SubTree
        self.ValueList = self.Cnf.ValueList
77 78
        self.Find = self.Cnf.Find
        self.FindB = self.Cnf.FindB
M
Mark Hymers 已提交
79

80 81 82
    def has_key(self, name):
        return self.Cnf.has_key(name)

M
Mark Hymers 已提交
83 84 85
    def __getitem__(self, name):
        return self.Cnf[name]

M
Mark Hymers 已提交
86 87
    def __setitem__(self, name, value):
        self.Cnf[name] = value
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

    @staticmethod
    def get_db_value(name, default=None, rettype=None):
        from daklib.dbconn import DBConfig, DBConn, NoResultFound
        try:
            res = DBConn().session().query(DBConfig).filter(DBConfig.name == name).one()
        except NoResultFound:
            return default

        if rettype:
            return rettype(res.value)
        else:
            return res.value

    def _setup_routines(self):
        """
        This routine is the canonical list of which fields need to exist in
        the config table.  If your dak instance is to work, we suggest reading it

        Of course, what the values do is another matter
        """
        for field in [('db_revision',      None,       int),
                      ('defaultsuitename', 'unstable', str)]:
            setattr(self, 'get_%s' % field[0], lambda x=None: self.get_db_value(field[0], field[1], field[2]))
            setattr(Config, '%s' % field[0], property(fget=getattr(self, 'get_%s' % field[0])))

    def get_defaultsuite(self):
        from daklib.dbconn import get_suite
        suitename = self.defaultsuitename
        if not suitename:
            return None
        else:
            return get_suite(suitename)

    defaultsuite = property(get_defaultsuite)