1
1
"""Cog content generation tool."""
2
2
3
3
import copy
4
+ import difflib
4
5
import getopt
5
6
import glob
6
7
import io
50
51
-z The end-output marker can be omitted, and is assumed at eof.
51
52
-v Print the version of cog and exit.
52
53
--check Check that the files would not change if run again.
54
+ --diff With --check, show a diff of what failed the check.
53
55
--markers='START END END-OUTPUT'
54
56
The patterns surrounding cog inline instructions. Should
55
57
include three values separated by spaces, the start, end,
@@ -240,6 +242,7 @@ def __init__(self):
240
242
self .prologue = ""
241
243
self .print_output = False
242
244
self .check = False
245
+ self .diff = False
243
246
244
247
def __eq__ (self , other ):
245
248
"""Comparison operator for tests to use."""
@@ -262,6 +265,7 @@ def parse_args(self, argv):
262
265
"cdD:eI:n:o:rs:p:PUvw:xz" ,
263
266
[
264
267
"check" ,
268
+ "diff" ,
265
269
"markers=" ,
266
270
"verbosity=" ,
267
271
],
@@ -308,6 +312,8 @@ def parse_args(self, argv):
308
312
self .eof_can_be_end = True
309
313
elif o == "--check" :
310
314
self .check = True
315
+ elif o == "--diff" :
316
+ self .diff = True
311
317
elif o == "--markers" :
312
318
self ._parse_markers (a )
313
319
elif o == "--verbosity" :
@@ -335,6 +341,9 @@ def validate(self):
335
341
if self .replace and self .output_name :
336
342
raise CogUsageError ("Can't use -o with -r (they are opposites)" )
337
343
344
+ if self .diff and not self .check :
345
+ raise CogUsageError ("Can't use --diff without --check" )
346
+
338
347
339
348
class Cog (Redirectable ):
340
349
"""The Cog engine."""
@@ -692,6 +701,18 @@ def process_one_file(self, fname):
692
701
else :
693
702
assert self .options .check
694
703
self .check_failed = True
704
+ if self .options .diff :
705
+ old_lines = old_text .splitlines ()
706
+ new_lines = new_text .splitlines ()
707
+ diff = difflib .unified_diff (
708
+ old_lines ,
709
+ new_lines ,
710
+ fromfile = f"current { fname } " ,
711
+ tofile = f"changed { fname } " ,
712
+ lineterm = "" ,
713
+ )
714
+ for diff_line in diff :
715
+ self .prout (diff_line )
695
716
finally :
696
717
# The try-finally block is so we can print a partial line
697
718
# with the name of the file, and print (changed) on the
0 commit comments