~ubuntu-cloud-archive/ubuntu/precise/nova/trunk

« back to all changes in this revision

Viewing changes to tools/conf/create_conf.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Adam Gandelman
  • Date: 2012-06-22 12:39:57 UTC
  • mfrom: (1.1.57)
  • Revision ID: package-import@ubuntu.com-20120622123957-hbzwg84nt9rqwg8r
Tags: 2012.2~f2~20120621.14517-0ubuntu1
[ Chuck Short ]
* New upstream version.

[ Adam Gandelman ]
* debian/rules: Temporarily disable test suite while blocking
  tests are investigated. 
* debian/patches/kombu_tests_timeout.patch: Dropped.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2012 SINA Corporation
4
 
# All Rights Reserved.
5
 
# Author: Zhongyue Luo <lzyeval@gmail.com>
6
 
#
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
10
 
#
11
 
#         http://www.apache.org/licenses/LICENSE-2.0
12
 
#
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
17
 
#    under the License.
18
 
 
19
 
"""Generates a nova.conf file."""
20
 
 
21
 
import os
22
 
import re
23
 
import sys
24
 
 
25
 
 
26
 
_PY_EXT = ".py"
27
 
_FLAGS = "FLAGS"
28
 
 
29
 
_STROPT = "StrOpt"
30
 
_BOOLOPT = "BoolOpt"
31
 
_INTOPT = "IntOpt"
32
 
_FLOATOPT = "FloatOpt"
33
 
_LISTOPT = "ListOpt"
34
 
_MULTISTROPT = "MultiStrOpt"
35
 
 
36
 
_OPTION_CACHE = list()
37
 
_OPTION_REGEX = re.compile(r"(%s)" % "|".join([_STROPT, _BOOLOPT, _INTOPT,
38
 
                                               _FLOATOPT, _LISTOPT,
39
 
                                               _MULTISTROPT]))
40
 
 
41
 
_BASEDIR = os.path.abspath(os.path.dirname(__file__) + "../../")
42
 
 
43
 
 
44
 
def main(srcfiles):
45
 
 
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])
49
 
 
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])
54
 
 
55
 
    print '#' * 20 + '\n# nova.conf sample #\n' + '#' * 20
56
 
    # NOTE(lzyeval): sort top level modules and packages
57
 
    #                to process modules first
58
 
    print
59
 
    print '[DEFAULT]'
60
 
    print
61
 
    mods_by_pkg = dict()
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())
67
 
        if not mods:
68
 
            mods_by_pkg[pkg_name] = mods
69
 
        mods.append(mod_str)
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)
78
 
        mods.sort()
79
 
        for mod_str in mods:
80
 
            print_module(mod_str)
81
 
 
82
 
 
83
 
def print_module(mod_str):
84
 
    opts = list()
85
 
    flags = None
86
 
    if mod_str.endswith('.__init__'):
87
 
        mod_str = mod_str[:mod_str.rfind(".")]
88
 
    try:
89
 
        __import__(mod_str)
90
 
        flags = getattr(sys.modules[mod_str], _FLAGS)
91
 
    except (ValueError, AttributeError), err:
92
 
        return
93
 
    except ImportError, ie:
94
 
        sys.stderr.write("%s\n" % str(ie))
95
 
        return
96
 
    except Exception, e:
97
 
        return
98
 
    for opt_name in sorted(flags.keys()):
99
 
        # check if option was processed
100
 
        if opt_name in _OPTION_CACHE:
101
 
            continue
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
106
 
    if not opts:
107
 
        return
108
 
    # print out module info
109
 
    print '######### defined in %s #########' % mod_str
110
 
    print
111
 
    for opt in opts:
112
 
        print_opt(opt)
113
 
    print
114
 
 
115
 
 
116
 
def convert_abspath(s):
117
 
    """Set up a reasonably sensible default for pybasedir."""
118
 
    if not s.startswith(_BASEDIR):
119
 
        return s
120
 
    return s.replace(_BASEDIR, '/usr/lib/python/site-packages')
121
 
 
122
 
 
123
 
def print_opt(opt):
124
 
    opt_type = None
125
 
    try:
126
 
        opt_type = _OPTION_REGEX.search(str(type(opt))).group(0)
127
 
    except (ValueError, AttributeError), err:
128
 
        sys.stderr.write("%s\n" % str(err))
129
 
        sys.exit(1)
130
 
    # print out option info
131
 
    print "######", "".join(["(", opt_type, ")"]), opt.help
132
 
 
133
 
    name, default = opt.name, opt.default
134
 
 
135
 
    if isinstance(default, basestring):
136
 
        default = convert_abspath(default)
137
 
 
138
 
    if default is None:
139
 
        print '# %s=<None>' % name
140
 
    else:
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())
150
 
        else:
151
 
            print '# %s=%s' % (name, default)
152
 
 
153
 
 
154
 
if __name__ == '__main__':
155
 
    if len(sys.argv) < 2:
156
 
        print "usage: python %s [srcfile]...\n" % sys.argv[0]
157
 
        sys.exit(0)
158
 
    main(sys.argv[1:])
159
 
    print "#", "Total option count: %d" % len(_OPTION_CACHE)