File: zconfig

package info (click to toggle)
zope2.13 2.13.22-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 38,644 kB
  • ctags: 38,805
  • sloc: python: 196,395; xml: 90,515; ansic: 24,121; sh: 916; makefile: 333; perl: 37
file content (101 lines) | stat: -rwxr-xr-x 2,821 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (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 validity 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;
                    this must be specified.

Each file named on the command line is checked for syntactical errors
and schema conformance.  The schema must be specified.  If no files
are specified and standard input is not a TTY, standard in is treated
as a configuration file.  Specifying a schema and no configuration
files causes the schema to be checked.

"""

import optparse
import sys
import os

if __name__ == "__main__":
    here = os.path.dirname(os.path.realpath(__file__))
    swhome = os.path.dirname(here)
    for parts in [("src",), ("lib", "python"), ("Lib", "site-packages")]:
        d = os.path.join(swhome, *(parts + ("ZConfig",)))
        if os.path.isdir(d):
            d = os.path.join(swhome, *parts)
            sys.path.insert(0, d)
            break

import ZConfig


def main():
    optparser = optparse.OptionParser(
        usage="usage: %prog [-s FILE] [file ...]")
    optparser.add_option(
        "-s", "--schema", dest="schema",
        help="use the schema in FILE (can be a URL)",
        metavar="FILE")
    options, args = optparser.parse_args()

    if not options.schema:
        print >>sys.stderr, "No schema specified, but is required."
        usage(sys.stderr)
        return 2
    schema = ZConfig.loadSchema(options.schema)

    if not args:
        if sys.stdin.isatty():
            # just checking the schema
            return 0
        else:
            # stdin is a pipe
            args = ["-"]

    errors = 0
    for fn in args:
        try:
            if fn == "-":
                ZConfig.loadConfigFile(schema, sys.stdin)
            else:
                ZConfig.loadConfig(schema, 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__


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