Commit 9920f1ad authored by David Gobbi's avatar David Gobbi Committed by Kitware Robot

Merge topic 'indent-utility'

2ff1f497 Add '--test' option to vtk_reindent_code.py
ccc2eb5d Call reindent() until no further changes occur
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: Ben Boeckel's avatarBen Boeckel <ben.boeckel@kitware.com>
Merge-request: !1608
parents 0c84f376 2ff1f497
Pipeline #18348 running with stage
#!/usr/bin/env python
"""
Usage: python vtk_reindent_code.py [--test] <file1> [<file2> ...]
This script takes old-style "Whitesmiths" indented VTK source files as
input, and re-indents the braces according to the new VTK style.
Only the brace indentation is modified.
If called with the --test option, then it will print an error message
for each file that it would modify, but it will not actually modify the
files.
Written by David Gobbi on Sep 30, 2015.
"""
import sys
import os
import re
def reindent(filename):
def reindent(filename, dry_run=False):
"""Reindent a file from Whitesmiths style to Allman style"""
# This first part of this function clears all strings and comments
......@@ -269,13 +276,57 @@ def reindent(filename):
lines[i] = trailing.sub("", lines[i])
while n > 0 and lines[n-1].rstrip() == "":
n -= 1
# rewrite the file
ofile = open(filename, 'w')
ofile.writelines(lines)
ofile.close()
if dry_run:
errcount = len(lines_changed)
line_numbers = list(lines_changed.keys())
line_numbers.sort()
line_numbers = [str(l + 1) for l in line_numbers[0:10] ]
if errcount > len(line_numbers):
line_numbers.append("...")
sys.stderr.write("Warning: " + filename +
": incorrect brace indentation on " +
str(errcount) +
(" lines: ", "line: ")[errcount == 1] +
", ".join(line_numbers) + "\n")
else:
# rewrite the file
ofile = open(filename, 'w')
ofile.writelines(lines)
ofile.close()
return True
return False
if __name__ == "__main__":
for filename in sys.argv[1:]:
reindent(filename)
files = []
opt_ignore = False # ignore all further options
opt_test = False # the --test option
for arg in sys.argv[1:]:
if arg[0:1] == '-' and not opt_ignore:
if arg == '--':
opt_ignore = True
elif arg == '--test':
opt_test = True
else:
sys.stderr.write("%s: unrecognized option %s\n" %
(os.path.split(sys.argv[0])[-1], arg))
sys.exit(1)
else:
files.append(arg)
# if --test was set, whenever a file needs modification, we set
# "failed" and continue checking the rest of the files
failed = False
for filename in files:
# repeat until no further changes occur
while reindent(filename, dry_run=opt_test):
if opt_test:
failed = True
break
if failed:
sys.exit(1)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment