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