summaryrefslogtreecommitdiffstats
path: root/tools/conf/analyze_opts.py
blob: f78169a8ca7c46c55c8ce55d5b89d0f8a4700fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2012, Cloudscaling
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
'''
find_unused_options.py

Compare the nova.conf file with the nova.conf.sample file to find any unused
options or default values in nova.conf
'''
import argparse
import os
import sys

sys.path.append(os.getcwd())
from nova.openstack.common import iniparser


class PropertyCollecter(iniparser.BaseParser):
    def __init__(self):
        super(PropertyCollecter, self).__init__()
        self.key_value_pairs = {}

    def assignment(self, key, value):
        self.key_value_pairs[key] = value

    def new_section(self, section):
        pass

    @classmethod
    def collect_properties(cls, lineiter, sample_format=False):
        def clean_sample(f):
            for line in f:
                if line.startswith("# ") and line != '# nova.conf sample #\n':
                    line = line[2:]
                yield line
        pc = cls()
        if sample_format:
            lineiter = clean_sample(lineiter)
        pc.parse(lineiter)
        return pc.key_value_pairs


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='''Compare the nova.conf
    file with the nova.conf.sample file to find any unused options or
    default values in nova.conf''')

    parser.add_argument('-c', action='store',
                        default='/etc/nova/nova.conf',
                        help='path to nova.conf\
                        (defaults to /etc/nova/nova.conf)')
    parser.add_argument('-s', default='./etc/nova/nova.conf.sample',
                        help='path to nova.conf.sample\
                        (defaults to ./etc/nova/nova.conf.sample')
    options = parser.parse_args()

    conf_file_options = PropertyCollecter.collect_properties(open(options.c))
    sample_conf_file_options = PropertyCollecter.collect_properties(
        open(options.s), sample_format=True)

    for k, v in sorted(conf_file_options.items()):
        if k not in sample_conf_file_options:
            print "Unused:", k
    for k, v in sorted(conf_file_options.items()):
        if k in sample_conf_file_options and v == sample_conf_file_options[k]:
            print "Default valued:", k