Switch to tar.bz2 file to match native & rename configure source
[fedora-mingw.git] / compare / compare.py
1 #!/usr/bin/python
2
3 import sys
4 from tempfile import mkdtemp
5 from os import mkdir, system
6 import os.path
7 import rpm
8
9
10 def compare_header(refspec, altspec):
11     warnings = []
12
13     refhdr = refspec.packages()[0].header()
14     althdr = altspec.packages()[0].header()
15
16     refname = refhdr[rpm.RPMTAG_NAME]
17     altname = althdr[rpm.RPMTAG_NAME]
18
19     refver = refhdr[rpm.RPMTAG_VERSION]
20     altver = althdr[rpm.RPMTAG_VERSION]
21
22     reflic = refhdr[rpm.RPMTAG_LICENSE]
23     altlic = althdr[rpm.RPMTAG_LICENSE]
24
25     refurl = refhdr[rpm.RPMTAG_URL]
26     alturl = althdr[rpm.RPMTAG_URL]
27
28     if refver != altver:
29         warnings.append("different version: '%s': '%s' != '%s': '%s'" % (refname, refver, altname, altver))
30
31     if reflic != altlic:
32         warnings.append("different license: '%s': '%s' != '%s': '%s'" % (refname, reflic, altname, altlic))
33
34     if refurl != alturl:
35         warnings.append("different URL: '%s': '%s' != '%s': '%s'" % (refname, refurl, altname, alturl))
36
37     return warnings
38
39 def compare_sources(refspec, altspec):
40     warnings = []
41     refsrc = []
42     altsrc = []
43     refsrcname = []
44     altsrcname = []
45     for src in refspec.sources():
46         if src[2] == rpm.RPMBUILD_ISSOURCE:
47             uri = src[0]
48             offset = uri.rfind("/")
49             if offset != -1:
50                 baseuri = uri[0:offset]
51                 srcname = uri[offset+1:]
52             else:
53                 baseuri = ""
54                 srcname = uri
55             refsrc.append([baseuri, srcname])
56             refsrcname.append(srcname)
57
58     for src in altspec.sources():
59         if src[2] == rpm.RPMBUILD_ISSOURCE:
60             uri = src[0]
61             offset = uri.rfind("/")
62             if offset != -1:
63                 baseuri = uri[0:offset]
64                 srcname = uri[offset+1:]
65             else:
66                 baseuri = ""
67                 srcname = uri
68             altsrc.append([baseuri, srcname])
69             altsrcname.append(srcname)
70
71
72     for s in refsrc:
73         if not s[1] in altsrcname:
74             warnings.append("missing source: '%s'" % s[1])
75     for s in altsrc:
76         if not s[1] in refsrcname:
77             warnings.append("extra source: '%s'" % s[1])
78
79     for s1 in refsrc:
80         for s2 in altsrc:
81             if s1[1] != s2[1]:
82                 continue
83             if s1[0] != s2[0]:
84                 warnings.append("different base URI for source '%s': '%s' != '%s'" % (s1[1], s1[0], s2[0]))
85
86     return warnings
87
88
89 def compare_patches(refspec, altspec):
90     warnings = []
91     refpatch = []
92     altpatch = []
93     for src in refspec.sources():
94         if src[2] == rpm.RPMBUILD_ISPATCH:
95             refpatch.append(src[0])
96     for src in altspec.sources():
97         if src[2] == rpm.RPMBUILD_ISPATCH:
98             altpatch.append(src[0])
99
100     for p in refpatch:
101         if not p in altpatch:
102             warnings.append("missing patch '%s'" % p)
103
104     for p in altpatch:
105         if not p in refpatch:
106             warnings.append("extra patch '%s'" % p)
107
108     return warnings
109
110
111 def compare_specs(refspec, altspec):
112     warnings = []
113
114     for w in compare_header(refspec, altspec):
115         warnings.append(w)
116     for w in compare_sources(refspec, altspec):
117         warnings.append(w)
118     for w in compare_patches(refspec, altspec):
119         warnings.append(w)
120
121     return warnings
122
123 def load_suppressions(file):
124     if not os.path.exists(file):
125         return []
126
127     supp = []
128     s = open(suppressionfile)
129     try:
130         while 1:
131             line = s.readline()
132             if not line:
133                 break;
134             if line[0] == '#':
135                 continue
136
137             line = line[0:-1]
138             supp.append(line)
139     finally:
140         s.close()
141
142     return supp
143
144
145
146 scratchdir = mkdtemp("rpm-source-compare")
147
148 if len(sys.argv) != 4:
149     print "syntax: %s REFERENCE-SPEC ALTERNATE-SPEC SUPPRESSIONS" % sys.argv[0]
150     sys.exit(1)
151
152 refspecfile = sys.argv[1]
153 altspecfile = sys.argv[2]
154 suppressionfile = sys.argv[3]
155
156 ts = rpm.ts()
157
158 refspec = ts.parseSpec(refspecfile)
159 altspec = ts.parseSpec(altspecfile)
160 suppressions = load_suppressions(suppressionfile)
161
162
163 warnings = []
164 for w in compare_specs(refspec, altspec):
165     if not w in suppressions:
166         warnings.append(w)
167
168 if len(warnings) == 0:
169     print "PASS"
170 else:
171     for w in warnings:
172         print "WARNING %s" % w