~launchpad-pqm/launchpad/devel

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