1#!/usr/local/bin/python3.8
2
3# Tool to compare MPlayer translation files against a base file. Reports
4# conflicting definitions, mismatching arguments, extra definitions
5# not present in the base file and (optionally) missing definitions.
6
7# Written by Uoti Urpala
8
9import sys
10import re
11
12def parse(filename):
13    r = {}
14    f = open(filename)
15    it = iter(f)
16    cur = ''
17    for line in it:
18        line = line.strip()
19        if not line.startswith('#define'):
20            while line and line[-1] == '\\':
21                line = it.next().strip()
22            continue
23        try:
24            _, name, value = line.split(None, 2)
25        except ValueError:
26            if name in r:
27                continue
28        value = value.strip('"')
29        while line[-1] == '\\':
30            line = it.next().strip()
31            value += line.rstrip('\\').strip('"')
32        if name in r:
33            print 'Conflict: ', name
34            print r[name]
35            print value
36            print
37        r[name] = value
38    f.close()
39    return r
40
41def compare(base, other, show_missing=False):
42    r = re.compile('%[^diouxXeEfFgGaAcspn%]*[diouxXeEfFgGaAcspn%]')
43    missing = []
44    for key in base:
45        if key not in other:
46            missing.append(key)
47            continue
48        if re.findall(r, base[key]) != re.findall(r, other[key]):
49            print 'Mismatch: ', key
50            print base[key]
51            print other[key]
52            print
53        del other[key]
54    if other:
55        extra = other.keys()
56        extra.sort()
57        print 'Extra: ', ' '.join(extra)
58    if show_missing and missing:
59        missing.sort()
60        print 'Missing: ', ' '.join(missing)
61
62if len(sys.argv) < 3:
63    print 'Usage:\n'+sys.argv[0]+' [--missing] base_helpfile otherfile1 '\
64          '[otherfile2 ...]'
65    sys.exit(1)
66i = 1
67show_missing = False
68if sys.argv[i] in ( '--missing', '-missing' ):
69    show_missing = True
70    i = 2
71base = parse(sys.argv[i])
72for filename in sys.argv[i+1:]:
73    print '*****', filename
74    compare(base, parse(filename), show_missing)
75    print '\n'
76