Debian Codegraph @

roles/generate-tempestconf-file-cloud/tasks/compare-ini.py

raw
1 # Copyright 2018 Red Hat, Inc.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 # This script is able to compare two ini files specified by arguments, f.e.:
17 # $ ./compare-ini.py ./file1.ini ./file2.ini
18
19 # If some of the values are actually lists divided by commas, f.e.:
20 #
21 # [section]
22 # key = value1,value2,value3
23 #
24 # then this script compares these values regardless their order.
25
26 import six
27 from six.moves import configparser
28 import sys
29
30
31 def compare_ini_files(conf1, path1, conf2, path2):
32 failed = 0
33 for section in conf1.sections():
34 if not conf2.has_section(section):
35 sys.stderr.write("%s doesn't have %s section.\n" %
36 (path2, section))
37 failed = 1
38 continue
39 for (key, value) in conf1.items(section):
40 values1 = value.split(',')
41 if len(values1) > 1:
42 # the value contains a list of strings divided by commas,
43 # e.g. api_extensions
44 values2 = conf2.get(section, key).split(',')
45 for i in values1:
46 if i not in values2:
47 sys.stderr.write("%s value not in %s.%s of %s\n" %
48 (i, section, key, path2))
49 failed = 1
50 else:
51 val1 = conf1.get(section, key)
52 if not conf2.has_option(section, key):
53 sys.stderr.write("%s doesn't have %s option in %s "
54 "section.\n" % (path2, key, section))
55 failed = 1
56 else:
57 val2 = conf2.get(section, key)
58 if not val1 == val2:
59 sys.stderr.write("%s in %s != %s in %s under %s.%s\n" %
60 (val1, path1, val2,
61 path2, section, key))
62 failed = 1
63 return failed
64
65
66 if __name__ == '__main__':
67 if six.PY3:
68 conf1 = configparser.ConfigParser()
69 conf2 = configparser.ConfigParser()
70 else:
71 conf1 = configparser.SafeConfigParser()
72 conf2 = configparser.SafeConfigParser()
73
74 conf1.read(sys.argv[1])
75 conf2.read(sys.argv[2])
76
77 # compare first file to the other one
78 ret = compare_ini_files(conf1, sys.argv[1], conf2, sys.argv[2])
79
80 # compare the other file to the first one
81 ret2 = compare_ini_files(conf2, sys.argv[2], conf1, sys.argv[1])
82
83 sys.exit(ret or ret2)