This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
codediff.py
executable file
·839 lines (733 loc) · 27.9 KB
/
codediff.py
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#!/usr/bin/env python
#
# Homepage: http://code.google.com/p/coderev
# License: GPLv2, see "COPYING"
#
'''
Diff two files/directories and produce HTML pages.
Class: CodeDiffer
Method: make_diff()
Exception: CodeDifferError
Following templates could be customized after init:
_index_template
_style_template
_header_info_template
_comments_template
_summary_info_template
_data_rows_template
_diff_data_row_template
_deleted_data_row_template
_added_data_row_template
_footer_info_template
'''
_self_name = 'coderev'
import sys, os, stat, errno, time, re, difflib, filecmp, urllib
_global_dir_ignore_list = (
r'^CVS$',
r'^SCCS$',
r'^\.svn$',
r'^\.repo$',
r'^\.git$',
)
_global_file_ignore_list = (
r'.*\.o$',
r'.*\.swp$',
r'.*\.bak$',
r'.*\.old$',
r'.*~$',
r'^\.cvsignore$',
)
textchars = ''.join(map(chr, [7,8,9,10,12,13,27] + range(0x20, 0x100)))
is_binary_string = lambda bytes: bool(bytes.translate(None, textchars))
def make_title(pathname, width):
'Wrap long pathname to abbreviate name to fit the text width'
if not pathname:
return 'None'
if not width or width <= 0:
title = pathname
elif len(pathname) > width:
if width > 3:
title = '...' + pathname[-(width-3):]
else:
title = pathname[-width:]
else:
title = pathname
return title
def get_lines(file):
'''Return content of file (a list, each is a line)'''
fp = open(file, 'r')
lines = [l.decode('utf-8', errors='replace') for l in fp.readlines()]
fp.close()
return lines
def write_file(file, content):
f = open(file, 'w')
f.write(content.encode('utf-8'))
f.close()
def sdiff_lines(from_lines, to_lines, from_title, to_title, use_context,
wrap_num, context_line):
'''
Generate side by side diff and return html, if use_context is False,
then all context around diff will be output
'''
d = difflib.HtmlDiff(tabsize=8, wrapcolumn=wrap_num)
d._styles += '''
/* customized style */
body { font-family:monospace; font-size: 9pt; }
table.diff {font-family:monospace; border:medium;}'''
html = d.make_file(from_lines, to_lines, from_title, to_title, use_context,
context_line)
return html
def cdiff_lines(from_lines, to_lines, from_name, to_name,
from_date, to_date, context_line):
'cdiff two text, return summary info and html content'
d = difflib.context_diff(from_lines, to_lines, from_name, to_name,
from_date, to_date, context_line)
title = 'Cdiff of %s and %s' % (from_name, to_name)
summary, html = cdiff_to_html(d, title)
return summary, html
def udiff_lines(from_lines, to_lines, from_name, to_name,
from_date, to_date, context_line):
'udiff two texts and return html page'
d = difflib.unified_diff(from_lines, to_lines, from_name, to_name,
from_date, to_date, context_line)
title = 'Udiff of %s and %s' % (from_name, to_name)
html = udiff_to_html(d, title)
return html
def html_filter(s):
return s.replace('&', '&').replace('>', '>').replace('<', '<')
def convert_to_html(src):
"Read file 'src' and convert to html"
html = '<html><head><title>%s</title></head><body>' % src
html += '<pre style="font-family:monospace; font-size:9pt;">'
lines = get_lines(src)
for s in lines:
html += html_filter(s)
html += '</pre></body></html>'
return html
def is_binary_file(file):
'''
Determine a binary file
see http://stackoverflow.com/questions/898669/how-can-i-detect-if-a-file-is-binary-non-text-in-python
'''
return is_binary_string(open(file).read(2014))
def cdiff_to_html(cdiff, title):
'''cdiff is context diff (a list) that generated by difflib.context_diff,
return summary and html page'''
summary = { 'changed': 0, 'added': 0, 'deleted': 0 }
line_pattern = '<span class="%s">%s</span>'
body = ''
old_group = False
for line in cdiff:
n = len(line)
line = html_filter(line)
if n >= 4 and line[0:4] == '*** ':
old_group = True
body += line_pattern % ('fromtitle', line)
elif n >= 4 and line[0:4] == '--- ':
old_group = False
body += line_pattern % ('totitle', line)
elif n >= 2 and line[0:2] == ' ':
body += line_pattern % ('same', line)
elif n >= 2 and line[0:2] == '! ':
body += line_pattern % ('change', line)
if old_group:
summary['changed'] += 1
elif n >= 2 and line[0:2] == '- ':
body += line_pattern % ('delete', line)
summary['deleted'] += 1
elif n >= 2 and line[0:2] == '+ ':
body += line_pattern % ('insert', line)
summary['added'] += 1
elif n >= 15 and line[0:15] == '*' * 15:
body += '<hr>'
else: # shouldn't happen
body += line
html = '''<html><head>
<title>%s</title>
<style type="text/css">
.fromtitle {color:brown; font:bold 11pt;}
.totitle {color:green; font:bold 11pt;}
.same {color:black; font:9pt;}
.change {color:blue; font:9pt;}
.delete {color:brown; font:9pt;}
.insert {color:green; font:9pt;}
</style>
<body>
<pre>%s</pre>
</body>
</head></html>''' % (title, body)
return summary, html
def udiff_to_html(udiff, title):
'''udiff is uniform diff (a list) that generated by difflib.uniform_diff,
return html page'''
line_pattern = '<span class="%s">%s</span>'
body = ''
for line in udiff:
n = len(line)
line = line.replace("&","&").replace(">",">").replace("<","<")
if n >= 4 and line[0:4] == '--- ':
body += line_pattern % ('fromtitle', line)
elif n >= 4 and line[0:4] == '+++ ':
body += line_pattern % ('totitle', line)
elif n >= 1 and line[0] == ' ':
body += line_pattern % ('same', line)
elif n >= 1 and line[0] == '-':
body += line_pattern % ('old', line)
elif n >= 1 and line[0] == '+':
body += line_pattern % ('new', line)
elif n >= 4 and line[0:4] == '@@ -':
body += '<hr>'
body += line_pattern % ('head', line)
else: # shouldn't happen
body += line
html = '''<html><head>
<title>%s</title>
<style type="text/css">
.fromtitle {color:brown; font:bold 11pt;}
.totitle {color:green; font:bold 11pt;}
.head {color:blue; font:bold 9pt;}
.same {color:black; font:9pt;}
.old {color:brown; font:9pt;}
.new {color:green; font:9pt;}
</style>
<body>
<pre>%s</pre>
</body>
</head></html>''' % (title, body)
return html
def strip_prefix(name, p=0):
'''strip NUM slashes, like patch(1) -pNUM
eg1: /foo/bar/a/b/x.c
-p0 gives orignal name (no change)
-p1 gives foo/bar/a/b/x.c
-p2 gives bar/a/b/x.c
-p9 gives x.c
eg2: foo/bar/a/b/x.c
-p0 gives orignal name (no change)
-p1 gives bar/a/b/x.c
-p2 gives a/b/x.c
-p9 gives x.c
eg3: ./foo/bar/a/b/x.c
-p0 gives orignal name (no change)
-p1 gives foo/bar/a/b/x.c
-p2 gives bar/a/b/x.c
-p9 gives x.c
'''
cur = 0
tail = len(name) - 1
while p > 0:
index = name.find('/', cur)
#print 'p:', p, 'cur:', cur, 'index:', index
if index == -1: break
while index <= tail and name[index] == '/':
index += 1
cur = index
p -= 1
return name[cur:]
class CodeDifferError(Exception):
pass
class Pager:
def __init__(self, numitem):
self._numitem=numitem
self.pages=['']
self._count = 0
def add(self, item):
if not item: return
if item=='': return
self._count += 1
if self._count >= self._numitem:
self._count = 0
self.pages.append('')
s=self.pages.pop()+item
self.pages.append(s)
class CodeDiffer:
# index page layout (templates are public):
#
# h1: dir1 vs dir2
# comments:
# comments here
# summary of files
# Changed Deleted Added
# Filename C/D/A Summary Diffs Sources
# Pathname x/y/z Cdiff Udiff Sdiff Fdiff Old New
# Pathname x/y/z Cdiff Udiff Sdiff Fdiff Old New
# Pathname x/y/z - - - - - New
# Pathname x/y/z - - - - Old -
# <hr>
# footer_info
#
########## templates begin ##########
_index_template = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>
%(title)s
</title>
<style type="text/css">
%(styles)s
</style>
</head>
<body>
%(header_info)s
%(comments_info)s
%(summary_info)s
%(index_div)s
%(data_rows)s
<hr>
%(footer_info)s
%(index_div)s
</body>
</html>"""
# TODO: provide as template
_style_template = """
body {font-family: monospace; font-size: 9pt;}
.comments { margin-left: 20px; font-stlye: italic; }
#summary_table {
text-align:left;font-family:monospace;
border: 1px solid #ccc; border-collapse: collapse
}
td {padding-left:5px;padding-right:5px;}
#summary {margin-left: 16px; border:medium;text-align:center;}
#footer_info {color:#333; font-size:8pt;}
.diff {background-color:#ffd;}
.added {background-color:#afa;}
.deleted {background-color:#faa;}
.table_header th {
text-align:center;
background-color:#f0f0f0;
border-bottom: 1px solid #aaa;
padding: 4px 4px 4px 4px;
}
li { display: inline; background-color:#eee; margin:.2em;padding:.2em;zoom:1; }
"""
_index_sub_template = """
<li><a href='index%04i.html'>%04i</a></li>
"""
_index_gen_template = """
<div><ul>
%s
</ul></div>
"""
_header_info_template = """
<h1>%(header)s</h1>"""
_comments_template = """
<p><b>Comments:</b></p>
<pre class="comments">%(comments)s</pre>"""
_summary_info_template = """
<p><b>Summary of file changes:</b></p>
<table id="summary">
<tr>
<td class="diff">%(changed)d Changed</td>
<td class="deleted">%(deleted)d Deleted</td>
<td class="added">%(added)d Added</td>
</tr>
</table><br>"""
_data_rows_template = """
<table id="summary_table" cellspacing="1" border="1" nowrap="nowrap">
<tr class="table_header">
<th>Filename</th>
<th><abbr title="Changed/Deleted/Added">C/D/A</abbr> Summary</th>
<th colspan="4">Diffs</th>
<th colspan="2">Sources</th>
</tr>
%(data_rows)s
</table>"""
_same_data_row_template = """
<tr class="same">
<td>%(pathname)s</td>
<td>-/-/-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td><a href="%(pathname_url)s-.html" title="old file">Old</a></td>
<td><a href="%(pathname_url)s.html" title="new file">New</a></td>
</tr>"""
_diff_data_row_template = """
<tr class="diff">
<td>%(pathname)s</td>
<td><abbr title="Changed/Deleted/Added">\
%(changed)s/%(deleted)s/%(added)s</abbr></td>
<td><a href="%(pathname_url)s.cdiff.html" title="context diff">Cdiff</a>\
</td>
<td><a href="%(pathname_url)s.udiff.html" title="unified diff">Udiff</a>\
</td>
<td><a href="%(pathname_url)s.sdiff.html" title="side-by-side context diff">\
Sdiff</a></td>
<td><a href="%(pathname_url)s.fdiff.html" title="side-by-side full diff">\
Fdiff</a></td>
<td><a href="%(pathname_url)s-.html" title="old file">Old</a></td>
<td><a href="%(pathname_url)s.html" title="new file">New</a></td>
</tr>"""
_deleted_data_row_template = """
<tr class="deleted">
<td>%(pathname)s</td>
<td>-/-/-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td><a href="%(pathname_url)s-.html" title="old file">Old</a></td>
<td>-</td>
</tr>"""
_added_data_row_template = """
<tr class="added">
<td>%(pathname)s</td>
<td>-/-/-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td><a href="%(pathname_url)s.html" title="new file">New</a></td>
</tr>"""
_footer_info_template = """
<i id="footer_info">
Generated by %(myname)s at %(time)s
</i>"""
########## templates end ##########
def __init__(self, obj1, obj2, output, input_list=None, strip_level=0,
wrap_num=0, context_line=3, title='', pager=1000,
comments='', show_common_files = False):
self.__obj1 = obj1
self.__obj2 = obj2
self.__output = output
self.__input_list = input_list
self.__strip_level = strip_level
self.__wrap_num = wrap_num
self.__context_line = context_line
self.__file_list = []
self.__title = title
self.__comments = comments
self.__pager=Pager(pager)
self.__show_common_files = show_common_files
# TODO: provide options
self.__dir_ignore_list = _global_dir_ignore_list
self.__file_ignore_list = _global_file_ignore_list
def __diff_file(self):
'''
Generate side by side diff in html and write to output file, if
context_line is 0, then all context around diff will be output
'''
from_lines = get_lines(self.__obj1)
to_lines = get_lines(self.__obj2)
from_title = make_title(self.__obj1, self.__wrap_num)
to_title = make_title(self.__obj2, self.__wrap_num)
use_context = self.__context_line != 0
html = sdiff_lines(from_lines, to_lines, from_title, to_title,
use_context, self.__wrap_num, self.__context_line)
write_file(self.__output, html)
def __is_igore_dir(self, dir):
for pat in self.__dir_ignore_list:
if re.match(pat, dir):
return True
return False
def __is_igore_file(self, file):
for pat in self.__file_ignore_list:
if re.match(pat, file):
return True
return False
def __grab_dir(self, dir):
'Get file list of dir, and remove unwanted file from the list'
flist = []
while dir[-1] == '/': # remove unwanted trailling slash
dir = dir[:-1]
prefix = dir + '/' # os.path.sep
plen = len(prefix)
for root, dirs, files in os.walk(dir):
for d in [k for k in dirs]:
if self.__is_igore_dir(d):
dirs.remove(d)
for f in files:
if not self.__is_igore_file(f):
name = os.path.join(root, f)
flist.append(name[plen:])
return flist
def __make_file_list(self):
'Read file list from input file or stdin or get from obj1 and obj2'
file_list = []
if self.__input_list:
if self.__input_list == '-':
file_list = sys.stdin.readlines()
else:
f = open(self.__input_list, 'r')
file_list = f.readlines()
f.close()
for i in file_list:
s = strip_prefix(i, self.__strip_level).rstrip()
self.__file_list.append(s)
else:
a = self.__grab_dir(self.__obj1)
b = self.__grab_dir(self.__obj2)
self.__file_list = list(set(a)|set(b))
print "total %s files to check"%len(self.__file_list)
def __diff_dir_by_list(self):
data_row = ''
summary = { 'changed': 0, 'added': 0, 'deleted': 0 }
file_summary = { 'changed': 0, 'added': 0, 'deleted': 0 }
has_diff = False
self.__file_list.sort()
for f in self.__file_list:
f_url = urllib.quote(f)
# set default values
from_lines = ''
to_lines = ''
from_date = ''
to_date = ''
target = os.path.join(self.__output, f)
obj1 = os.path.join(self.__obj1, f)
obj2 = os.path.join(self.__obj2, f)
# make output dir and sub dir
try:
os.makedirs(os.path.join(self.__output, os.path.dirname(f)))
except OSError, e:
if e.errno != errno.EEXIST:
raise CodeDifferError, 'OSError: ' + str(e)
stat1 = None
stat2 = None
if os.path.exists(obj1): stat1 = os.lstat(obj1)
if os.path.exists(obj2): stat2 = os.lstat(obj2)
if stat1 and not stat2: # deleted
print ' * %-40s |' % f,
print 'File removed',
#st_mtime = time.localtime(stat1[8])
if not stat.S_ISREG(stat1[0]) or is_binary_file(obj1):
print '(skipped dir/special/binary)'
continue
print
write_file(target + '-.html', convert_to_html(obj1))
data_row = self._deleted_data_row_template % {'pathname': f, 'pathname_url': f_url}
summary['deleted'] += 1
has_diff = True
elif not stat1 and stat2: # added
print ' * %-40s |' % f,
print 'New file',
if not stat.S_ISREG(stat2[0]) or is_binary_file(obj2):
print '(skipped special/binary)'
continue
print
write_file(target + '.html', convert_to_html(obj2))
data_row = self._added_data_row_template % {'pathname': f, 'pathname_url': f_url}
summary['added'] += 1
has_diff = True
elif stat1 and stat2: # same or diff
# do not compare special or binary file
if not stat.S_ISREG(stat1[0]) or is_binary_file(obj1):
print ' * %-40s |' % f,
print '(skipped, former file is special)'
continue
if not stat.S_ISREG(stat2[0]) or is_binary_file(obj2):
print ' * %-40s |' % f,
print '(skipped, latter file is binary)'
continue
if not self.__show_common_files:
if filecmp.cmp(obj1, obj2):
continue
has_diff = True
from_date = time.ctime(stat1[8])
to_date = time.ctime(stat2[8])
from_lines = get_lines(obj1)
to_lines = get_lines(obj2)
# Cdiff
file_summary, html = cdiff_lines(from_lines, to_lines, obj1,
obj2, from_date, to_date, self.__context_line)
write_file(target + '.cdiff.html', html)
# Udiff
html = udiff_lines(from_lines, to_lines, obj1, obj2, from_date,
to_date, self.__context_line)
write_file(target + '.udiff.html', html)
# Sdiff
html = sdiff_lines(from_lines, to_lines, obj1, obj2, True,
self.__wrap_num, self.__context_line)
write_file(target + '.sdiff.html', html)
# Fdiff
html = sdiff_lines(from_lines, to_lines, obj1, obj2, False,
self.__wrap_num, self.__context_line)
write_file(target + '.fdiff.html', html)
print ' * %-40s |' % f,
print 'Changed/Deleted/Added: %d/%d/%d' % (\
file_summary['changed'],
file_summary['deleted'],
file_summary['added'])
write_file(target + '-.html', convert_to_html(obj1))
write_file(target + '.html', convert_to_html(obj2))
if file_summary['changed'] == \
file_summary['deleted'] == \
file_summary['added'] == 0:
template = self._same_data_row_template
else:
template = self._diff_data_row_template
data_row = template % dict(
pathname = f,
pathname_url = f_url,
changed = file_summary['changed'],
deleted = file_summary['deleted'],
added = file_summary['added'],
)
summary['changed'] += 1
else: # this case occured when controlled by master file list
print ' * %-40s |' % f,
print 'Not found'
data_row=''
self.__pager.add(data_row)
if not has_diff:
return False
# Generate footer info
footer_info = self._footer_info_template % dict(
time = time.strftime('%a %b %d %X %Z %Y', time.localtime()),
myname = _self_name,
)
# now wirte index page
if self.__title:
title = html_filter(self.__title)
else:
title = '%s vs %s' % (self.__obj1, self.__obj2)
header_info = self._header_info_template % {'header': title}
# index of index generation
pages = len(self.__pager.pages)
if pages > 1:
index_div = ""
for i in range(pages):
index_div+=self._index_sub_template%(i,i)
index_div=self._index_gen_template%index_div
else:
index_div=""
# index pages generation
ix = 0
for p in self.__pager.pages:
pagename="index%04i.html"%ix
if pages == 1 : pagename='index.html'
index = open(os.path.join(self.__output, pagename), 'w')
index.write(self._index_template % dict(
title = title,
styles = self._style_template,
header_info = header_info,
comments_info = self._comments_template % \
{'comments': html_filter(self.__comments)},
summary_info = self._summary_info_template % summary,
data_rows = self._data_rows_template % {'data_rows': p},
footer_info = footer_info,
index_div = index_div,
))
index.close()
ix += 1
def __diff_dir(self):
self.__make_file_list()
self.__diff_dir_by_list()
def make_diff(self):
try:
# Note: use stat instead lstat to permit symbolic links
stat1 = os.stat(self.__obj1)[0]
stat2 = os.stat(self.__obj2)[0]
if stat.S_ISREG(stat1) and stat.S_ISREG(stat2):
self.__diff_file()
elif stat.S_ISDIR(stat1) and stat.S_ISDIR(stat2):
self.__diff_dir()
else:
e = '%s and %s are of different type, aborted' % \
(self.__obj1, self.__obj2)
raise CodeDifferError, e
except OSError, e:
raise CodeDifferError, 'OSError: ' + str(e)
except IOError, e:
raise CodeDifferError, 'IOError: ' + str(e)
#
# Test code
#
if __name__ == '__main__':
def warn_overwrite(pathname):
'Warnning for overwriting, return True if answered yes, False if no'
msg = "`%s' exists, are you sure you want to overwrite it (yes/no)? "
while True:
sys.stderr.write(msg % pathname)
answer = raw_input('')
if answer == 'yes':
return True
elif answer == 'no':
return False
# else: prompt again
import optparse
usage = '''
%(name)s [options] OLD NEW
%(name)s OLD NEW [options]
Diff two files/directories and produce HTML pages.''' % \
{'name': os.path.basename(sys.argv[0])}
parser = optparse.OptionParser(usage)
parser.add_option('-c', '--context', action='store_true',
dest='context', default=False,
help='generate context diff (default is full diff),' + \
' only take effect when diffing two files')
parser.add_option('-F', '--commentfile', dest='commentfile', metavar='FILE',
help='specify a file to read comments')
parser.add_option('-f', '--filelist', dest='filelist', metavar='FILE',
help='specify a file list to read from, filelist can ' + \
'be generated by find -type f, specify - to read' + \
' from stdin')
parser.add_option('-m', '--comments', dest='comments',
help='specify inline comments (precedes -F)')
parser.add_option('-n', '--lines', dest='lines',
type='int', metavar='NUM', default=3,
help='specify context line count when generating ' + \
'context diff or unified diff, default is 3')
parser.add_option('-o', '--output', dest='output',
help='specify output file or directory name')
parser.add_option('-p', '--striplevel', dest='striplevel',
type='int', metavar='NUM',
help='for all pathnames in the filelist, delete NUM ' + \
'path name components from the beginning of each' + \
' path name, it is similar to patch(1) -p')
parser.add_option('-t', '--title', dest='title',
help='specify title of output index page')
parser.add_option('-w', '--wrap', dest='wrapnum',
type='int', metavar='WIDTH',
help='specify column number where lines are broken ' + \
'and wrapped for sdiff, default is no line wrapping')
parser.add_option('-y', '--yes', action='store_true',
dest='overwrite', default=False,
help='do not prompt for overwriting')
parser.add_option('-P', '--pager', dest='pager',
type='int', metavar='NUM', default=1000,
help='specify maximum number of files listed in index page.' + \
'if there\'s more, additionnal indexnn.html will be generated.')
opts, args = parser.parse_args()
if len(args) != 2:
sys.stderr.write("Sorry, you must specify two file/directory names\n" \
+ "type `%s -h' for help\n" % _self_name)
sys.exit(1)
if not opts.output:
sys.stderr.write("Sorry, you must specify output name (use `-o')\n")
sys.exit(2)
if opts.comments:
comments = opts.comments
elif opts.commentfile:
try:
comments = ''.join(get_lines(opts.commentfile))
except IOError, e:
sys.stderr.write(str(e) + '\n')
sys.exit(1)
else:
comments = ''
if not opts.overwrite and os.path.exists(opts.output):
if opts.filelist == '-':
# stdin redirected, so we cannot read answer from stdin
print "`%s' exists, please select another output directory, " \
"or specify '-y' to force overwriting." % opts.output
sys.exit(1)
else:
if not warn_overwrite(opts.output):
sys.exit(1)
try:
differ = CodeDiffer(args[0], args[1], opts.output, opts.filelist,
opts.striplevel, opts.wrapnum, opts.lines,
opts.title, opts.pager, comments)
differ.make_diff()
except CodeDifferError, e:
sys.stderr.write(str(e) + '\n')
sys.exit(1)
else:
sys.exit(0)
# vim:set et sts=4 sw=4 tw=80:
# EOF