1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 SINA Corporation
5
# Author: Zhongyue Luo <lzyeval@gmail.com>
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8
# not use this file except in compliance with the License. You may obtain
9
# a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
# License for the specific language governing permissions and limitations
19
"""Generates a nova.conf file."""
32
_FLOATOPT = "FloatOpt"
34
_MULTISTROPT = "MultiStrOpt"
36
_OPTION_CACHE = list()
37
_OPTION_REGEX = re.compile(r"(%s)" % "|".join([_STROPT, _BOOLOPT, _INTOPT,
41
_BASEDIR = os.path.abspath(os.path.dirname(__file__) + "../../")
46
def mod_prefer(mod_str):
47
prefer = ["flags.py", "log.py", "utils.py", "service.py"]
48
return prefer.index(mod_str) if mod_str in prefer else ord(mod_str[0])
50
def pkg_prefer(pkg_str):
51
prefer = ["auth", "api", "vnc", "ipv6", "network", "compute", "virt",
52
"console", "consoleauth", "image"]
53
return prefer.index(pkg_str) if pkg_str in prefer else ord(pkg_str[0])
55
print '#' * 20 + '\n# nova.conf sample #\n' + '#' * 20
56
# NOTE(lzyeval): sort top level modules and packages
57
# to process modules first
62
for filepath in srcfiles:
63
pkg_name = filepath.split(os.sep)[3]
64
mod_str = '.'.join(['.'.join(filepath.split(os.sep)[2:-1]),
65
os.path.basename(filepath).split('.')[0]])
66
mods = mods_by_pkg.get(pkg_name, list())
68
mods_by_pkg[pkg_name] = mods
70
# NOTE(lzyeval): place top level modules before packages
71
pkg_names = filter(lambda x: x.endswith(_PY_EXT), mods_by_pkg.keys())
72
pkg_names.sort(key=lambda x: mod_prefer(x))
73
ext_names = filter(lambda x: x not in pkg_names, mods_by_pkg.keys())
74
ext_names.sort(key=lambda x: pkg_prefer(x))
75
pkg_names.extend(ext_names)
76
for pkg_name in pkg_names:
77
mods = mods_by_pkg.get(pkg_name)
83
def print_module(mod_str):
86
if mod_str.endswith('.__init__'):
87
mod_str = mod_str[:mod_str.rfind(".")]
90
flags = getattr(sys.modules[mod_str], _FLAGS)
91
except (ValueError, AttributeError), err:
93
except ImportError, ie:
94
sys.stderr.write("%s\n" % str(ie))
98
for opt_name in sorted(flags.keys()):
99
# check if option was processed
100
if opt_name in _OPTION_CACHE:
102
opt_dict = flags._get_opt_info(opt_name)
103
opts.append(opt_dict['opt'])
104
_OPTION_CACHE.append(opt_name)
105
# return if flags has no unique options
108
# print out module info
109
print '######### defined in %s #########' % mod_str
116
def convert_abspath(s):
117
"""Set up a reasonably sensible default for pybasedir."""
118
if not s.startswith(_BASEDIR):
120
return s.replace(_BASEDIR, '/usr/lib/python/site-packages')
126
opt_type = _OPTION_REGEX.search(str(type(opt))).group(0)
127
except (ValueError, AttributeError), err:
128
sys.stderr.write("%s\n" % str(err))
130
# print out option info
131
print "######", "".join(["(", opt_type, ")"]), opt.help
133
name, default = opt.name, opt.default
135
if isinstance(default, basestring):
136
default = convert_abspath(default)
139
print '# %s=<None>' % name
141
if opt_type == 'StrOpt':
142
print '# %s="%s"' % (name, default)
143
elif opt_type == 'ListOpt':
144
print '# %s="%s"' % (name, ','.join(default))
145
elif opt_type == 'MultiStrOpt':
146
for default in default:
147
print '# %s="%s"' % (name, default)
148
elif opt_type == 'BoolOpt':
149
print '# %s=%s' % (name, str(default).lower())
151
print '# %s=%s' % (name, default)
154
if __name__ == '__main__':
155
if len(sys.argv) < 2:
156
print "usage: python %s [srcfile]...\n" % sys.argv[0]
159
print "#", "Total option count: %d" % len(_OPTION_CACHE)