[Zope3-checkins] CVS: Packages/ZConfig/scripts - zconfig:1.1

Fred L. Drake, Jr. fred@zope.com
Wed, 15 Jan 2003 12:23:22 -0500


Update of /cvs-repository/Packages/ZConfig/scripts
In directory cvs.zope.org:/tmp/cvs-serv3674

Added Files:
	zconfig 
Log Message:
Command line tool for configuration checking.  This can check any
number of configuration files against a single schema, or for simple
syntactical errors if no schema is specified (using the old-style
schema support; has limitations if the config is meant to be used with
a schema).


=== Added File Packages/ZConfig/scripts/zconfig ===
#! /usr/local/bin/python
##############################################################################
#
# Copyright (c) 2002, 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

"""zconfig:  Script to check well-formedness of a configuration file.

Usage:

    zconfig [options] [file...]

Options:

    -h
    --help          Print this help text.

    -s file
    --schema file   Use the schema in 'file' to validate the configuration.

Each file named on the command line is checked for syntactical errors
and schema conformance (when the schema is specified).
"""

import getopt
import sys

import ZConfig
import ZConfig.Context
import ZConfig.loader


def main():
    loader = ClassicLoader()
    schema = None
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hs:", ["help", "schema="])
    except getopt.GetoptError, e:
        print >>sys.stderr, e
        usage(sys.stderr)
        return 2

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage(sys.stdout)
            return 0
        if opt in ("-s", "--schema"):
            schema = ZConfig.loadSchema(arg)
            loader = ZConfig.loader.ConfigLoader(schema)

    if not args:
        if sys.stdin.isatty():
            print >>sys.stderr, "No configuration files specified."
            usage(sys.stderr)
            return 2
        else:
            # stdin is a pipe
            args = ["-"]

    errors = 0
    for fn in args:
        try:
            if fn == "-":
                loader.loadFile(sys.stdin)
            else:
                loadFile.loadURL(fn)
        except ZConfig.ConfigurationError, e:
            print >>sys.stderr, str(e)
            errors += 1

    if errors:
        return 1
    else:
        return 0


def usage(fp):
    print >>fp, __doc__


class ClassicLoader(ZConfig.Context.Context):
    def loadResource(self, resource):
        # create a temporary context since it's not reusable
        context = ZConfig.Context.Context()
        return context.loadResource(resource)


if __name__ == "__main__":
    sys.exit(main())