~launchpad-pqm/launchpad/devel

4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
1
#! /usr/bin/python2.4
2
# Copyright 2007 Canonical Ltd.  All rights reserved.
3
4
"""Check that all the launchpad.conf files can be loaded.
5
6
Usage hint:
7
6071.1.3 by Curtis Hovey
Removed bogus usage instruction.
8
% utilities/check-configs.py -v
4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
9
"""
10
11
import _pythonpath
12
13
import os
14
import sys
15
import ZConfig
16
import optparse
17
import traceback
18
7465.5.1 by Gary Poster
integrate lazr.config and lazr.delegates
19
from lazr.config import ConfigSchema
20
from lazr.config.interfaces import ConfigErrors
6071.1.1 by Curtis Hovey
Remove all code that tries to load canonical/config/schema.xml.
21
22
# Calculate some landmark paths.
23
import canonical.config
24
here = os.path.dirname(canonical.config.__file__)
25
lazr_schema_file = os.path.join(here, 'schema-lazr.conf')
26
lazr_schema = ConfigSchema(lazr_schema_file)
27
zconfig_schema_file = os.path.join(
28
    here, os.pardir, os.pardir, 'zope/app/server/schema.xml')
29
zconfig_schema = ZConfig.loadSchema(zconfig_schema_file)
30
4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
31
32
def main():
33
    parser = optparse.OptionParser(usage="""\
6071.1.4 by Curtis Hovey
Revised script documentation.
34
%prog [options] [zconfig_overrides]
4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
35
6071.1.4 by Curtis Hovey
Revised script documentation.
36
Parse all launchpad.conf and *-lazr.conf files found under the 'config'
37
directory rooted at the current working directory.  Warn about any
38
problems loading the config file.
4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
39
40
With a specified directory, search there instead of the current working
41
directory for launchpad.conf files.  The search is always recursive.
42
43
The environment variable LPCONFIG can be used to limit the search to only
44
subdirectories of config that match $LPCONFIG, otherwise all are searched.
45
6071.1.4 by Curtis Hovey
Revised script documentation.
46
zconfig_overrides are passed directly to ZConfig.loadConfig().
4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
47
""")
48
    parser.add_option('-v', '--verbose',
49
                      action='count', dest='verbosity',
50
                      help='Increase verbosity')
51
    options, arguments = parser.parse_args()
52
53
    # Are we searching for one config or for all configs?
6071.1.1 by Curtis Hovey
Remove all code that tries to load canonical/config/schema.xml.
54
    directory = os.path.join(here, os.pardir, os.pardir, os.pardir, 'configs')
4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
55
    configs = []
56
    lpconfig = os.environ.get('LPCONFIG')
57
    if lpconfig is None:
58
        for dirpath, dirnames, filenames in os.walk(directory):
59
            for filename in filenames:
6071.1.1 by Curtis Hovey
Remove all code that tries to load canonical/config/schema.xml.
60
                if (filename == 'launchpad.conf'
61
                    or filename.endswith('-lazr.conf')):
4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
62
                    configs.append(os.path.join(dirpath, filename))
63
    else:
6071.1.1 by Curtis Hovey
Remove all code that tries to load canonical/config/schema.xml.
64
        configs.append(os.path.join(directory, lpconfig, 'launchpad.conf'))
65
        configs.append(os.path.join(
66
            directory, lpconfig, 'launchpad-lazr.conf'))
4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
67
68
    # Load each config and report any errors.
69
    summary = []
70
    for config in sorted(configs):
6071.1.1 by Curtis Hovey
Remove all code that tries to load canonical/config/schema.xml.
71
        if config.endswith('launchpad.conf'):
72
            # This is a ZConfig conf file.
73
            try:
74
                root, handlers = ZConfig.loadConfig(
75
                    zconfig_schema, config, arguments)
76
            except ZConfig.ConfigurationSyntaxError, error:
77
                if options.verbosity > 2:
78
                    traceback.print_exc()
79
                elif options.verbosity > 1:
80
                    print error
81
                summary.append((config, False))
82
            else:
83
                summary.append((config, True))
4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
84
        else:
6071.1.1 by Curtis Hovey
Remove all code that tries to load canonical/config/schema.xml.
85
            # This is a lazr.config conf file.
86
            lazr_config = lazr_schema.load(config)
87
            try:
88
                lazr_config.validate()
89
            except ConfigErrors, error:
90
                if options.verbosity > 2:
91
                    messages = '\n'.join([str(er) for er in error.errors])
92
                    print messages
93
                elif options.verbosity > 1:
94
                    print error
95
                summary.append((config, False))
96
            else:
97
                summary.append((config, True))
4895.2.1 by Barry Warsaw
utilities/check-configs.py: A launchpad.conf file checker.
98
99
    prefix_length = len(directory)
100
    for config, status in summary:
101
        path = config[prefix_length + 1:]
102
        if status:
103
            if options.verbosity > 0:
104
                print 'SUCCESS:', path
105
        else:
106
            print 'FAILURE:', path
107
108
    # Return a useful exit code.  0 == all success.
109
    return len([config for config, status in summary if not status])
110
111
112
if __name__ == '__main__':
113
    sys.exit(main())