mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
The `__future__` we relied on is now, where the 3 specific things are all included [since Python 3.0](https://docs.python.org/3/library/__future__.html): * absolute_import * print_function * unicode_literals * division These import statements are no-ops and are no longer necessary.
73 lines
2.1 KiB
Python
Executable File
73 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
|
|
import argparse
|
|
import sys
|
|
|
|
|
|
DESCRIPTION = """
|
|
This is a simple script that takes input from stdin and prints out any lines that are
|
|
greater than 80+ characters. In such a case, it will print out the line number, the
|
|
length of the line, and the line with whitespace stripped. The reason why we print out
|
|
the line with whitespace stripped is that in the case where there is a lot of redundant
|
|
whitespace, the output becomes hard to read and no value is provided in terms of finding
|
|
the line in question. Exits with 1 if it finds any violating lines, 0 otherwise.
|
|
"""
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(description=DESCRIPTION)
|
|
|
|
parser.add_argument(
|
|
"infile",
|
|
nargs="?",
|
|
type=argparse.FileType("r"),
|
|
default=sys.stdin,
|
|
help="The file to read input from. If no file is provided, stdin is used.",
|
|
)
|
|
parser.add_argument(
|
|
"-o",
|
|
"--output",
|
|
type=argparse.FileType("w"),
|
|
default=sys.stdout,
|
|
help="The file to print to. If no file is provided, stdout is used",
|
|
dest="outfile",
|
|
)
|
|
parser.add_argument(
|
|
"--max-length",
|
|
type=int,
|
|
default=80,
|
|
metavar="LENGTH",
|
|
help="Maximum line length that the script should check for",
|
|
)
|
|
parser.add_argument(
|
|
"--count-newline",
|
|
action="store_false",
|
|
help="Should we include the newline in our line length count",
|
|
)
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
|
|
found_violation = False
|
|
|
|
for lineno, line in enumerate(args.infile, start=1):
|
|
# sys.stdin.readlines() includes a newline. So we subtract 1 from our length to
|
|
# get the "true" line length.
|
|
length = len(line) - int(args.count_newline)
|
|
if length > args.max_length:
|
|
found_violation = True
|
|
print(
|
|
"line: {}. length: {}: {}".format(lineno, length, line.strip()),
|
|
file=args.outfile,
|
|
)
|
|
|
|
sys.exit(found_violation)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|