config.py 4.9 KB

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