-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathline_length.py
More file actions
executable file
·55 lines (44 loc) · 1.75 KB
/
Copy pathline_length.py
File metadata and controls
executable file
·55 lines (44 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3.10
import argparse
import sys
from script_utils import changed_files, color_txt, git_files
def main():
parser = argparse.ArgumentParser(description="Check that all code lines are not too long.")
parser.add_argument(
"--max_line_length",
"-l",
type=int,
default=100,
help="The maximal amount of characters a line can have (default: 100)",
)
parser.add_argument("--files", nargs="+", help="Run on specified files. Ignore other flags.")
parser.add_argument("--changes_only", action="store_true", help="Run only on changed files.")
parser.add_argument("--quiet", "-q", dest="verbose", action="store_false")
args = parser.parse_args()
extensions = ["py"]
if args.files:
files = [path for path in args.files if path.endswith(tuple(extensions))]
elif args.changes_only:
files = changed_files(extensions)
else:
files = git_files(extensions)
if args.verbose:
print(color_txt("yellow", "=== checking the following files: ===\n" + "\n".join(files)))
sys.stdout.flush()
long_lines = []
for f in files:
for line_num, line in enumerate(open(f), 1):
line = line.rstrip("\n")
if line.startswith("from") or line.startswith("import"):
continue
if len(line) > args.max_line_length:
long_lines.append((f, line_num))
if len(long_lines) > 0:
print(color_txt("red", "The following lines are too long:"))
for file_name, line_num in long_lines:
print(f"{file_name}:{line_num}")
sys.exit(1)
if args.verbose:
print(color_txt("green", "=== Line length check completed successfully ==="))
if __name__ == "__main__":
main()