Skip to content

Commit b4bb2db

Browse files
committed
[__main__] add initial support for external diff executable calls with new --external flag
1 parent ac01e80 commit b4bb2db

File tree

1 file changed

+66
-42
lines changed

1 file changed

+66
-42
lines changed

lib/fdiff/__main__.py

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from fdiff import __version__
88
from fdiff.color import color_unified_diff_line
9-
from fdiff.diff import u_diff
9+
from fdiff.diff import external_diff, u_diff
1010
from fdiff.textiter import head, tail
1111
from fdiff.utils import file_exists, get_tables_argument_list
1212

@@ -62,6 +62,7 @@ def run(argv):
6262
parser.add_argument(
6363
"--nomp", action="store_true", help="Do not use multi process optimizations"
6464
)
65+
parser.add_argument("--external", type=str, help="Run external diff tool command")
6566
parser.add_argument("PREFILE", help="Font file path/URL 1")
6667
parser.add_argument("POSTFILE", help="Font file path/URL 2")
6768

@@ -105,10 +106,6 @@ def run(argv):
105106
#
106107
# /////////////////////////////////////////////////////////
107108

108-
# ---------------
109-
# Unified diff
110-
# ---------------
111-
112109
# parse explicitly included or excluded tables in
113110
# the command line arguments
114111
# set as a Python list if it was defined on the command line
@@ -120,41 +117,68 @@ def run(argv):
120117
# optimizations for use as a u_diff function argument
121118
use_mp = not args.nomp
122119

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)
152143
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

Comments
 (0)