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