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