~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
8452.3.3 by Karl Fogel
* utilities/: Add copyright header block to source files that were
2
#
8687.15.2 by Karl Fogel
In files modified by r8688, change "<YEARS>" to "2009", as per
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
8687.15.3 by Karl Fogel
Shorten the copyright header block to two lines.
4
# GNU Affero General Public License version 3 (see the file LICENSE).
6071.2.2 by Curtis Hovey
Changes per review.
5
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
6
"""Create lazr.config schema and confs from ZConfig data."""
6071.2.2 by Curtis Hovey
Changes per review.
7
8
__metatype__ = type
9
9641.1.4 by Gary Poster
more updates for _pythonpath. Still need to update all subprocess calls to python to use -S; still need to update z3c.recipe.filetemplate to provide sys.modules from -S run.
10
# Scripts may have relative imports.
11
# pylint: disable-msg=W0403
12
import _pythonpath
6071.2.2 by Curtis Hovey
Changes per review.
13
14
import os
15
import sys
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
16
from optparse import OptionParser
17
from operator import attrgetter
6071.2.2 by Curtis Hovey
Changes per review.
18
from textwrap import dedent
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
19
20
import canonical.config
7465.5.1 by Gary Poster
integrate lazr.config and lazr.delegates
21
from lazr.config import ImplicitTypeSchema
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
22
23
24
_schema_dir = os.path.abspath(os.path.dirname(canonical.config.__file__))
25
_root = os.path.dirname(os.path.dirname(os.path.dirname(_schema_dir)))
26
27
28
class Configuration:
29
    """A lazr.config configuration."""
30
    _schema_path = os.path.join(_schema_dir, 'schema-lazr.conf')
31
8377.4.3 by Jonathan Lange
The constructor shouldn't do anything.
32
    def __init__(self, config):
33
        self.config = config
34
35
    @classmethod
36
    def load(cls, conf_path, schema_path=None):
13194.2.1 by Gavin Panella
Change all uses of 'initialise' to 'initialize'.
37
        """Initialize the Configuration.
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
38
6071.2.2 by Curtis Hovey
Changes per review.
39
        :conf_path: The path to the lazr.config conf file.
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
40
        :schema_path: The path to the lazr.config schema that defines
41
            the configuration.
42
        """
8377.4.3 by Jonathan Lange
The constructor shouldn't do anything.
43
        if schema_path is None:
44
            schema_path = cls._schema_path
8377.4.2 by Jonathan Lange
Get rid of even more state.
45
        schema = ImplicitTypeSchema(schema_path)
8377.4.3 by Jonathan Lange
The constructor shouldn't do anything.
46
        return cls(schema.load(conf_path))
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
47
6071.2.2 by Curtis Hovey
Changes per review.
48
    def config_file_for_value(self, section, key):
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
49
        """Return the local path to the file that sets the section key."""
8377.4.2 by Jonathan Lange
Get rid of even more state.
50
        conf_file_name = self.config.schema.filename
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
51
        value = section[key]
52
        previous_config_data = self.config.data
53
        # Walk the stack of config_data until a change is found.
54
        for config_data in self.config.overlays:
55
            if (section.name in config_data
56
                and config_data[section.name][key] != value):
57
                conf_file_name = previous_config_data.filename
58
                break
59
            previous_config_data = config_data
6071.2.2 by Curtis Hovey
Changes per review.
60
        conf_path = os.path.abspath(conf_file_name)
61
        return conf_path[len(_root) + 1:]
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
62
6374.1.1 by Curtis Hovey
Fix config to include google_test_service in every environment. Set
63
    def list_config(self, verbose=False, section_name=None):
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
64
        """Print all the sections and keys in a configuration.
65
66
        Print the final state of configuration after all the conf files
67
        are loaded.
7465.5.1 by Gary Poster
integrate lazr.config and lazr.delegates
68
6374.1.4 by Curtis Hovey
Added the verbose and section_name params to the docstring.
69
        :param verbose: If True, each key has a comment stating where it
70
            was defined.
71
        :param section_name: Only print the named section.
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
72
        """
73
        print '# This configuration derives from:'
74
        for config_data in self.config.overlays:
75
            print '#     %s' % config_data.filename
76
        print
77
        name_key = attrgetter('name')
78
        for count, section in enumerate(sorted(self.config, key=name_key)):
6374.1.1 by Curtis Hovey
Fix config to include google_test_service in every environment. Set
79
            if section_name is not None and section_name != section.name:
80
                continue
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
81
            if count > 0:
82
                # Separate sections by a blank line, or two when verbose.
83
                print
84
            print '[%s]' % section.name
85
            if verbose and section.optional:
86
                print '# This section is optional.\n'
87
            for count, key in enumerate(sorted(section)):
88
                if verbose:
89
                    if count > 0:
90
                        # Separate keys by a blank line.
91
                        print
6071.2.2 by Curtis Hovey
Changes per review.
92
                    conf_file_name = self.config_file_for_value(section, key)
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
93
                    print '# Defined in: %s' % conf_file_name
94
                print '%s: %s' % (key, section[key])
95
96
97
def get_option_parser():
98
    """Return the option parser for this program."""
6071.2.2 by Curtis Hovey
Changes per review.
99
    usage = dedent("""    %prog [options] lazr-config.conf
100
101
    List all the sections and keys in an environment's lazr configuration.
102
    The configuration is assembled from the schema and conf files. Verbose
6374.1.3 by Curtis Hovey
Updated the lsconf.py documentation.
103
    annotates each key with the location of the file that set its value.
104
    The 'section' option limits the list to just the named section.""")
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
105
    parser = OptionParser(usage=usage)
106
    parser.add_option(
107
        "-l", "--schema", dest="schema_path",
6071.2.2 by Curtis Hovey
Changes per review.
108
        help="the path to the lazr.config schema file")
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
109
    parser.add_option(
6071.2.2 by Curtis Hovey
Changes per review.
110
        "-v", "--verbose", action="store_true",
111
        help="explain where the section and keys are set")
6374.1.1 by Curtis Hovey
Fix config to include google_test_service in every environment. Set
112
    parser.add_option(
113
        "-s", "--section", dest="section_name",
114
        help="restrict the listing to the section")
8377.4.4 by Jonathan Lange
If no file is specified, use the canonical configuration.
115
    parser.add_option(
116
        '-i', "--instance", dest="instance_name",
117
        help="the configuration instance to use")
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
118
    return parser
119
120
121
def main(argv=None):
122
    """Run the command line operations."""
123
    if argv is None:
124
        argv = sys.argv
125
    parser = get_option_parser()
6071.2.2 by Curtis Hovey
Changes per review.
126
    (options, arguments) = parser.parse_args(args=argv[1:])
127
    if len(arguments) == 0:
8377.4.4 by Jonathan Lange
If no file is specified, use the canonical configuration.
128
        canonical_config = canonical.config.config
129
        if options.instance_name:
130
            canonical_config.setInstance(options.instance_name)
131
        canonical_config._getConfig()
132
        configuration = Configuration(canonical_config._config)
133
    elif len(arguments) == 1:
134
        conf_path = arguments[0]
135
        configuration = Configuration.load(conf_path, options.schema_path)
136
    else:
6071.2.2 by Curtis Hovey
Changes per review.
137
        parser.error('Too many arguments.')
138
        # Does not return.
6374.1.1 by Curtis Hovey
Fix config to include google_test_service in every environment. Set
139
    configuration.list_config(
140
        verbose=options.verbose, section_name=options.section_name)
6071.2.1 by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file.
141
142
143
if __name__ == '__main__':
144
    sys.exit(main())