6
6
7
7
from fdiff import __version__
8
8
from fdiff .color import color_unified_diff_line
9
- from fdiff .diff import u_diff
9
+ from fdiff .diff import external_diff , u_diff
10
10
from fdiff .textiter import head , tail
11
11
from fdiff .utils import file_exists , get_tables_argument_list
12
12
@@ -62,6 +62,7 @@ def run(argv):
62
62
parser .add_argument (
63
63
"--nomp" , action = "store_true" , help = "Do not use multi process optimizations"
64
64
)
65
+ parser .add_argument ("--external" , type = str , help = "Run external diff tool command" )
65
66
parser .add_argument ("PREFILE" , help = "Font file path/URL 1" )
66
67
parser .add_argument ("POSTFILE" , help = "Font file path/URL 2" )
67
68
@@ -105,10 +106,6 @@ def run(argv):
105
106
#
106
107
# /////////////////////////////////////////////////////////
107
108
108
- # ---------------
109
- # Unified diff
110
- # ---------------
111
-
112
109
# parse explicitly included or excluded tables in
113
110
# the command line arguments
114
111
# set as a Python list if it was defined on the command line
@@ -120,41 +117,68 @@ def run(argv):
120
117
# optimizations for use as a u_diff function argument
121
118
use_mp = not args .nomp
122
119
123
- # perform the unified diff analysis
124
- try :
125
- diff = u_diff (
126
- args .PREFILE ,
127
- args .POSTFILE ,
128
- context_lines = args .lines ,
129
- include_tables = include_list ,
130
- exclude_tables = exclude_list ,
131
- use_multiprocess = use_mp ,
132
- )
133
- except Exception as e :
134
- sys .stderr .write (f"[*] ERROR: { e } { os .linesep } " )
135
- sys .exit (1 )
136
-
137
- # re-define the line contents of the diff iterable
138
- # if head or tail is requested
139
- if args .head :
140
- iterable = head (diff , args .head )
141
- elif args .tail :
142
- iterable = tail (diff , args .tail )
143
- else :
144
- iterable = diff
145
-
146
- # print unified diff results to standard output stream
147
- has_diff = False
148
- if args .color :
149
- for line in iterable :
150
- has_diff = True
151
- sys .stdout .write (color_unified_diff_line (line ))
120
+ if args .external :
121
+ # ------------------------------
122
+ # External executable tool diff
123
+ # ------------------------------
124
+ try :
125
+ diff = external_diff (
126
+ args .external ,
127
+ args .PREFILE ,
128
+ args .POSTFILE ,
129
+ include_tables = include_list ,
130
+ exclude_tables = exclude_list ,
131
+ use_multiprocess = use_mp ,
132
+ )
133
+
134
+ # write stdout from external tool
135
+ for line , exit_code in diff :
136
+ # if exit_code is None:
137
+ sys .stdout .write (line )
138
+ if exit_code is not None :
139
+ sys .exit (exit_code )
140
+ except Exception as e :
141
+ sys .stderr .write (f"[*] ERROR: { e } { os .linesep } " )
142
+ sys .exit (1 )
152
143
else :
153
- for line in iterable :
154
- has_diff = True
155
- sys .stdout .write (line )
156
-
157
- # if no difference was found, tell the user instead of
158
- # simply closing with zero exit status code.
159
- if not has_diff :
160
- print ("[*] There is no difference between the files." )
144
+ # ---------------
145
+ # Unified diff
146
+ # ---------------
147
+ # perform the unified diff analysis
148
+ try :
149
+ diff = u_diff (
150
+ args .PREFILE ,
151
+ args .POSTFILE ,
152
+ context_lines = args .lines ,
153
+ include_tables = include_list ,
154
+ exclude_tables = exclude_list ,
155
+ use_multiprocess = use_mp ,
156
+ )
157
+ except Exception as e :
158
+ sys .stderr .write (f"[*] ERROR: { e } { os .linesep } " )
159
+ sys .exit (1 )
160
+
161
+ # re-define the line contents of the diff iterable
162
+ # if head or tail is requested
163
+ if args .head :
164
+ iterable = head (diff , args .head )
165
+ elif args .tail :
166
+ iterable = tail (diff , args .tail )
167
+ else :
168
+ iterable = diff
169
+
170
+ # print unified diff results to standard output stream
171
+ has_diff = False
172
+ if args .color :
173
+ for line in iterable :
174
+ has_diff = True
175
+ sys .stdout .write (color_unified_diff_line (line ))
176
+ else :
177
+ for line in iterable :
178
+ has_diff = True
179
+ sys .stdout .write (line )
180
+
181
+ # if no difference was found, tell the user instead of
182
+ # simply closing with zero exit status code.
183
+ if not has_diff :
184
+ print ("[*] There is no difference between the files." )
0 commit comments