File: compare-ini.py

package info (click to toggle)
python-tempestconf 3.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: python: 4,530; makefile: 18; sh: 9
file content (83 lines) | stat: -rwxr-xr-x 3,032 bytes parent folder | download | duplicates (2)
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
81
82
83
# Copyright 2018 Red Hat, Inc.
# 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.

# This script is able to compare two ini files specified by arguments, f.e.:
#   $ ./compare-ini.py ./file1.ini ./file2.ini

# If some of the values are actually lists divided by commas, f.e.:
#
# [section]
# key = value1,value2,value3
#
# then this script compares these values regardless their order.

import six
from six.moves import configparser
import sys


def compare_ini_files(conf1, path1, conf2, path2):
    failed = 0
    for section in conf1.sections():
        if not conf2.has_section(section):
            sys.stderr.write("%s doesn't have %s section.\n" %
                             (path2, section))
            failed = 1
            continue
        for (key, value) in conf1.items(section):
            values1 = value.split(',')
            if len(values1) > 1:
                # the value contains a list of strings divided by commas,
                # e.g. api_extensions
                values2 = conf2.get(section, key).split(',')
                for i in values1:
                    if i not in values2:
                        sys.stderr.write("%s value not in %s.%s of %s\n" %
                                         (i, section, key, path2))
                        failed = 1
            else:
                val1 = conf1.get(section, key)
                if not conf2.has_option(section, key):
                    sys.stderr.write("%s doesn't have %s option in %s "
                                     "section.\n" % (path2, key, section))
                    failed = 1
                else:
                    val2 = conf2.get(section, key)
                    if not val1 == val2:
                        sys.stderr.write("%s in %s != %s in %s under %s.%s\n" %
                                         (val1, path1, val2,
                                          path2, section, key))
                        failed = 1
    return failed


if __name__ == '__main__':
    if six.PY3:
        conf1 = configparser.ConfigParser()
        conf2 = configparser.ConfigParser()
    else:
        conf1 = configparser.SafeConfigParser()
        conf2 = configparser.SafeConfigParser()

    conf1.read(sys.argv[1])
    conf2.read(sys.argv[2])

    # compare first file to the other one
    ret = compare_ini_files(conf1, sys.argv[1], conf2, sys.argv[2])

    # compare the other file to the first one
    ret2 = compare_ini_files(conf2, sys.argv[2], conf1, sys.argv[1])

    sys.exit(ret or ret2)