view treepkg/readconfig.py @ 521:d26642ed6afa

use empty string as default config values instead of None
author Bjoern Ricks <bricks@intevation.de>
date Wed, 10 Nov 2010 16:48:03 +0000
parents f2de1c162d30
children
line wrap: on
line source
# Copyright (C) 2007, 2008, 2009 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
import os.path
from ConfigParser import SafeConfigParser, NoOptionError

import util
import packager

def convert_bool(s):
    s = s.lower()
    if s in ("true", "yes", "1"):
        return True
    if s in ("false", "no", "0"):
        return False
    raise ValueError("cannot determine boolean value of %r" % (s,))


def convert_subversion_subset(raw):
    """Converts the string representation an svn subset into internal form
    The format in the config file is typically:
    url: svn://example.com/repository/trunk
    svn_subset: -N .
                subdir1
                subdir2

    Each line of the svn_subset value consists of an optional flag
    followed by a subdirectory.  Empty lines are ignored.  Each
    non-empty line is split into words using shlex.split, so whitespace
    characters can be included in filenames using e.g. quotes (see the
    shlex documentation for details).  The only flag supported is -N
    which indicates that the subdirectory on that line is not to be
    checked out recursively.  If -N is given on a line, svn checkout
    will be called with -N as parameter.

    The example above will be converted into the internal form [('.',
    False), ('subdir1', True), ('subdir2', True)] used by the
    treepkg.subversion module.
    """
    subset = []
    for line in raw.splitlines():
        split = shlex.split(line)
        if len(split) < 1:
            # ignore empty lines
            continue
        subdir = split[-1]
        recurse = True
        flags = split[:-1]
        if flags:
            if flags == ["-N"]:
                recurse = False
            else:
                raise ValueError("Only -N is supported as flag, but flags = %r"
                                 % (flags,))
        subset.append((subdir, recurse))
    return subset


packager_desc = [
    "name", "base_dir",
    ("url", str, ""),
    ("svn_url", str, ""),
    ("svn_subset", convert_subversion_subset, ""),
    ("svn_externals", shlex.split, ""),
    ("rules_url", str, ""), ("packager_class", str, "treepkg.packager"),
    ("root_cmd", shlex.split, "sudo"), "builderconfig",
    "deb_email", "deb_fullname", ("deb_build_options", str, ""),
    ("version_template", str, "%(revision)s"),
    "pkg_revision_template", ("pkg_basename", str, ""),
    ("handle_dependencies", convert_bool),
    ("signing_key_id", str, ""),
    ("changelog_msg_template", str, "Update to revision %(revision)s"),
    ("branch", str,""),
    ("os", str, ""),
    ("builder_cls", str, "PBuilder"),
    ("status_hook", str, ""),
    ("scm_type", str, "svn"),
    ("rules_scm_type", str, "svn")
    ]

treepkg_desc = [
    ("check_interval", int),
    "instructions_file",
    ("name", str, ""),
    ("treepkg_dir", str, ""),
    ("tracks_dir", str, "")
    ]


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


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

    The function returns a tuple with a dict ('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.
    """
    if not os.path.exists(filename):
        print >>sys.stderr, "Config file %s does not exist" % filename
        sys.exit(2)
    parser = SafeConfigParser()
    parser.read([filename])

    # extract packager configurations
    packagers = []
    for section in parser.sections():
        if section.startswith("pkg_"):
            vars = dict(name=section[4:])
            try:
                packager_class = parser.get(section, "packager_class",
                                            vars=vars)
            except NoOptionError:
                packager_class = "treepkg.packager"
            module = packager.import_packager_module(packager_class)
            desc = packager_desc + module.PackageTrack.extra_config_desc
            packager_options = read_config_section(parser, section, desc,
                                                    defaults=vars)
            if not packager_options.get("url") and \
                not packager_options.get("svn_url"):
                print >>sys.stderr, "Missing repository URL in section %r" \
                                     % (section)
                sys.exit(1)
            packagers.append(packager_options)

    # 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)