summaryrefslogtreecommitdiffstats
path: root/doc/gen_pkgdiff.py
blob: 11b380987aee7aaaf6522dd355274d76558408d2 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/python
###############################################################################
#
# Diff two licenses.xml files. There are two cases:
# * Two arguments
#    In this case, the two arguments are the two files to compare
#
# * No arguments
#     In this casem the license files are specified by the following
#     parameters in pardoc-distro.xml:
#     - prev_baseline
#     - prev_lic_file
#     - new_lic_file
#
# The result is presented as three sets:
# 1) Removed
#   Packages present in the previous file, but not in the new.
#
# 2) Added
#   Packages present in the new file, but not in the previous.
#
# 3) Changed
#   Packages present in both files, but with different versions. If more than
#   one version of a package is included, then all difference in the version
#   set causes the package to be listed as changed.
#   E.g.
#    (v1) -> (v2)
#    (v1, v2) -> (v2, v3)
#
# Note that packages with the unchanged version is not listed.
#
# The output is presented as XML code printed to stdout. A summary is printed
# to STDERR at the end.
#
###############################################################################

import os
import sys
import subprocess as sp
import xml.etree.ElementTree as ET
import re

repo_root   = sp.check_output(["git", "rev-parse", "--show-toplevel"]).rstrip()
script_root = os.path.dirname(os.path.realpath(__file__))
param_file  = os.path.join(script_root, "docsrc_common/pardoc-distro.xml")

def get_param(param_name):
    pat = re.compile("%s.*>([^<>]+)</" % param_name)

    with open(param_file) as f:
        for line in f:
            m = pat.search(line)
            if m:
                return m.group(1)

    return None

def get_pkgs(file_spec):
    if file_spec.find(":") >= 0:
        s = sp.check_output(("git", "show", file_spec))
    else:
        f = open(file_spec)
        s = f.read()
        f.close()
        del f

    # ET can't handle some special quotes
    for old, new in (("&rdquor;", "&quot;"), ("&rdquo;", "&quot;")):
        s = s.replace(old, new)

    root = ET.fromstring(s)

    for node in root.iter("section"):
        if "id" in node.attrib:
            if node.attrib["id"] == "licenses_packages":
                break

    else:
        return None

    for node in node:
        if node.tag == "informaltable":
            break
    else:
        return None

    tab = node[0][-1]

    plist = dict()
    for row in tab:
        pname = row[0].text
        pver  = row[1].text
        if not pname in plist:
            plist[pname] = set()

        plist[pname].add(pver)

    return set(plist), plist

#----------------------------------------


if len(sys.argv) == 3:
    new_file, prev_file = sys.argv[1:3]

elif len(sys.argv) == 1:
    prev_baseline = get_param("prev_baseline")
    prev_lic_file = get_param("prev_lic_file")
    new_lic_file  = get_param("new_lic_file")

    if not (prev_baseline and prev_lic_file and new_lic_file):
        print '<?xml version="1.0" encoding="ISO-8859-1"?>'
        print '<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"'
        print '"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">'
        print '<section id="relinfo-package-set-changes">'
        print '  <title>Changes in the Set of Provided Package</title>'
        print '  <para>'
        print '    N/A. No previous baseline defined.'
        print '  </para>'
        print '</section>'
        exit(0)

    new_file = os.path.relpath(os.path.join(repo_root, new_lic_file))
    prev_file = "%s:%s" % (prev_baseline, prev_lic_file)

else:
    sys.stderr.write("Usage:\n")
    sys.stderr.write("  1) %s\n" % sys.argv[0])
    sys.stderr.write("  2) %s " % sys.argv[0])
    sys.stderr.write("<new license file> <old license file>\n")
    sys.stderr.write("\n")
    sys.stderr.write("In case 1, the files are specified using the following\n")
    sys.stderr.write("parameters in pardoc-distro.xml:\n")
    sys.stderr.write("  - prev_baseline\n")
    sys.stderr.write("  - prev_lic_file\n")
    sys.stderr.write("  - new_lic_file\n")
    exit()

sys.stderr.write("New license file  : %s\n" % new_file)
sys.stderr.write("Prev license file : %s\n" % prev_file)

old_pset, old_pdict = get_pkgs(prev_file)
new_pset, new_pdict = get_pkgs(new_file)

added   = new_pset - old_pset # Set subtraction
removed = old_pset - new_pset # Set subtraction
common  = old_pset & new_pset
changed = [ p for p in common if old_pdict[p] != new_pdict[p] ]

print '<?xml version="1.0" encoding="ISO-8859-1"?>'
print '<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"'
print '"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">'
print '<section id="relinfo-package-set-changes">'
print '  <title>Changes in the Set of Provided Package</title>'
print '  <para>'
print '    This section describes changes in the provided packages.'
print '  </para>'
print '  <section id="relinfo-added-packages">'
print '    <title>Added Packages</title>'
print '    <informaltable>'
print '      <tgroup cols="2">'
print '        <thead>'
print '          <row>'
print '            <entry>Package</entry>'
print '            <entry>Version(s)</entry>'
print '          </row>'
print '        </thead>'
print '        <tbody>'

for p in sorted(list(added)):
    print '          <row>'
    print '            <entry>%s</entry>' % p
    print '            <entry>%s</entry>' % ", ".join(sorted(new_pdict[p]))
    print '          </row>'

print '        </tbody>'
print '      </tgroup>'
print '    </informaltable>'
print '  </section>'

print '  <section id="relinfo-removed-packages">'
print '    <title>Removed Packages</title>'
print '    <informaltable>'
print '      <tgroup cols="2">'
print '        <thead>'
print '          <row>'
print '            <entry>Package</entry>'
print '            <entry>Version(s)</entry>'
print '          </row>'
print '        </thead>'
print '        <tbody>'
for p in sorted(list(removed)):
    print '          <row>'
    print '            <entry>%s</entry>' % p
    print '            <entry>%s</entry>' % ", ".join(sorted(old_pdict[p]))
    print '          </row>'
print '        </tbody>'
print '      </tgroup>'
print '    </informaltable>'
print '  </section>'

print '  <section id="relinfo-changed-packages">'
print '    <title>Changed Package Versions</title>'
print '    <informaltable>'
print '      <tgroup cols="3">'
print '        <thead>'
print '          <row>'
print '            <entry>Package</entry>'
print '            <entry>Previous Version(s)</entry>'
print '            <entry>New Version(s)</entry>'
print '          </row>'
print '        </thead>'
print '        <tbody>'
for p in sorted(list(changed)):
    print '          <row>'
    print '            <entry>%s</entry>' % p
    print '            <entry>%s</entry>' % ", ".join(sorted(old_pdict[p]))
    print '            <entry>%s</entry>' % ", ".join(sorted(new_pdict[p]))
    print '          </row>'

print '        </tbody>'
print '      </tgroup>'
print '    </informaltable>'
print '  </section>'
print '</section>'

sys.stderr.write("Package Summary:\n")
sys.stderr.write("  Prev file : %3d\n" % len(old_pset))
sys.stderr.write("  New file  : %3d\n" % len(new_pset))
sys.stderr.write("  Added     : %3d\n" % len(added))
sys.stderr.write("  Removed   : %3d\n" % len(removed))
sys.stderr.write("  Changed   : %3d\n" % len(changed))
sys.stderr.write("  Unchanged : %3d\n" % (len(common) - len(changed)))
sys.stderr.write("Done\n")