]> git.decadent.org.uk Git - dak.git/blob - daklib/config.py
b35ed9a9229e602fcbde3e51eda59a70b8f93b84
[dak.git] / daklib / config.py
1 #!/usr/bin/env python
2
3 """
4 Config access class
5
6 @contact: Debian FTPMaster <ftpmaster@debian.org>
7 @copyright: 2008  Mark Hymers <mhy@debian.org>
8 @license: GNU General Public License version 2 or later
9 """
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25 ################################################################################
26
27 # <NCommander> mhy, how about "Now with 20% more monty python references"
28
29 ################################################################################
30
31 import os
32 import apt_pkg
33 import socket
34
35 ################################################################################
36
37 default_config = "/etc/dak/dak.conf" #: default dak config, defines host properties
38
39 def which_conf_file():
40     return os.getenv("DAK_CONFIG", default_config)
41
42 class Config(object):
43     """
44     A Config object is a singleton containing
45     information about the DAK configuration
46     """
47
48     __shared_state = {}
49
50     def __init__(self, *args, **kwargs):
51         self.__dict__ = self.__shared_state
52
53         if not getattr(self, 'initialised', False):
54             self.initialised = True
55             self._readconf()
56             self._setup_routines()
57
58     def _readconf(self):
59         apt_pkg.init()
60
61         self.Cnf = apt_pkg.newConfiguration()
62
63         apt_pkg.ReadConfigFileISC(self.Cnf, which_conf_file())
64
65         # Check whether our dak.conf was the real one or
66         # just a pointer to our main one
67         res = socket.gethostbyaddr(socket.gethostname())
68         conffile = self.Cnf.get("Config::" + res[0] + "::DakConfig")
69         if conffile:
70             apt_pkg.ReadConfigFileISC(self.Cnf, conffile)
71
72         # Rebind some functions
73         # TODO: Clean this up
74         self.get = self.Cnf.get
75         self.SubTree = self.Cnf.SubTree
76         self.ValueList = self.Cnf.ValueList
77         self.Find = self.Cnf.Find
78         self.FindB = self.Cnf.FindB
79
80     def has_key(self, name):
81         return self.Cnf.has_key(name)
82
83     def __getitem__(self, name):
84         return self.Cnf[name]
85
86     def __setitem__(self, name, value):
87         self.Cnf[name] = value
88
89     @staticmethod
90     def get_db_value(name, default=None, rettype=None):
91         from daklib.dbconn import DBConfig, DBConn, NoResultFound
92         try:
93             res = DBConn().session().query(DBConfig).filter(DBConfig.name == name).one()
94         except NoResultFound:
95             return default
96
97         if rettype:
98             return rettype(res.value)
99         else:
100             return res.value
101
102     def _setup_routines(self):
103         """
104         This routine is the canonical list of which fields need to exist in
105         the config table.  If your dak instance is to work, we suggest reading it
106
107         Of course, what the values do is another matter
108         """
109         for field in [('db_revision',      None,       int),
110                       ('defaultsuitename', 'unstable', str),
111                       ('signingkeyids',    '',         str),
112                       ('exportpath',       '',         str)
113                       ]:
114             setattr(self, 'get_%s' % field[0], lambda s=None, x=field[0], y=field[1], z=field[2]: self.get_db_value(x, y, z))
115             setattr(Config, '%s' % field[0], property(fget=getattr(self, 'get_%s' % field[0])))
116
117     def get_defaultsuite(self):
118         from daklib.dbconn import get_suite
119         suitename = self.defaultsuitename
120         if not suitename:
121             return None
122         else:
123             return get_suite(suitename)
124
125     defaultsuite = property(get_defaultsuite)
126