-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff2html
executable file
·422 lines (383 loc) · 14 KB
/
diff2html
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/python
# diff2html
#
# Copyright (C) 2001 Yves Bailly <[email protected]>
# (C) 2001 MandrakeSoft S.A.
#
# This script is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA, or look at the website
# http://www.gnu.org/copyleft/gpl.html
import sys, os, string, re, time, stat
default_css = \
"""
TABLE { border-collapse: collapse; border-spacing: 0px; width: 100%;}
TD.linenum { color: #909090;
text-align: right;
vertical-align: top;
font-weight: bold;
border-right: 1px solid black;
border-left: 1px solid black; }
TD.added { background-color: #DDDDFF; }
TD.modified { background-color: #BBFFBB; }
TD.removed { background-color: #FFCCCC; }
TD.normal { background-color: #FFFFE1; }
"""
def print_usage() :
print """
diff2html - Formats diff(1) output to an HTML page on stdout
http://diff2html.tuxfamily.org
Usage: diff2html [--help] [--only-changes] [--style-sheet file.css]
[diff options] file1 file2
--help This message
--only-changes Do not display lines that have not changed
--style-sheet file.css Use an alternate style sheet, linked to the given file
diff options All other options are passed to diff(1)
Example:
# Basic use
diff2html file1.txt file2.txt > differences.html
# Treat all files as text and compare them line-by-line, even if they do
# not seem to be text.
diff2html -a file1 file2 > differences.html
# The same, but use the alternate style sheet contained in diff_style.css
diff2html --style-sheet diff_style.css -a file1 file2 > differences.html
The default, hard-coded style sheet is the following:%s
Note 1: if you give invalid additionnal options to diff(1), diff2html will
silently ignore this, but the resulting HTML page will be incorrect;
Note 2: for now, diff2html can't be used with standard input, so the diff(1)
'-' option can't be used.
diff2html is released under the GNU GPL.
Feel free to submit bugs or ideas to <[email protected]>.
""" % default_css
def str2html(s) :
s1 = string.replace(string.rstrip(s), "&", "&")
if ( len(s1) == 0 ) : return ( s1 ) ;
s1 = string.replace(s1, "<", "<")
s1 = string.replace(s1, ">", ">")
i = 0
s2 = ""
while ( s1[i] == " " ) :
s2 += " "
i += 1
s2 += s1[i:]
return ( s2 )
def print_footer() :
# And finally, the end of the HTML
print """
<hr/>
<i>Generated by <b>diff2html</b> on %s<br/>
</body>
</html>
""" % (time.asctime(time.gmtime(time.time())))
def get_file_name(fn) :
return ( os.path.basename(fn) )
def missing_file(dir, file1, is_left) :
file1 = dir + '/' + file1
if is_left == 0 :
file2 = file1
file1 = ' '
else:
file2 = ' '
print """
<table>
<tr>
<th> </th>
<th width="45%%"><strong><big>%s</big></strong></th>
<th> </th>
<th> </th>
<th width="45%%"><strong><big>%s</big></strong></th>
</tr>
</table>
<hr/>
""" % (file1, file2)
def diff(file1, file2, only_changes, print_header, print_footer, diff_options) :
if not os.access(file1, os.F_OK) :
print "File %s does not exist or is not readable, aborting." % file1
sys.exit(1)
if not os.access(file2, os.F_OK) :
print "File %s does not exist or is not readable, aborting." % file2
sys.exit(1)
# Invokes "diff"
diff_stdout = os.popen("diff %s %s %s" % (diff_options, file1, file2), "r")
diff_output = diff_stdout.readlines()
diff_stdout.close()
# Maps to store the reported differences
changed = {}
deleted = {}
added = {}
# Magic regular expression
diff_re = re.compile(
r"^(?P<f1_start>\d+)(,(?P<f1_end>\d+))?"+ \
"(?P<diff>[acd])"+ \
"(?P<f2_start>\d+)(,(?P<f2_end>\d+))?")
# Now parse the output from "diff"
for diff_line in diff_output:
diffs = diff_re.match(string.strip(diff_line))
# If the line doesn't match, it's useless for us
if not ( diffs == None ) :
# Retrieving informations about the differences :
# starting and ending lines (may be the same)
f1_start = int(diffs.group("f1_start"))
if ( diffs.group("f1_end") == None ) :
f1_end = f1_start
else :
f1_end = int(diffs.group("f1_end"))
f2_start = int(diffs.group("f2_start"))
if ( diffs.group("f2_end") == None ) :
f2_end = f2_start
else :
f2_end = int(diffs.group("f2_end"))
f1_nb = (f1_end - f1_start) + 1
f2_nb = (f2_end - f2_start) + 1
# Is it a changed (modified) line ?
if ( diffs.group("diff") == "c" ) :
# We have to handle the way "diff" reports lines merged
# or splitted
if ( f2_nb < f1_nb ) :
# Lines merged : missing lines are marqued "deleted"
for lf1 in range(f1_start, f1_start+f2_nb) :
changed[lf1] = 0
for lf1 in range(f1_start+f2_nb, f1_end+1) :
deleted[lf1] = 0
elif ( f1_nb < f2_nb ) :
# Lines splitted : extra lines are marqued "added"
for lf1 in range(f1_start, f1_end+1) :
changed[lf1] = 0
for lf2 in range(f2_start+f1_nb, f2_end+1) :
added[lf2] = 0
else :
# Lines simply modified !
for lf1 in range(f1_start, f1_end+1) :
changed[lf1] = 0
# Is it an added line ?
elif ( diffs.group("diff") == "a" ) :
for lf2 in range(f2_start, f2_end+1):
added[lf2] = 0
else :
# OK, so it's a deleted line
for lf1 in range(f1_start, f1_end+1) :
deleted[lf1] = 0
# Storing the two compared files, to produce the HTML output
f1 = open(file1, "r")
f1_lines = f1.readlines()
f1.close()
f2 = open(file2, "r")
f2_lines = f2.readlines()
f2.close()
# Finding some infos about the files
f1_stat = os.stat(file1)
f2_stat = os.stat(file2)
# Printing the HTML header, and various known informations
# Preparing the links to changes
if ( len(changed) == 0 ) :
changed_lnks = "None"
else :
changed_lnks = ""
keys = changed.keys()
keys.sort()
for key in keys :
changed_lnks += "<a href=\"#F1_%d\">%d</a>, " % (key, key)
changed_lnks = changed_lnks[:-2]
if ( len(added) == 0 ) :
added_lnks = "None"
else :
added_lnks = ""
keys = added.keys()
keys.sort()
for key in keys :
added_lnks += "<a href=\"#F2_%d\">%d</a>, " % (key, key)
added_lnks = added_lnks[:-2]
if ( len(deleted) == 0 ) :
deleted_lnks = "None"
else :
deleted_lnks = ""
keys = deleted.keys()
keys.sort()
for key in keys :
deleted_lnks += "<a href=\"#F1_%d\">%d</a>, " % (key, key)
deleted_lnks = deleted_lnks[:-2]
if print_header :
print """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Differences between %s and %s</title>""" % (file1, file2)
if ( external_css == "" ) :
print " <style>%s</style>" % default_css
else :
print " <link rel=\"stylesheet\" href=\"%s\" type=\"text/css\">" % \
external_css
print """
</head>
<body>
"""
print """
<table>
<tr>
<th> </th>
<th width="45%%"><strong><big>%s</big></strong></th>
<th> </th>
<th> </th>
<th width="45%%"><strong><big>%s</big></strong></th>
</tr>
<tr>
<td width="16"> </td>
<td>
%d lines<br/>
%d bytes<br/>
Last modified : %s<br/>
<hr/>
</td>
<td width="16"> </td>
<td width="16"> </td>
<td>
%d lines<br/>
%d bytes<br/>
Last modified : %s<br/>
<hr/>
</td>
</tr>
""" % (file1, file2,
len(f1_lines), f1_stat[stat.ST_SIZE],
time.asctime(time.gmtime(f1_stat[stat.ST_MTIME])),
len(f2_lines), f2_stat[stat.ST_SIZE],
time.asctime(time.gmtime(f2_stat[stat.ST_MTIME])))
# Running through the differences...
nl1 = nl2 = 0
while not ( (nl1 >= len(f1_lines)) and (nl2 >= len(f2_lines)) ) :
if ( added.has_key(nl2+1) ) :
f2_lines[nl2]
# This is an added line
print """
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_%d">%d</a></td>
<td class="added">%s</td>
</tr>
""" % (nl2+1, nl2+1, str2html(f2_lines[nl2]))
nl2 += 1
elif ( deleted.has_key(nl1+1) ) :
# This is a deleted line
print """
<tr>
<td class="linenum"><a name="F1_%d">%d</a></td>
<td class="removed">%s</td>
<td width="16"> </td>
<td class="linenum"> </td>
<td class="removed"> </td>
</tr>
""" % (nl1+1, nl1+1, str2html(f1_lines[nl1]))
nl1 += 1
elif ( changed.has_key(nl1+1) ) :
# This is a changed (modified) line
print """
<tr>
<td class="linenum"><a name="F1_%d">%d</a></td>
<td class="modified">%s</td>
<td width="16"> </td>
<td class="linenum">%d</td>
<td class="modified">%s</td>
</tr>
""" % (nl1+1, nl1+1, str2html(f1_lines[nl1]),
nl2+1, str2html(f2_lines[nl2]))
nl1 += 1
nl2 += 1
else :
# These lines have nothing special
if ( not only_changes ) :
print """
<tr>
<td class="linenum">%d</td>
<td class="normal">%s</td>
<td width="16"> </td>
<td class="linenum">%d</td>
<td class="normal">%s</td>
</tr>
""" % (nl1+1, str2html(f1_lines[nl1]),
nl2+1, str2html(f2_lines[nl2]))
nl1 += 1
nl2 += 1
print " </table>"
if print_footer :
print_footer
if ( __name__ == "__main__" ) :
# Processes command-line options
cmd_line = string.join(sys.argv)
# First, look for "--help"
if ( len(sys.argv) < 3 ) :
print_usage()
sys.exit(1)
for ind_opt in range(len(sys.argv)) :
if ( sys.argv[ind_opt] == "--help" ) :
print_usage()
sys.exit(0)
external_css = ""
ind_last = -1
only_changes = 0
only_list = 0
argv = tuple(sys.argv)
for ind_opt in range(len(argv)) :
if ( argv[ind_opt] == "--style-sheet" ) :
ind_last = ind_opt + 2
external_css = argv[ind_css+1]
if ( argv[ind_opt] == "--only-changes" ) :
ind_last = ind_opt + 1
only_changes = 1
if ( argv[ind_opt] == "--only-list" ) :
ind_last = ind_opt + 1
only_list = 1
argv = list(argv)
if ( ind_last >= 0 ) :
del argv[0:ind_last]
# Check if both files exists
file1 = argv[-2]
file2 = argv[-1]
diff_options = string.join(filter(lambda x: x.startswith("-"), argv[1:]))
diff_count = 0
if os.path.isdir(file1) :
diff_stdout = os.popen("diff -q %s %s %s" % (diff_options, file1, file2), "r")
diff_output = diff_stdout.readlines()
diff_stdout.close()
diff_re = re.compile(r"^Files (.+?) and (.+?) differ$")
only_re = re.compile(r"^Only in (.+?):\s+(.+)$")
header = 1
for diff_line in diff_output:
m = diff_re.match(string.strip(diff_line))
if not ( m == None ) :
if only_list != 1 :
diff(m.group(1), m.group(2), only_changes, header, 0, diff_options)
else :
print get_file_name(m.group(2))
header = 0
++diff_count
else :
m1 = only_re.match(string.strip(diff_line))
if not ( m1 == None ) :
if only_list != 1 :
if m1.group(1) == file1 :
missing_file (m1.group(1), m1.group(2), 1)
else :
missing_file (m1.group(1), m1.group(2), 0)
else :
if m1.group(1) == file1 :
print "<", get_file_name(m1.group(2))
else :
print ">", get_file_name(m1.group(2))
++diff_count
print_footer
else :
diff(file1, file2, only_changes, 1, 1, diff_options)