Package daklib :: Module config
[hide private]
[frames] | no frames]

Source Code for Module daklib.config

  1  """ 
  2  Config access class 
  3   
  4  @contact: Debian FTPMaster <ftpmaster@debian.org> 
  5  @copyright: 2008  Mark Hymers <mhy@debian.org> 
  6  @license: GNU General Public License version 2 or later 
  7  """ 
  8   
  9  # This program is free software; you can redistribute it and/or modify 
 10  # it under the terms of the GNU General Public License as published by 
 11  # the Free Software Foundation; either version 2 of the License, or 
 12  # (at your option) any later version. 
 13   
 14  # This program is distributed in the hope that it will be useful, 
 15  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 16  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 17  # GNU General Public License for more details. 
 18   
 19  # You should have received a copy of the GNU General Public License 
 20  # along with this program; if not, write to the Free Software 
 21  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 22   
 23  ################################################################################ 
 24   
 25  # <NCommander> mhy, how about "Now with 20% more monty python references" 
 26   
 27  ################################################################################ 
 28   
 29  import getpass 
 30  import grp 
 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 47 -def which_conf_file():
48 return os.getenv("DAK_CONFIG", default_config)
49
50 51 -class Config:
52 """ 53 A Config object is a singleton containing 54 information about the DAK configuration 55 """ 56 57 __shared_state = {} 58
59 - def __init__(self, *args, **kwargs):
60 self.__dict__ = self.__shared_state 61 62 if not getattr(self, 'initialised', False): 63 self.initialised = True 64 self._readconf() 65 self._setup_routines()
66
67 - def _readconf(self):
68 apt_pkg.init() 69 70 self.Cnf = apt_pkg.Configuration() 71 72 apt_pkg.read_config_file_isc(self.Cnf, which_conf_file()) 73 74 # Check whether our dak.conf was the real one or 75 # just a pointer to our main one 76 fqdn = socket.getfqdn() 77 conffile = self.Cnf.get("Config::" + fqdn + "::DakConfig") 78 if conffile: 79 apt_pkg.read_config_file_isc(self.Cnf, conffile) 80 81 # Read group-specific options 82 if 'ByGroup' in self.Cnf: 83 bygroup = self.Cnf.subtree('ByGroup') 84 groups = set([os.getgid()]) 85 groups.update(os.getgroups()) 86 87 for group in bygroup.list(): 88 gid = grp.getgrnam(group).gr_gid 89 if gid in groups: 90 if bygroup.get(group): 91 apt_pkg.read_config_file_isc(self.Cnf, bygroup[group]) 92 break 93 94 # Read user-specific options 95 byuser_config_path = self.Cnf.get(f"ByUser::{getpass.getuser()}", "") 96 if byuser_config_path: 97 apt_pkg.read_config_file_isc(self.Cnf, byuser_config_path) 98 99 if 'Include' in self.Cnf: 100 for filename in self.Cnf.value_list('Include'): 101 apt_pkg.read_config_file_isc(self.Cnf, filename) 102 103 # Rebind some functions 104 # TODO: Clean this up 105 self.get = self.Cnf.get 106 self.subtree = self.Cnf.subtree 107 self.value_list = self.Cnf.value_list 108 self.find = self.Cnf.find 109 self.find_b = self.Cnf.find_b 110 self.find_i = self.Cnf.find_i
111
112 - def __contains__(self, name):
113 return name in self.Cnf
114
115 - def __getitem__(self, name):
116 return self.Cnf[name]
117
118 - def __setitem__(self, name, value):
119 self.Cnf[name] = value
120 121 @staticmethod
122 - def get_db_value(name, default=None, rettype=None):
123 from daklib.dbconn import DBConfig, DBConn, NoResultFound 124 try: 125 res = DBConn().session().query(DBConfig).filter(DBConfig.name == name).one() 126 except NoResultFound: 127 return default 128 129 if rettype: 130 return rettype(res.value) 131 else: 132 return res.value
133
134 - def _setup_routines(self):
135 """ 136 This routine is the canonical list of which fields need to exist in 137 the config table. If your dak instance is to work, we suggest reading it 138 139 Of course, what the values do is another matter 140 """ 141 for field in [('db_revision', None, int), 142 ('defaultsuitename', 'unstable', str), 143 ('use_extfiles', None, int) 144 ]: 145 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)) 146 setattr(Config, '%s' % field[0], property(fget=getattr(self, 'get_%s' % field[0])))
147
148 - def get_defaultsuite(self):
149 from daklib.dbconn import get_suite 150 suitename = self.defaultsuitename 151 if not suitename: 152 return None 153 else: 154 return get_suite(suitename)
155 156 defaultsuite = property(get_defaultsuite)
157