config.py 5.0 KB

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