mirror of
https://github.com/CorsixTH/CorsixTH.git
synced 2025-07-23 04:13:01 +02:00
In addition to listing bad files, it also reports what is bad about them.
This commit is contained in:
@@ -1,15 +1,17 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Usage: check_whitespace.py [root-dir]
|
Usage: check_whitespace.py [-e EXCL] [root-dir ...]
|
||||||
|
|
||||||
This script will check for incorrect whitespace (usage of TAB and trailing
|
This script will check for incorrect whitespace (usage of TAB and trailing
|
||||||
whitespace) in Lua, Python, and C/C++ files below |root-dir|.
|
whitespace) in Lua, Python, and C/C++ files below one of the listed
|
||||||
|
|root-dir| directories. With the -e option you can specify one or more EXCL
|
||||||
|
fragments that skips file paths that have one of the fragments.
|
||||||
It will return 0 (ie success) if only correct whitespace is found.
|
It will return 0 (ie success) if only correct whitespace is found.
|
||||||
Otherwise, it will print the path of the violating file(s) and return an
|
Otherwise, it will print the path of the violating file(s) and return an
|
||||||
error code.
|
error code.
|
||||||
|
|
||||||
If root-dir is not specified, it will use the current directory.
|
If no root-dir is specified, it will use the current directory.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import getopt
|
import getopt
|
||||||
@@ -24,19 +26,36 @@ import sys
|
|||||||
# - a SPACE as last character.
|
# - a SPACE as last character.
|
||||||
BAD_WHITESPACE = re.compile(r'\t| \r| \n| $')
|
BAD_WHITESPACE = re.compile(r'\t| \r| \n| $')
|
||||||
|
|
||||||
|
MESSAGES = {
|
||||||
|
(True, False): "has at least one tab character",
|
||||||
|
(False, True): "has trailing whitespace",
|
||||||
|
(True, True): "has both at least one tab character and trailing whitespace"
|
||||||
|
}
|
||||||
|
|
||||||
def has_bad_whitespace(path):
|
def check_for_bad_whitespace(path):
|
||||||
"""
|
"""
|
||||||
Returns whether the file at |path| has bad whitespace.
|
Checks the file at |path| for tab and trailing white space and returns an
|
||||||
|
error-message if something bad was found, or None.
|
||||||
"""
|
"""
|
||||||
|
has_bad_tab = False
|
||||||
|
has_bad_space = False
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
with open(path, 'r') as handle:
|
with open(path, 'r') as handle:
|
||||||
for line in handle:
|
for line in handle:
|
||||||
m = BAD_WHITESPACE.search(line)
|
bad_whites = BAD_WHITESPACE.findall(line)
|
||||||
if m:
|
if bad_whites:
|
||||||
return True
|
bad_tab_count = sum(1 for bw in bad_whites if bw == '\t')
|
||||||
|
bad_space_count = len(bad_whites) - bad_tab_count
|
||||||
|
|
||||||
return False
|
has_bad_tab = has_bad_tab or (bad_tab_count > 0)
|
||||||
|
has_bad_space = has_bad_space or (bad_space_count > 0)
|
||||||
|
|
||||||
|
if has_bad_tab and has_bad_space:
|
||||||
|
break # Found everything we can possibly find, done.
|
||||||
|
|
||||||
|
if has_bad_tab or has_bad_space:
|
||||||
|
return f"File \"{path}\" {MESSAGES[(has_bad_tab, has_bad_space)]}"
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -82,7 +101,7 @@ def main():
|
|||||||
|
|
||||||
# Process files.
|
# Process files.
|
||||||
count = 0
|
count = 0
|
||||||
offending_files = []
|
messages = []
|
||||||
found_errors = False
|
found_errors = False
|
||||||
for top in tops:
|
for top in tops:
|
||||||
for root, dirs, files in os.walk(top):
|
for root, dirs, files in os.walk(top):
|
||||||
@@ -99,9 +118,10 @@ def main():
|
|||||||
# Check the file.
|
# Check the file.
|
||||||
count += 1
|
count += 1
|
||||||
try:
|
try:
|
||||||
if has_bad_whitespace(path):
|
message = check_for_bad_whitespace(path)
|
||||||
|
if message is not None:
|
||||||
found_errors = True
|
found_errors = True
|
||||||
offending_files.append(path)
|
messages.append(message)
|
||||||
|
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
print(f"ERROR: File {path} has Unicode errors.")
|
print(f"ERROR: File {path} has Unicode errors.")
|
||||||
@@ -109,10 +129,11 @@ def main():
|
|||||||
|
|
||||||
# Report files with bad whitespace.
|
# Report files with bad whitespace.
|
||||||
print('Checked {} files'.format(count))
|
print('Checked {} files'.format(count))
|
||||||
if offending_files:
|
if messages:
|
||||||
|
print()
|
||||||
print('Found files with bad whitespace:')
|
print('Found files with bad whitespace:')
|
||||||
for path in offending_files:
|
for message in messages:
|
||||||
print(path)
|
print(message)
|
||||||
|
|
||||||
# And construct the return code.
|
# And construct the return code.
|
||||||
if found_errors:
|
if found_errors:
|
||||||
|
Reference in New Issue
Block a user