24
24
import argparse
25
25
import glob
26
26
import os
27
+ import itertools
27
28
import platform
28
29
import shlex
29
30
import shutil
@@ -41,6 +42,9 @@ def _parseArgs():
41
42
action = 'append' ,
42
43
help = ('Compare output file with a file in the baseline-dir of the '
43
44
'same name' ))
45
+ parser .add_argument ('--diff-compare-options' , default = [], type = str ,
46
+ action = 'append' ,
47
+ help = 'Options for the diff tool' )
44
48
parser .add_argument ('--image-diff-compare' , default = [], type = str ,
45
49
action = 'append' ,
46
50
help = ('Compare output image with an image in the baseline '
@@ -153,25 +157,45 @@ def _cleanOutput(pathPattern, fileName, verbose):
153
157
_stripPath (fileName , pathPattern )
154
158
return True
155
159
156
- def _diff (fileName , baselineDir , verbose , failuresDir = None ):
160
+ def _diff (fileName , baselineDir , diffOptions , verbose , failuresDir = None ):
157
161
# Use the diff program or equivalent, rather than filecmp or similar
158
162
# because it's possible we might want to specify other diff programs
159
163
# in the future.
164
+
160
165
import platform
161
- if platform .system () == 'Windows' :
162
- diff = 'fc.exe'
163
- else :
164
- diff = '/usr/bin/diff'
166
+ isWindows = platform .system () == 'Windows' ;
167
+
168
+ diffTool = shutil .which ('diff' )
169
+ if not diffTool and isWindows :
170
+ gitExe = shutil .which ('git' )
171
+ if gitExe :
172
+ # On Windows, diff is bundled with Git.
173
+ # git.exe should be found here: <prefix>/Git/cmd/git.exe
174
+ # And diff.exe should be here: <prefix>/Git/usr/bin/diff.exe
175
+ gitToolsPath = os .path .normpath (os .path .join (os .path .dirname (gitExe ),
176
+ '..' , 'usr' , 'bin' ))
177
+ diffTool = shutil .which ('diff' , path = gitToolsPath )
178
+
179
+ if not diffTool :
180
+ sys .stderr .write (
181
+ "Error: could not find \" diff\" tool. Make sure it's in your PATH.\n " )
182
+ return False
165
183
166
184
filesToDiff = glob .glob (fileName )
167
185
if not filesToDiff :
168
186
sys .stderr .write (
169
187
"Error: could not files matching {0} to diff" .format (fileName ))
170
188
return False
171
189
190
+
191
+ options = list (itertools .chain .from_iterable ([shlex .split (option )
192
+ for option in diffOptions ]))
193
+ if isWindows :
194
+ options .append ('--strip-trailing-cr' )
195
+
172
196
for fileToDiff in filesToDiff :
173
197
baselineFile = _resolvePath (baselineDir , fileToDiff )
174
- cmd = [diff , baselineFile , fileToDiff ]
198
+ cmd = [diffTool , * options , baselineFile , fileToDiff ]
175
199
if verbose :
176
200
print ("diffing with {0}" .format (cmd ))
177
201
@@ -413,8 +437,8 @@ def _runCommand(raw_command, stdout_redir, stderr_redir, env,
413
437
os .environ .get ('PXR_CTEST_RUN_ID' , 'NOT_RUN_FROM_CTEST' ))
414
438
if args .diff_compare :
415
439
for diff in args .diff_compare :
416
- if not _diff (diff , args .baseline_dir , args .verbose ,
417
- failuresDir = failuresDir ):
440
+ if not _diff (diff , args .baseline_dir , args .diff_compare_options ,
441
+ args . verbose , failuresDir = failuresDir ):
418
442
sys .stderr .write ('Error: diff for {0} failed '
419
443
'(DIFF_COMPARE).' .format (diff ))
420
444
sys .exit (1 )
0 commit comments