bh@0: # Copyright (C) 2007 by Intevation GmbH bh@0: # Authors: bh@0: # Bernhard Herzog bh@0: # bh@0: # This program is free software under the GPL (>=v2) bh@0: # Read the file COPYING coming with the software for details. bh@0: bh@0: """Reads the configuration file""" bh@0: bh@0: import sys bh@2: import shlex bh@0: from ConfigParser import SafeConfigParser, NoOptionError bh@0: bh@4: import util bh@4: bh@0: defaults = dict(root_cmd="sudo") bh@0: bh@2: packager_desc = [ bh@4: "name", "base_dir", "svn_url", "packager_class", bh@47: ("root_cmd", shlex.split), "pbuilderrc", bh@2: "deb_email", "deb_fullname", bh@2: ] bh@2: bh@2: treepkg_desc = [ bh@2: ("check_interval", int), bh@2: ] bh@2: bh@2: bh@2: def read_config_section(parser, section, item_desc, defaults=None): bh@2: if defaults is None: bh@2: defaults = dict() bh@2: options = dict() bh@2: for item in item_desc: bh@2: if isinstance(item, tuple): bh@2: key, converter = item bh@2: else: bh@2: key = item bh@2: converter = str bh@2: try: bh@2: value = parser.get(section, key, vars=defaults) bh@2: options[key] = converter(value) bh@2: except NoOptionError: bh@17: print >>sys.stderr, "Missing option %r in section %r" \ bh@17: % (key, section) bh@2: sys.exit(1) bh@2: return options bh@0: bh@0: bh@0: def read_config(filename): bh@2: """Reads the tree packager configuration from the file given by filename. bh@0: bh@2: The function returns a tuple with a ('treepkg') and a list of dicts bh@2: ('packagers'). The treepkg dict contains the main configuration of bh@2: the tree packager. The packagers list contains one dict with the bh@2: configuratiin for each packager. bh@0: """ bh@0: parser = SafeConfigParser(defaults) bh@0: parser.read([filename]) bh@2: bh@2: # extract packager configurations bh@2: packagers = [] bh@0: for section in parser.sections(): bh@0: if section.startswith("pkg_"): bh@4: packager_class = parser.get(section, "packager_class") bh@4: module = util.import_dotted_name(packager_class) bh@52: desc = packager_desc + module.PackageTrack.extra_config_desc bh@4: packagers.append(read_config_section(parser, section, desc, bh@2: dict(name=section[4:]))) bh@0: bh@2: # main config bh@2: treepkg = read_config_section(parser, "treepkg", treepkg_desc) bh@0: bh@2: return treepkg, packagers bh@0: bh@0: bh@0: if __name__ == "__main__": bh@2: import pprint bh@2: print pprint.pprint(read_config(sys.argv[1]))