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