view treepkg/readconfig.py @ 47:2802be410156

add config options pbuilderrc and use it when calling pbuilder
author Bernhard Herzog <bh@intevation.de>
date Mon, 02 Apr 2007 20:46:51 +0200
parents 6ced445aa090
children 78cf5f6778ec
line wrap: on
line source
# Copyright (C) 2007 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with the software for details.

"""Reads the configuration file"""

import sys
import shlex
from ConfigParser import SafeConfigParser, NoOptionError

import util

defaults = dict(root_cmd="sudo")

packager_desc = [
    "name", "base_dir", "svn_url", "packager_class",
    ("root_cmd", shlex.split), "pbuilderrc",
    "deb_email", "deb_fullname",
    ]

treepkg_desc = [
    ("check_interval", int),
    ]


def read_config_section(parser, section, item_desc, defaults=None):
    if defaults is None:
        defaults = dict()
    options = dict()
    for item in item_desc:
        if isinstance(item, tuple):
            key, converter = item
        else:
            key = item
            converter = str
        try:
            value = parser.get(section, key, vars=defaults)
            options[key] = converter(value)
        except NoOptionError:
            print >>sys.stderr, "Missing option %r in section %r" \
                  % (key, section)
            sys.exit(1)
    return options


def read_config(filename):
    """Reads the tree packager configuration from the file given by filename.

    The function returns a tuple with a ('treepkg') and a list of dicts
    ('packagers').  The treepkg dict contains the main configuration of
    the tree packager.  The packagers list contains one dict with the
    configuratiin for each packager.
    """
    parser = SafeConfigParser(defaults)
    parser.read([filename])

    # extract packager configurations
    packagers = []
    for section in parser.sections():
        if section.startswith("pkg_"):
            packager_class = parser.get(section, "packager_class")
            module = util.import_dotted_name(packager_class)
            desc = packager_desc + module.PackageLine.extra_config_desc
            packagers.append(read_config_section(parser, section, desc,
                                                 dict(name=section[4:])))

    # main config
    treepkg = read_config_section(parser, "treepkg", treepkg_desc)

    return treepkg, packagers


if __name__ == "__main__":
    import pprint
    print pprint.pprint(read_config(sys.argv[1]))
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)