]> git.decadent.org.uk Git - dak.git/blob - daklib/config.py
Merge branch 'dak-unpriv' into merge
[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 # suppress some deprecation warnings in squeeze related to apt_pkg
40 # module
41 import warnings
42 warnings.filterwarnings('ignore', ".*apt_pkg.* is deprecated.*", DeprecationWarning)
43
44 ################################################################################
45
46 def which_conf_file():
47     return os.getenv("DAK_CONFIG", default_config)
48
49 class Config(object):
50     """
51     A Config object is a singleton containing
52     information about the DAK configuration
53     """
54
55     __shared_state = {}
56
57     def __init__(self, *args, **kwargs):
58         self.__dict__ = self.__shared_state
59
60         if not getattr(self, 'initialised', False):
61             self.initialised = True
62             self._readconf()
63             self._setup_routines()
64
65     def _readconf(self):
66         apt_pkg.init()
67
68         self.Cnf = apt_pkg.Configuration()
69
70         apt_pkg.read_config_file_isc(self.Cnf, which_conf_file())
71
72         # Check whether our dak.conf was the real one or
73         # just a pointer to our main one
74         res = socket.gethostbyaddr(socket.gethostname())
75         conffile = self.Cnf.get("Config::" + res[0] + "::DakConfig")
76         if conffile:
77             apt_pkg.read_config_file_isc(self.Cnf, conffile)
78
79         # Rebind some functions
80         # TODO: Clean this up
81         self.get = self.Cnf.get
82         self.subtree = self.Cnf.subtree
83         self.value_list = self.Cnf.value_list
84         self.find = self.Cnf.find
85         self.find_b = self.Cnf.find_b
86         self.find_i = self.Cnf.find_i
87
88     def has_key(self, name):
89         return name in self.Cnf
90
91     def __contains__(self, name):
92         return name in self.Cnf
93
94     def __getitem__(self, name):
95         return self.Cnf[name]
96
97     def __setitem__(self, name, value):
98         self.Cnf[name] = value
99
100     @staticmethod
101     def get_db_value(name, default=None, rettype=None):
102         from daklib.dbconn import DBConfig, DBConn, NoResultFound
103         try:
104             res = DBConn().session().query(DBConfig).filter(DBConfig.name == name).one()
105         except NoResultFound:
106             return default
107
108         if rettype:
109             return rettype(res.value)
110         else:
111             return res.value
112
113     def _setup_routines(self):
114         """
115         This routine is the canonical list of which fields need to exist in
116         the config table.  If your dak instance is to work, we suggest reading it
117
118         Of course, what the values do is another matter
119         """
120         for field in [('db_revision',      None,       int),
121                       ('defaultsuitename', 'unstable', str),
122                       ('exportpath',       '',         str),
123                       ('unprivgroup',      None,       str)
124                       ]:
125             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))
126             setattr(Config, '%s' % field[0], property(fget=getattr(self, 'get_%s' % field[0])))
127
128     def get_defaultsuite(self):
129         from daklib.dbconn import get_suite
130         suitename = self.defaultsuitename
131         if not suitename:
132             return None
133         else:
134             return get_suite(suitename)
135
136     defaultsuite = property(get_defaultsuite)