-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathroberteldersoftwarediff.py
executable file
·1596 lines (1389 loc) · 77.4 KB
/
roberteldersoftwarediff.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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# Copyright 2017 Robert Elder Software Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import time
import sys
import argparse
import codecs
import unicodedata
import platform
import subprocess
import signal
import traceback
import ctypes
codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)
MISSING_BYTE_ORDER_MARKER_EXIT_CODE = 100
TERMINAL_WIDTH_ERROR_EXIT_CODE = 101
COMMON_PREFIX_ERROR_EXIT_CODE = 102
FILE_OPEN_FAIL_ERROR_EXIT_CODE = 103
INVALID_MAX_LINE_LENGTH_ERROR_EXIT_CODE = 104
UNIX_INSERTION_COLOUR = 42
UNIX_DELETION_COLOUR = 41
UNIX_CHANGE_COLOUR = 44
UNIX_CORRECT_COLOUR = 32
UNIX_INCORRECT_COLOUR = 31
INSERTION_COLOUR = 1
DELETION_COLOUR = 2
CHANGE_COLOUR = 3
CORRECT_COLOUR = 4
INCORRECT_COLOUR = 5
GLOBAL_RUN_PARAMS = None # For use in signal handler.
def make_unix_terminal_interface(rp):
class UnixTerminalInterface(object):
def __init__(self, rp):
self.semi = e_encode(u";", rp.output_encoding, "internal")
self.esc = e_encode(u"\033", rp.output_encoding, "internal")
self.m = e_encode(u"m", rp.output_encoding, "internal")
self.open_sq = e_encode(u"[", rp.output_encoding, "internal")
self.zero = e_encode(u"0", rp.output_encoding, "internal")
self.rp = rp
self.terminal_width = None
try:
p = subprocess.Popen(["stty", "size"], stdout=subprocess.PIPE)
out, err = p.communicate()
if p.returncode == 0:
self.terminal_width = int((out.split()[1]))
except:
pass
self.num_colours_supported = None
try:
p = subprocess.Popen(["tput", "colors"], stdout=subprocess.PIPE)
out, err = p.communicate()
if p.returncode == 0:
self.num_colours_supported = int(out)
except:
pass
def unix_colour_lookup(self, c):
if c == INSERTION_COLOUR:
return UNIX_INSERTION_COLOUR
elif c == DELETION_COLOUR:
return UNIX_DELETION_COLOUR
elif c == CHANGE_COLOUR:
return UNIX_CHANGE_COLOUR
elif c == CORRECT_COLOUR:
return UNIX_CORRECT_COLOUR
elif c == INCORRECT_COLOUR:
return UNIX_INCORRECT_COLOUR
else:
raise Exception("Unknown unix colour." + str(c))
def set_terminal_colours(self, colours):
colours_encoded_strings = [as_byte_string(str(self.unix_colour_lookup(k)), self.rp.output_encoding, "internal") for k in colours]
colours_with_semi = self.semi.join(colours_encoded_strings)
output_bytes(self.esc + self.open_sq + colours_with_semi + self.m, self.rp)
def reset_terminal_colours(self):
output_bytes(self.esc + self.open_sq + self.zero + self.m, self.rp)
def as_unicode(self):
rtn = u""
# Used for debugging
rtn += u"terminal_width: " + py23_str(self.terminal_width, self.rp.output_encoding, "internal") + self.rp.output_newline
rtn += u"num_colours_supported: " + py23_str(self.num_colours_supported, self.rp.output_encoding, "internal") + self.rp.output_newline
return rtn
def output_test(self):
nl = self.rp.output_newline
output_bytes(e_encode(u"Begin Unix Terminal Interface Test" + nl, self.rp.output_encoding, "internal"), self.rp)
colours = [INSERTION_COLOUR, DELETION_COLOUR, CHANGE_COLOUR, CORRECT_COLOUR, INCORRECT_COLOUR]
colours_n = [u"INSERTION_COLOUR", u"DELETION_COLOUR", u"CHANGE_COLOUR", u"CORRECT_COLOUR", u"INCORRECT_COLOUR"]
for i in range(0,len(colours)):
# Flush any previous output
sys.stdout.flush()
self.set_terminal_colours([colours[i]])
output_bytes(e_encode(u"This text should be coloured with " + colours_n[i], self.rp.output_encoding, "internal"), self.rp)
sys.stdout.flush()
self.reset_terminal_colours()
output_bytes(e_encode(nl, self.rp.output_encoding, "internal"), self.rp)
out_c = u"U"
str_width = py23_str(self.terminal_width, self.rp.output_encoding, "internal")
output_bytes(e_encode(u"The next line should exactly fill the terminal width with " + str_width + u" '" + out_c + u"' characters:" + nl, self.rp.output_encoding, "internal"), self.rp)
if self.terminal_width is not None and self.terminal_width > 0:
for i in range(0, self.terminal_width):
output_bytes(e_encode(out_c, self.rp.output_encoding, "internal"), self.rp)
return UnixTerminalInterface(rp)
def make_windows_terminal_interface(rp):
try:
from ctypes import windll, wintypes
try:
class COORD(ctypes.Structure):
_fields_ = [
("x", ctypes.c_short),
("y", ctypes.c_short)
]
class RECT(ctypes.Structure):
_fields_ = [
("left", ctypes.c_short),
("top", ctypes.c_short),
("right", ctypes.c_short),
("bottom", ctypes.c_short)
]
class CSBI(ctypes.Structure):
_fields_ = [
("dwSize", COORD),
("dwCursorPosition", COORD),
("wAttributes", ctypes.c_ushort),
("srWindow", RECT),
("dwMaximumWindowSize", COORD)
]
class WindowsTerminalInterface(object):
def __init__(self, rp):
self.rp = rp
self.WINDOWS_INSERTION_COLOUR = 0x0020
self.WINDOWS_DELETION_COLOUR = 0x0040
self.WINDOWS_CHANGE_COLOUR = 0x0010
self.WINDOWS_CORRECT_COLOUR = 0x0002
self.WINDOWS_INCORRECT_COLOUR = 0x0004
self.original_colour_mask = 0xFFFFFF0F # Erase background colour, and keep current font colour.
# Define the foreign function interfaces (required in Python 3)
windll.Kernel32.GetStdHandle.restype = ctypes.c_ulong
windll.kernel32.GetStdHandle.argtypes = [ctypes.c_ulong]
windll.kernel32.GetConsoleScreenBufferInfo.restype = ctypes.c_ulong
windll.kernel32.GetConsoleScreenBufferInfo.argtypes = [wintypes.HANDLE, ctypes.POINTER(CSBI)]
windll.kernel32.SetConsoleTextAttribute.restype = ctypes.c_ulong
windll.kernel32.SetConsoleTextAttribute.argtypes = [wintypes.HANDLE, ctypes.c_ushort]
windll.kernel32.GetLastError.restype = ctypes.c_ulong
# Setup stream handles
self.STD_INPUT_HANDLE_NUMBER = ctypes.c_ulong(0xfffffff6) # -10
self.STD_OUTPUT_HANDLE_NUMBER = ctypes.c_ulong(0xfffffff5) # -11
self.STD_ERROR_HANDLE_NUMBER = ctypes.c_ulong(0xfffffff4) # -12
self.STD_INPUT_HANDLE = windll.kernel32.GetStdHandle(self.STD_INPUT_HANDLE_NUMBER)
self.STD_OUTPUT_HANDLE = windll.kernel32.GetStdHandle(self.STD_OUTPUT_HANDLE_NUMBER)
self.STD_ERROR_HANDLE = windll.kernel32.GetStdHandle(self.STD_ERROR_HANDLE_NUMBER)
self.csbi = CSBI()
self.GetConsoleScreenBufferInfo_result_code = windll.kernel32.GetConsoleScreenBufferInfo(self.STD_OUTPUT_HANDLE, self.csbi)
if self.GetConsoleScreenBufferInfo_result_code > 0:
# Save this so we can reset it later
self.csbi_wAttributes_original = self.csbi.wAttributes
self.terminal_width = int(self.csbi.dwSize.x)
self.GetConsoleScreenBufferInfo_last_error = None
else:
self.csbi_wAttributes_original = None
self.terminal_width = None
self.GetConsoleScreenBufferInfo_last_error = windll.kernel32.GetLastError()
def windows_colour_lookup(self, c):
if c == INSERTION_COLOUR:
return self.WINDOWS_INSERTION_COLOUR
elif c == DELETION_COLOUR:
return self.WINDOWS_DELETION_COLOUR
elif c == CHANGE_COLOUR:
return self.WINDOWS_CHANGE_COLOUR
elif c == CORRECT_COLOUR:
return self.WINDOWS_CORRECT_COLOUR
elif c == INCORRECT_COLOUR:
return self.WINDOWS_INCORRECT_COLOUR
else:
raise Exception("Unknown windows colour." + str(c))
def set_terminal_colours(self, colours):
new_colour = self.csbi_wAttributes_original & self.original_colour_mask
for c in colours:
new_colour = new_colour | self.windows_colour_lookup(c)
windll.kernel32.SetConsoleTextAttribute(self.STD_OUTPUT_HANDLE, ctypes.c_ushort(new_colour))
def reset_terminal_colours(self):
windll.kernel32.SetConsoleTextAttribute(self.STD_OUTPUT_HANDLE, ctypes.c_ushort(self.csbi_wAttributes_original))
def as_unicode(self):
nl = self.rp.output_newline
rtn = u""
# Used for debugging
rtn += u"STD_INPUT_HANDLE_NUMBER: " + py23_str(self.STD_INPUT_HANDLE_NUMBER, self.rp.output_encoding, "internal") + nl
rtn += u"STD_OUTPUT_HANDLE_NUMBER: " + py23_str(self.STD_OUTPUT_HANDLE_NUMBER, self.rp.output_encoding, "internal") + nl
rtn += u"STD_ERROR_HANDLE_NUMBER: " + py23_str(self.STD_ERROR_HANDLE_NUMBER, self.rp.output_encoding, "internal") + nl
rtn += u"STD_INPUT_HANDLE: " + py23_str(self.STD_INPUT_HANDLE, self.rp.output_encoding, "internal") + nl
rtn += u"STD_OUTPUT_HANDLE: " + py23_str(self.STD_OUTPUT_HANDLE, self.rp.output_encoding, "internal") + nl
rtn += u"STD_ERROR_HANDLE: " + py23_str(self.STD_ERROR_HANDLE, self.rp.output_encoding, "internal") + nl
rtn += u"original_colour_mask: " + py23_str(self.original_colour_mask, self.rp.output_encoding, "internal") + nl
rtn += u"GetConsoleScreenBufferInfo_result_code (0 means error): " + py23_str(self.GetConsoleScreenBufferInfo_result_code, self.rp.output_encoding, "internal") + nl
rtn += u"GetConsoleScreenBufferInfo_last_error: " + py23_str(self.GetConsoleScreenBufferInfo_last_error, self.rp.output_encoding, "internal") + nl
rtn += u"csbi_wAttributes_original: " + py23_str(self.csbi_wAttributes_original, self.rp.output_encoding, "internal") + nl
rtn += u"terminal_width: " + py23_str(self.terminal_width, self.rp.output_encoding, "internal") + nl
return rtn
def output_test(self):
nl = self.rp.output_newline
output_bytes(e_encode(u"Begin Windows Terminal Interface Test" + nl, self.rp.output_encoding, "internal"), self.rp)
colours = [INSERTION_COLOUR, DELETION_COLOUR, CHANGE_COLOUR, CORRECT_COLOUR, INCORRECT_COLOUR]
colours_n = [u"INSERTION_COLOUR", u"DELETION_COLOUR", u"CHANGE_COLOUR", u"CORRECT_COLOUR", u"INCORRECT_COLOUR"]
if self.csbi_wAttributes_original:
for i in range(0,len(colours)):
sys.stdout.flush()
self.set_terminal_colours([colours[i]])
output_bytes(e_encode(u"This text should be coloured with " + colours_n[i], self.rp.output_encoding, "internal"), self.rp)
sys.stdout.flush()
self.reset_terminal_colours()
output_bytes(e_encode(self.rp.output_newline, self.rp.output_encoding, "internal"), self.rp)
out_c = u"W"
str_width = py23_str(self.terminal_width, self.rp.output_encoding, "internal")
output_bytes(e_encode(u"The next line should exactly fill the terminal width with " + str_width + u" '" + out_c + u"' characters:" + self.rp.output_newline, self.rp.output_encoding, "internal"), self.rp)
if self.terminal_width is not None and self.terminal_width > 0:
for i in range(0, self.terminal_width):
output_bytes(e_encode(out_c, self.rp.output_encoding, "internal"), self.rp)
else:
output_bytes(e_encode(u"csbi_wAttributes_original not set: Skipping output test." + nl, self.rp.output_encoding, "internal"), self.rp)
return WindowsTerminalInterface(rp)
except Exception as e:
traceback.print_exc() # If import worked, but there was a problem, we want to know about it.
except:
return None # Don't worry about the windows interface if import fails
try: # Only works on unix
signal.signal(signal.SIGPIPE,signal.SIG_DFL) # Don't throw exceptions when piping.
except:
pass
def do_graceful_exit(rp, exit_code):
if rp and hasattr(rp, "use_windows_terminal_colours") and rp.use_windows_terminal_colours:
try:
rp.windows_terminal_interface.reset_terminal_colours()
except:
pass
if rp and hasattr(rp, "use_ansi") and rp.use_ansi:
try:
rp.unix_terminal_interface.reset_terminal_colours()
except:
pass
if rp and hasattr(rp, "outfile_f") and rp.outfile_f is not None:
try:
# Close the output file
if rp.outfile_f is not None:
rp.outfile_f.close()
except:
pass
sys.exit(exit_code)
def on_sigint_handler(signal, frame):
global GLOBAL_RUN_PARAMS
do_graceful_exit(GLOBAL_RUN_PARAMS, 0)
signal.signal(signal.SIGINT, on_sigint_handler)
disable_implicit_encoding = True # Used to help catch implicit encoding in python2
default_encoding = sys.getdefaultencoding()
if disable_implicit_encoding:
try:
reload(sys)
sys.setdefaultencoding("undefined")
except:
pass
def diff(e, f, i=0, j=0):
# Documented at http://blog.robertelder.org/diff-algorithm/
N,M,L,Z = len(e),len(f),len(e)+len(f),2*min(len(e),len(f))+2
if N > 0 and M > 0:
w,g,p = N-M,[0]*Z,[0]*Z
for h in range(0, (L//2+(L%2!=0))+1):
for r in range(0, 2):
c,d,o,m = (g,p,1,1) if r==0 else (p,g,0,-1)
for k in range(-(h-2*max(0,h-M)), h-2*max(0,h-N)+1, 2):
a = c[(k+1)%Z] if (k==-h or k!=h and c[(k-1)%Z]<c[(k+1)%Z]) else c[(k-1)%Z]+1
b = a-k
s,t = a,b
while a<N and b<M and e[(1-o)*N+m*a+(o-1)]==f[(1-o)*M+m*b+(o-1)]:
a,b = a+1,b+1
c[k%Z],z=a,-(k-w)
if L%2==o and z>=-(h-o) and z<=h-o and c[k%Z]+d[z%Z] >= N:
D,x,y,u,v = (2*h-1,s,t,a,b) if o==1 else (2*h,N-a,M-b,N-s,M-t)
if D > 1 or (x != u and y != v):
return diff(e[0:x],f[0:y],i,j)+diff(e[u:N],f[v:M],i+u,j+v)
elif M > N:
return diff([],f[N:M],i+N,j+N)
elif M < N:
return diff(e[M:N],[],i+M,j+M)
else:
return []
elif N > 0: # Modify the return statements below if you want a different edit script format
return [{"operation": "delete", "position_old": i+n} for n in range(0,N)]
else:
return [{"operation": "insert", "position_old": i,"position_new":j+n} for n in range(0,M)]
err_source = None
err_counts = None
def initialize_error_counts_object(oldfile_encoding, oldfile_as_binary, newfile_encoding, newfile_as_binary, output_encoding, parameters_encoding):
global err_counts
err_counts = {
"parameters": {
"src" : u"Command line parameters",
"dst" : parameters_encoding,
"count" : 0
},
"oldfile": {
"src" : u"binary oldfile" if oldfile_as_binary else oldfile_encoding,
"dst" : output_encoding,
"count" : 0
},
"newfile": {
"src" : u"binary newfile" if newfile_as_binary else newfile_encoding,
"dst" : output_encoding,
"count" : 0
},
"internal": {
"src" : u"internal constants",
"dst" : output_encoding,
"count" : 0
}
}
def ignore_errors(e):
err_counts[err_source]["count"] += 1
return (u"", e.end) # Skip this character
def e_encode(s, enc, err):
global err_source
err_source = err # So we can properly attribute where the error came from.
return codecs.encode(s, enc, "ignore")
def e_decode(s, enc, err):
global err_source
err_source = err # So we can properly attribute where the error came from.
return codecs.decode(s, enc, "ignore")
codecs.register_error("ignore", ignore_errors)
def group_unicode_characters(s):
# This function groups together unicode 'characters' that are
# really one unicode 'character'. This is done specifically to
# consider cases where multiple surrogate characters that are split
# in certain versions/configurations of python.
# For example,
# len([c for c in u"\U0001D517"]) == 1 in Python 2.7.12 on Linux
# len([c for c in u"\U0001D517"]) == 2 in Python 2.7.13 on Windows
# len([c for c in u"\U0001D517"]) == 1 in Python 3.6.1 on Windows
rtn = []
i = 0
while i < len(s):
# Check for surrogate pairs
if ord(s[i]) >= 0xD800 and ord(s[i]) <= 0xDBFF and (i + 1) < len(s) and ord(s[i + 1]) >= 0xDC00 and ord(s[i + 1]) <= 0xDFFF:
rtn.append([s[i], s[i+1]])
i += 1
else:
rtn.append([s[i]])
i += 1
return rtn
def encode_unicode_characters(chrs, enc, src):
# Expects chrs to be an array of arrays where each innner
# array contains either one or two unicode character code points.
# An inner array will contain two unicode code points in some
# cases for surrogate pairs.
# Join any possible surrogate pairs before they are encoded.
return [e_encode(u"".join(parts), enc, src) for parts in chrs]
def is_unicode_instance(s):
# Works in Python 2 and 3
rtn = False
try:
if type(s) == unicode:
rtn = True
finally:
return rtn
# Normalization functions for Python 2/3 compatability
def string_as_int_array(a, enc, err):
# Turn a string or byte array into an array of ordinal numbers that identify the bytes.
return [py23_ord(b) for b in as_byte_string(a, enc, err)]
def int_array_as_byte_string(a):
# Turn an array of ordinal numbers that identify the bytes into a byte string or bytes.
assert(type(a) == list)
for b in a:
assert(type(b) == int)
return bytes(bytearray(a))
def py23_str(o, enc, err):
# Turns anything (such as an int) into a unicode string.
return e_decode(as_byte_string(str(o), enc, err), enc, err)
def as_byte_string(s, enc, err):
# Make sure we know we're dealing with a byte string
if is_unicode_instance(s): # Python 2 unicode
return e_encode(s, enc, err)
else:
if type(s) == bytes and type(s) == str: # Python 2 str
return s
elif type(s) == bytes and not type(s) == str: # Python 3 bytes
return s
elif not type(s) == bytes and type(s) == str: # Python 3 str
return e_encode(s, enc, err) # Python3 bytes, or python2 str
else:
assert(0)
def py23_ord(c):
# Return the ordinal number that corresponds to a given byte.
if type(c) is int:
return c
else:
# Unicode, or python 3 str
return ord(c)
def portable_escape(s, enc, err):
# Escapes unicode and control characters in python 2 and 3
return e_encode(e_decode(s, enc, err), "unicode-escape", err)
def de_double_slashes(int_array):
# Takes a list of integers representing each byte value of the string
# then removes any instance of double slashes.
# Returns a binary string.
new_array = []
last_c = None
for c in int_array:
if last_c == 92 and c == 92:
pass # Ignore this extra slash
else:
new_array.append(c)
last_c = c
return int_array_as_byte_string(new_array)
def evaluate_escape_sequences(s, enc, err):
# This function will return a byte string where all
# embedded unicode or hex escape sequences have been evaluated.
# The reason for using this function and not 'unicode-escape' directly
# is because 'unicode-escape' does not work when there are already
# unicode characters in the string.
double_escaped_unicode = portable_escape(s, enc, err)
bytes_as_ints = string_as_int_array(double_escaped_unicode, enc, err)
return e_decode(de_double_slashes(bytes_as_ints), "unicode-escape", err)
def get_parts_for_change_region(edit_script, i, ins, dels):
parts = []
# This is the size of the 'changed' region.
square_size = min(len(ins), len(dels))
# These are the inserts and deletes that have been paired up
for n in range(0, square_size):
parts.append({"operation": "change", "position_old": edit_script[dels[n]]["position_old"] ,"position_new": edit_script[ins[n]]["position_new"]})
# These are the leftover inserts, that must be pushed 'square_size' units to the right.
for n in range(square_size, len(ins)):
m = edit_script[ins[n]]
# Adjust the insertion positions so the offsets make sense in the simplified path.
shift_right = square_size - (m["position_old"] - edit_script[i]["position_old"])
p = {"operation": "insert", "position_old": m["position_old"] + shift_right, "position_new": m["position_new"]}
parts.append(p)
# These are the leftover deletes.
for n in range(square_size, len(dels)):
m = edit_script[dels[n]]
parts.append(m)
return parts
def simplify_edit_script(edit_script):
# If we find a contiguous path composed of inserts and deletes, make them into 'changes' so they
# can produce more visually pleasing diffs.
new_edit_script = []
m = len(edit_script)
i = 0
while i < m:
others = []
ins = []
dels = []
last_indx = edit_script[i]["position_old"]
# Follow the path of inserts and deletes
while i + len(ins) + len(dels) < m:
indx = i + len(ins) + len(dels)
edit = edit_script[indx]
if edit["operation"] == "insert" and edit["position_old"] == last_indx:
last_indx = edit["position_old"]
ins.append(indx)
elif edit["operation"] == "delete" and edit["position_old"] == last_indx:
last_indx = edit["position_old"] + 1
dels.append(indx)
else:
if edit["operation"] == "insert" or edit["operation"] == "delete":
pass # Non-contiguous insert or delete.
else: # The current edit is something other than delete or insert, just add it...
others.append(indx)
break
if len(ins) > 0 and len(dels) > 0:
# Do simplify
new_edit_script.extend(get_parts_for_change_region(edit_script, i, ins, dels))
else:
# Add the lone sequence of deletes or inserts
for r in range(0, len(dels)):
new_edit_script.append(edit_script[dels[r]])
for r in range(0, len(ins)):
new_edit_script.append(edit_script[ins[r]])
for r in range(0, len(others)):
new_edit_script.append(edit_script[others[r]])
i += len(ins) + len(dels) + len(others)
return new_edit_script
def is_probably_on_windows():
p = platform.system().lower()
if p.find("windows") != -1:
return True
elif p.find("cygwin") != -1:
return True
return False
def read_char(in_fileobj, file_encoding, rp, file_source, as_binary):
c = None
try:
c = in_fileobj.read(1)
except UnicodeError as e:
src = e_decode(as_byte_string(file_source, rp.output_encoding, "internal"), rp.output_encoding, "internal")
msg = e_decode(as_byte_string(str(e), rp.output_encoding, "internal"), rp.output_encoding, "internal")
output_bytes(e_encode(u"Fatal unicode error from " + src + u": " + msg + rp.output_newline, rp.output_encoding, "internal"), rp)
do_graceful_exit(rp, MISSING_BYTE_ORDER_MARKER_EXIT_CODE)
if as_binary:
more_chars = c != b""
else:
more_chars = c != u""
# Get the bytes instead of characters
c = e_encode(c, file_encoding, file_source)
if not rp.pretty_output: # Try to convert it to the output format right away
c = e_encode(e_decode(c, file_encoding, file_source), rp.output_encoding, file_source)
return more_chars, c
def do_file_open_fail_error(f, e, rp):
fname = e_decode(as_byte_string(f, rp.output_encoding, "internal"), rp.output_encoding, "internal")
msg = e_decode(as_byte_string(str(e), rp.output_encoding, "internal"), rp.output_encoding, "internal")
output_bytes(e_encode(u"Failed to open file " + fname + u": " + msg + rp.output_newline, rp.output_encoding, "internal"), rp)
do_graceful_exit(rp, FILE_OPEN_FAIL_ERROR_EXIT_CODE)
def read_file_as_list(infile, rp, file_encoding, file_source, as_binary):
indentation_levels = []
byte_offsets = []
rtn = []
in_fileobj = None
current_level = 0
current_byte_offset = 0
global err_source
err_source = file_source
if as_binary:
try:
in_fileobj = open(infile, "rb")
except Exception as e:
do_file_open_fail_error(infile, e, rp)
else:
try:
in_fileobj = codecs.open(infile, "r", encoding=file_encoding, errors="ignore")
except Exception as e:
do_file_open_fail_error(infile, e, rp)
line = b''
try:
num_reads = 1
more_chars, c = read_char(in_fileobj, file_encoding, rp, file_source, as_binary)
# Note u"".encode("utf-16") == b"\xff\xfe", so be careful on loop termination.
while more_chars:
line = line + c
for d in rp.delimiters:
position = line.find(d["delimiter"])
if position != -1:
if position > 0: # Avoid adding empty lines
rtn.append(line[0:position])
byte_offsets.append(current_byte_offset)
current_byte_offset += len(bytearray(line[0:position]))
indentation_levels.append(current_level)
level_before = current_level
current_level += d["level_adjust"]
if current_level < 0:
current_level = 0
if rp.include_delimiters:
rtn.append(line[position:]) # Only if you want to include delimiters.
indentation_levels.append(min(current_level, level_before))
byte_offsets.append(current_byte_offset)
current_byte_offset += len(bytearray(line[position:]))
line = b''
# For cutting long lines into multiple lines
if rp.cut_lines:
if num_reads == rp.max_line_length:
num_reads = 0
rtn.append(line)
indentation_levels.append(current_level)
byte_offsets.append(current_byte_offset)
current_byte_offset += len(bytearray(line))
line = b''
more_chars, c = read_char(in_fileobj, file_encoding, rp, file_source, as_binary)
num_reads += 1
if len(line) > 0:
rtn.append(line)
indentation_levels.append(current_level)
byte_offsets.append(current_byte_offset)
current_byte_offset += len(bytearray(line))
finally:
in_fileobj.close()
# Add an extra entry so we know how long the entire thing is.
byte_offsets.append(current_byte_offset)
return rtn, byte_offsets, indentation_levels
def get_terminal_width(rp, unix_terminal_interface, windows_terminal_interface):
if unix_terminal_interface is not None:
if unix_terminal_interface.terminal_width is not None:
if unix_terminal_interface.terminal_width > 0:
return unix_terminal_interface.terminal_width
if windows_terminal_interface is not None:
if windows_terminal_interface.terminal_width is not None:
if windows_terminal_interface.terminal_width > 0:
return windows_terminal_interface.terminal_width
# Give up and assume 80 wide
msg = u"WARNING: Unable to determine terminal width so defaulting to 80. You can specify with --cols flag." + rp.output_newline
b = e_encode(msg, rp.output_encoding, "internal")
output_bytes(b, rp)
default_width = 80
return default_width
def validate_delimiters(delimiters, rp):
for i in range(0, len(delimiters)):
for j in range(0, len(delimiters)):
if i != j:
if delimiters[i]["delimiter"].find(delimiters[j]["delimiter"]) == 0:
d1 = e_decode(delimiters[i]["delimiter"], rp.parameters_encoding, "parameters")
d2 = e_decode(delimiters[j]["delimiter"], rp.parameters_encoding, "parameters")
d1 = e_encode(d1, "unicode-escape", "internal")
d2 = e_encode(d2, "unicode-escape", "internal")
d1 = e_decode(d1, "utf-8", "internal")
d2 = e_decode(d2, "utf-8", "internal")
msg = u"ERROR: The delimiter '" + d2 + u"' is a prefix of the delimiter '" + d1 + u"'. This would cause the delimiter '" + d1 + u"' to never be matched." + rp.output_newline
b = e_encode(msg, rp.output_encoding, "internal")
output_bytes(b, rp)
do_graceful_exit(rp, COMMON_PREFIX_ERROR_EXIT_CODE)
class RunParameters(object):
def __init__(self, args):
self.one_indent = u" "
self.input_newline = u"\r\n" if is_probably_on_windows() else u"\n"
self.output_newline = self.input_newline # Default to platform. Will be everwitten later.
self.uses_change = False
self.uses_deletion = False
self.uses_insertion = False
self.oldfile = args.oldfile
self.newfile = args.newfile
self.terminal_width = None
# Global encoding flag:
if args.e is not None:
args.parameters_encoding = args.e
args.oldfile_encoding = args.e
args.newfile_encoding = args.e
args.output_encoding = args.e
# For minified files
if args.m is not None:
args.include_delimiters = True
if args.m == "json" or args.m == "js" or args.m == "css":
args.push_delimiters = [u"(", u"{", u"["]
args.pop_delimiters = [u")", u"}", u"]"]
if args.m == "html":
args.push_delimiters = [u"(", u"{", u"[", u"<"]
args.pop_delimiters = [u")", u"}", u"]", u">"]
# View hex mode
if args.x is not None:
args.delimiters = []
args.show_byte_offsets = True
args.max_line_length = args.x
# First, get the encodings so we know how to decode/encode everything else
self.parameters_encoding = sys.getdefaultencoding() if disable_implicit_encoding is False else default_encoding
if args.parameters_encoding is not None:
self.parameters_encoding = args.parameters_encoding # byte str in Python 2, unicode str in python 3
self.pretty_output = True
self.output_encoding = sys.getdefaultencoding() if disable_implicit_encoding is False else default_encoding
if args.output_encoding is not None:
self.pretty_output = False
self.output_encoding = args.output_encoding
self.oldfile_encoding = "ascii"
self.oldfile_as_binary = True
if args.oldfile_encoding is not None:
self.oldfile_as_binary = False
self.oldfile_encoding = args.oldfile_encoding
self.newfile_encoding = "ascii"
self.newfile_as_binary = True
if args.newfile_encoding is not None:
self.newfile_as_binary = False
self.newfile_encoding = args.newfile_encoding
self.outfile = None
self.outfile_f = None
if args.outfile is not None:
self.outfile = args.outfile
try:
self.outfile_f = open(self.outfile, "wb")
except Exception as e:
do_file_open_fail_error(self.outfile, e, self)
# The unix terminal interface needs to be initialized after the output encoding is known because it will output ANSI escape sequences.
self.unix_terminal_interface = make_unix_terminal_interface(self)
self.windows_terminal_interface = make_windows_terminal_interface(self)
initialize_error_counts_object(self.oldfile_encoding, self.oldfile_as_binary, self.newfile_encoding, self.newfile_as_binary, self.output_encoding, self.parameters_encoding)
# Change the newline if it was specified by the user.
if args.newline is not None:
d = args.newline
param_as_unicode = e_decode(d, self.parameters_encoding, "parameters") if (type(d) == bytes and type(d) == str) else d
bs = as_byte_string(param_as_unicode, self.parameters_encoding, "parameters")
ed = as_byte_string(evaluate_escape_sequences(bs, self.parameters_encoding, "parameters"), self.parameters_encoding, "parameters")
self.output_newline = e_decode(ed, self.parameters_encoding, "parameters")
self.delimiters = []
if args.delimiters is not None:
for d in args.delimiters:
param_as_unicode = e_decode(d, self.parameters_encoding, "parameters") if (type(d) == bytes and type(d) == str) else d
bs = as_byte_string(param_as_unicode, self.parameters_encoding, "parameters")
ed = as_byte_string(evaluate_escape_sequences(bs, self.parameters_encoding, "parameters"), self.parameters_encoding, "parameters")
self.delimiters.append({"delimiter": ed, "level_adjust": 0})
else:
bs = as_byte_string(self.input_newline, self.output_encoding, "internal")
self.delimiters.append({"delimiter": bs, "level_adjust": 0})
if args.push_delimiters is not None:
for d in args.push_delimiters:
param_as_unicode = e_decode(d, self.parameters_encoding, "parameters") if (type(d) == bytes and type(d) == str) else d
bs = as_byte_string(param_as_unicode, self.parameters_encoding, "parameters")
ed = as_byte_string(evaluate_escape_sequences(bs, self.parameters_encoding, "parameters"), self.parameters_encoding, "parameters")
self.delimiters.append({"delimiter": ed, "level_adjust": 1})
if args.pop_delimiters is not None:
for d in args.pop_delimiters:
param_as_unicode = e_decode(d, self.parameters_encoding, "parameters") if (type(d) == bytes and type(d) == str) else d
bs = as_byte_string(param_as_unicode, self.parameters_encoding, "parameters")
ed = as_byte_string(evaluate_escape_sequences(bs, self.parameters_encoding, "parameters"), self.parameters_encoding, "parameters")
self.delimiters.append({"delimiter": ed, "level_adjust": -1})
validate_delimiters(self.delimiters, self)
if args.cols is not None:
self.terminal_width = args.cols
else:
self.terminal_width = get_terminal_width(self, self.unix_terminal_interface, self.windows_terminal_interface)
if args.lines_context is not None:
self.lines_context = args.lines_context
else:
self.lines_context = 2
if args.max_line_length is not None:
self.cut_lines = True
self.max_line_length = args.max_line_length
if not (self.max_line_length > 0):
do_max_line_length_error(self)
else:
self.cut_lines = False
self.infinite_context = False
if args.infinite_context is not None and args.infinite_context == True:
self.infinite_context = True
decoded_oldfile_message = e_decode(args.oldfile_message, self.parameters_encoding, "parameters") if (type(args.oldfile_message) == bytes and type(args.oldfile_message) == str) else args.oldfile_message
if decoded_oldfile_message is None:
self.oldfile_message = self.oldfile
else:
self.oldfile_message = decoded_oldfile_message
decoded_newfile_message = e_decode(args.newfile_message, self.parameters_encoding, "parameters") if (type(args.newfile_message) == bytes and type(args.newfile_message) == str) else args.newfile_message
if decoded_newfile_message is None:
self.newfile_message = self.newfile
else:
self.newfile_message = decoded_newfile_message
self.enable_line_numbers = True
if args.disable_line_numbers is not None and args.disable_line_numbers == True:
self.enable_line_numbers = False
self.disable_colours = False
if args.disable_colours is not None and args.disable_colours == True:
self.disable_colours = True
self.include_delimiters = False
if args.include_delimiters is not None and args.include_delimiters == True:
self.include_delimiters = True
self.show_byte_offsets = False
if args.show_byte_offsets is not None and args.show_byte_offsets == True:
self.show_byte_offsets = True
self.enable_header = True
if args.disable_header is not None and args.disable_header == True:
self.enable_header = False
self.enable_mark = False
if args.enable_mark is not None and args.enable_mark == True:
self.enable_mark = True
# Default print method comes from things we detect in terminal.
self.use_ansi = False
if self.unix_terminal_interface is not None:
if self.unix_terminal_interface.num_colours_supported is not None:
if self.unix_terminal_interface.num_colours_supported > 0:
self.use_ansi = True
self.use_windows_terminal_colours = False
if self.windows_terminal_interface is not None:
if self.windows_terminal_interface.csbi_wAttributes_original is not None:
self.use_windows_terminal_colours = True
if args.enable_ansi is not None and args.enable_ansi == True:
self.use_ansi = True
if args.disable_ansi is not None and args.disable_ansi == True:
self.use_ansi = False
if args.enable_windows_terminal_colours is not None and args.enable_windows_terminal_colours == True:
self.use_windows_terminal_colours = True
if args.disable_windows_terminal_colours is not None and args.disable_windows_terminal_colours == True:
self.use_windows_terminal_colours = False
# If we chop up the file, then line numbers don't make sense anymore.
if self.include_delimiters or (args.pop_delimiters is not None and len(args.pop_delimiters)) or (args.pop_delimiters is not None and len(args.pop_delimiters)) or (self.cut_lines):
self.show_byte_offsets = True
if self.disable_colours:
self.use_windows_terminal_colours = False
self.use_ansi = False
if args.verbose is not None and args.verbose == True:
if self.windows_terminal_interface:
self.windows_terminal_interface.output_test()
output_bytes(e_encode(self.windows_terminal_interface.as_unicode(), self.output_encoding, "internal"), self)
else:
output_bytes(e_encode(u"Windows terminal interface is None" + self.output_newline, self.output_encoding, "internal"), self)
if self.unix_terminal_interface:
self.unix_terminal_interface.output_test()
output_bytes(e_encode(self.unix_terminal_interface.as_unicode(), self.output_encoding, "internal"), self)
else:
output_bytes(e_encode(u"Unix terminal interface is None" + self.output_newline, self.output_encoding, "internal"), self)
# Dump all of the various params:
output_bytes(e_encode(u"Here are the final calculated runtime parameters that are about to be used:" + self.output_newline, self.output_encoding, "internal"), self)
output_bytes(e_encode(u"use_windows_terminal_colours: " + py23_str(self.use_windows_terminal_colours, self.output_encoding, "internal") + self.output_newline, self.output_encoding, "internal"), self)
output_bytes(e_encode(u"use_ansi: " + py23_str(self.use_ansi, self.output_encoding, "internal") + self.output_newline, self.output_encoding, "internal"), self)
output_bytes(e_encode(u"output_encoding: " + py23_str(self.output_encoding, self.output_encoding, "internal") + self.output_newline, self.output_encoding, "internal"), self)
output_bytes(e_encode(u"oldfile_encoding: " + py23_str(self.oldfile_encoding, self.output_encoding, "internal") + self.output_newline, self.output_encoding, "internal"), self)
output_bytes(e_encode(u"newfile_encoding: " + py23_str(self.newfile_encoding, self.output_encoding, "internal") + self.output_newline, self.output_encoding, "internal"), self)
output_bytes(e_encode(u"parameters_encoding: " + py23_str(self.parameters_encoding, self.output_encoding, "internal") + self.output_newline, self.output_encoding, "internal"), self)
output_bytes(e_encode(u"show_byte_offsets: " + py23_str(self.show_byte_offsets, self.output_encoding, "internal") + self.output_newline, self.output_encoding, "internal"), self)
output_bytes(e_encode(u"enable_mark: " + py23_str(self.enable_mark, self.output_encoding, "internal") + self.output_newline, self.output_encoding, "internal"), self)
output_bytes(e_encode(u"Total number of delimiters (includes push and pop): " + py23_str(str(len(self.delimiters)), self.output_encoding, "internal") + self.output_newline, self.output_encoding, "internal"), self)
for d in self.delimiters:
output_bytes(e_encode(u" Level Adjust: " + py23_str(d["level_adjust"], self.output_encoding, "internal") + u" ", self.output_encoding, "internal"), self)
output_bytes(e_encode(u"Delimiter: " + py23_str(bytearray(d["delimiter"]), self.output_encoding, "internal") + self.output_newline, self.output_encoding, "internal"), self)
# TODO: Rest of params.
class DiffState(object):
def __init__(self, rp, old_sequence, new_sequence, byte_offsets_old, byte_offsets_new, indents_old, indents_new, edit_script):
self.indents_new = indents_new
self.indents_old = indents_old
self.byte_offsets_new = byte_offsets_new
self.byte_offsets_old = byte_offsets_old
self.old_sequence = old_sequence
self.new_sequence = new_sequence
self.edit_script = edit_script
self.correct_symbol = u"="
self.incorrect_symbol = u"x"
self.separator = u"|"
self.marking_symbol_area_width = 2 if rp.enable_mark else 0
self.largest_line_number = max(len(self.new_sequence), len(self.old_sequence))
highest_byte_offset_old = 0 if len(byte_offsets_old) == 0 else byte_offsets_old[len(byte_offsets_old) -1]
highest_byte_offset_new = 0 if len(byte_offsets_new) == 0 else byte_offsets_new[len(byte_offsets_new) -1]
self.largest_byte_offset = max(highest_byte_offset_old, highest_byte_offset_new)
self.side_width = int((int(rp.terminal_width) - self.marking_symbol_area_width - (3*len(self.separator))) / 2)
if rp.show_byte_offsets:
self.line_number_area_width = len(str(self.largest_byte_offset)) + 3 # Extra 2 for '0x'
else:
self.line_number_area_width = len(str(self.largest_line_number)) + 1
self.line_data_width = self.side_width - self.line_number_area_width
class SideBySideViewLines(object):
def __init__(self, old_line, new_line, old_line_number, new_line_number, match, insertion, deletion, change):
self.old_line = old_line
self.new_line = new_line
self.old_line_number = old_line_number
self.new_line_number = new_line_number
self.match = match
self.insertion = insertion
self.deletion = deletion
self.change = change
class ColouredCharacter(object):
def __init__(self, character_bytes, colours):
# A character is a list of integers that make up the bytes of the character.
assert(isinstance(character_bytes, list))
for i in character_bytes:
assert(isinstance(i, int))
self.character_bytes = character_bytes
self.colours = colours
def coloured_text(line, colours, rp, err):
# Expects line to be made up of individual characters (not bytes)
result = []
grouped = group_unicode_characters(line)
for c in encode_unicode_characters(grouped, rp.output_encoding, err):
i_bytes = [py23_ord(b) for b in c]
result.append(ColouredCharacter(i_bytes, colours))
return result
def apply_character_colours(character_bytes, bg_colours, rp, err):
# Iterate over all characters, and return a list of coloured characters
result = []
if rp.pretty_output:
for b in list(character_bytes):
result.append(ColouredCharacter([py23_ord(b)], bg_colours))
else:
decoded = e_decode(character_bytes, rp.output_encoding, err)
grouped = group_unicode_characters(decoded)
for c in encode_unicode_characters(grouped, rp.output_encoding, err):
result.append(ColouredCharacter([py23_ord(b) for b in c], bg_colours))
return result
def get_recursive_diff_list(chrs, rp, err):
# Each line is a byte array that contains one character
if rp.pretty_output:
return [int_array_as_byte_string([py23_ord(c)]) for c in chrs]
else:
decoded = e_decode(chrs, rp.output_encoding, err)
grouped = group_unicode_characters(decoded)
return encode_unicode_characters(grouped, rp.output_encoding, err)