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 |
|
14612.2.6
by William Grant
utilities |
14 |
from operator import attrgetter |
15 |
from optparse import OptionParser |
|
6071.2.2
by Curtis Hovey
Changes per review. |
16 |
import os |
17 |
import sys |
|
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 |
|
14612.2.6
by William Grant
utilities |
20 |
from lazr.config import ImplicitTypeSchema |
21 |
||
14605.1.1
by Curtis Hovey
Moved canonical.config to lp.services. |
22 |
import lp.services.config |
6071.2.1
by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file. |
23 |
|
24 |
||
14605.1.1
by Curtis Hovey
Moved canonical.config to lp.services. |
25 |
_schema_dir = os.path.abspath(os.path.dirname(lp.services.config.__file__)) |
6071.2.1
by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file. |
26 |
_root = os.path.dirname(os.path.dirname(os.path.dirname(_schema_dir))) |
27 |
||
28 |
||
29 |
class Configuration: |
|
30 |
"""A lazr.config configuration."""
|
|
31 |
_schema_path = os.path.join(_schema_dir, 'schema-lazr.conf') |
|
32 |
||
8377.4.3
by Jonathan Lange
The constructor shouldn't do anything. |
33 |
def __init__(self, config): |
34 |
self.config = config |
|
35 |
||
36 |
@classmethod
|
|
37 |
def load(cls, conf_path, schema_path=None): |
|
13194.2.1
by Gavin Panella
Change all uses of 'initialise' to 'initialize'. |
38 |
"""Initialize the Configuration.
|
6071.2.1
by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file. |
39 |
|
6071.2.2
by Curtis Hovey
Changes per review. |
40 |
: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. |
41 |
:schema_path: The path to the lazr.config schema that defines
|
42 |
the configuration.
|
|
43 |
"""
|
|
8377.4.3
by Jonathan Lange
The constructor shouldn't do anything. |
44 |
if schema_path is None: |
45 |
schema_path = cls._schema_path |
|
8377.4.2
by Jonathan Lange
Get rid of even more state. |
46 |
schema = ImplicitTypeSchema(schema_path) |
8377.4.3
by Jonathan Lange
The constructor shouldn't do anything. |
47 |
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. |
48 |
|
6071.2.2
by Curtis Hovey
Changes per review. |
49 |
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. |
50 |
"""Return the local path to the file that sets the section key."""
|
8377.4.2
by Jonathan Lange
Get rid of even more state. |
51 |
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. |
52 |
value = section[key] |
53 |
previous_config_data = self.config.data |
|
54 |
# Walk the stack of config_data until a change is found.
|
|
55 |
for config_data in self.config.overlays: |
|
56 |
if (section.name in config_data |
|
57 |
and config_data[section.name][key] != value): |
|
58 |
conf_file_name = previous_config_data.filename |
|
59 |
break
|
|
60 |
previous_config_data = config_data |
|
6071.2.2
by Curtis Hovey
Changes per review. |
61 |
conf_path = os.path.abspath(conf_file_name) |
62 |
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. |
63 |
|
6374.1.1
by Curtis Hovey
Fix config to include google_test_service in every environment. Set |
64 |
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. |
65 |
"""Print all the sections and keys in a configuration.
|
66 |
||
67 |
Print the final state of configuration after all the conf files
|
|
68 |
are loaded.
|
|
7465.5.1
by Gary Poster
integrate lazr.config and lazr.delegates |
69 |
|
6374.1.4
by Curtis Hovey
Added the verbose and section_name params to the docstring. |
70 |
:param verbose: If True, each key has a comment stating where it
|
71 |
was defined.
|
|
72 |
: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. |
73 |
"""
|
74 |
print '# This configuration derives from:' |
|
75 |
for config_data in self.config.overlays: |
|
76 |
print '# %s' % config_data.filename |
|
77 |
print
|
|
78 |
name_key = attrgetter('name') |
|
79 |
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 |
80 |
if section_name is not None and section_name != section.name: |
81 |
continue
|
|
6071.2.1
by Curtis Hovey
Added a utility to list the run-time state of a lazr.config conf file. |
82 |
if count > 0: |
83 |
# Separate sections by a blank line, or two when verbose.
|
|
84 |
print
|
|
85 |
print '[%s]' % section.name |
|
86 |
if verbose and section.optional: |
|
87 |
print '# This section is optional.\n' |
|
88 |
for count, key in enumerate(sorted(section)): |
|
89 |
if verbose: |
|
90 |
if count > 0: |
|
91 |
# Separate keys by a blank line.
|
|
92 |
print
|
|
6071.2.2
by Curtis Hovey
Changes per review. |
93 |
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. |
94 |
print '# Defined in: %s' % conf_file_name |
95 |
print '%s: %s' % (key, section[key]) |
|
96 |
||
97 |
||
98 |
def get_option_parser(): |
|
99 |
"""Return the option parser for this program."""
|
|
6071.2.2
by Curtis Hovey
Changes per review. |
100 |
usage = dedent(""" %prog [options] lazr-config.conf |
101 |
||
102 |
List all the sections and keys in an environment's lazr configuration.
|
|
103 |
The configuration is assembled from the schema and conf files. Verbose
|
|
6374.1.3
by Curtis Hovey
Updated the lsconf.py documentation. |
104 |
annotates each key with the location of the file that set its value.
|
105 |
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. |
106 |
parser = OptionParser(usage=usage) |
107 |
parser.add_option( |
|
108 |
"-l", "--schema", dest="schema_path", |
|
6071.2.2
by Curtis Hovey
Changes per review. |
109 |
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. |
110 |
parser.add_option( |
6071.2.2
by Curtis Hovey
Changes per review. |
111 |
"-v", "--verbose", action="store_true", |
112 |
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 |
113 |
parser.add_option( |
114 |
"-s", "--section", dest="section_name", |
|
115 |
help="restrict the listing to the section") |
|
8377.4.4
by Jonathan Lange
If no file is specified, use the canonical configuration. |
116 |
parser.add_option( |
117 |
'-i', "--instance", dest="instance_name", |
|
118 |
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. |
119 |
return parser |
120 |
||
121 |
||
122 |
def main(argv=None): |
|
123 |
"""Run the command line operations."""
|
|
124 |
if argv is None: |
|
125 |
argv = sys.argv |
|
126 |
parser = get_option_parser() |
|
6071.2.2
by Curtis Hovey
Changes per review. |
127 |
(options, arguments) = parser.parse_args(args=argv[1:]) |
128 |
if len(arguments) == 0: |
|
14605.1.1
by Curtis Hovey
Moved canonical.config to lp.services. |
129 |
canonical_config = lp.services.config.config |
8377.4.4
by Jonathan Lange
If no file is specified, use the canonical configuration. |
130 |
if options.instance_name: |
131 |
canonical_config.setInstance(options.instance_name) |
|
132 |
canonical_config._getConfig() |
|
133 |
configuration = Configuration(canonical_config._config) |
|
134 |
elif len(arguments) == 1: |
|
135 |
conf_path = arguments[0] |
|
136 |
configuration = Configuration.load(conf_path, options.schema_path) |
|
137 |
else: |
|
6071.2.2
by Curtis Hovey
Changes per review. |
138 |
parser.error('Too many arguments.') |
139 |
# Does not return.
|
|
6374.1.1
by Curtis Hovey
Fix config to include google_test_service in every environment. Set |
140 |
configuration.list_config( |
141 |
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. |
142 |
|
143 |
||
144 |
if __name__ == '__main__': |
|
145 |
sys.exit(main()) |