forked from phoenix/litellm-mirror
refactor(check_file_length.py): add local pre-commit check for file length
This commit is contained in:
parent
54cf065048
commit
290bcc09e0
2 changed files with 36 additions and 1 deletions
|
@ -24,3 +24,10 @@ repos:
|
||||||
language: system
|
language: system
|
||||||
types: [python]
|
types: [python]
|
||||||
files: ^litellm/
|
files: ^litellm/
|
||||||
|
# - id: check-file-length
|
||||||
|
# name: Check file length
|
||||||
|
# entry: python check_file_length.py
|
||||||
|
# args: ["10000"] # set your desired maximum number of lines
|
||||||
|
# language: python
|
||||||
|
# files: litellm/.*\.py
|
||||||
|
# exclude: ^litellm/tests/
|
28
check_file_length.py
Normal file
28
check_file_length.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def check_file_length(max_lines, filenames):
|
||||||
|
bad_files = []
|
||||||
|
for filename in filenames:
|
||||||
|
with open(filename, "r") as file:
|
||||||
|
lines = file.readlines()
|
||||||
|
if len(lines) > max_lines:
|
||||||
|
bad_files.append((filename, len(lines)))
|
||||||
|
return bad_files
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
max_lines = int(sys.argv[1])
|
||||||
|
filenames = sys.argv[2:]
|
||||||
|
|
||||||
|
bad_files = check_file_length(max_lines, filenames)
|
||||||
|
if bad_files:
|
||||||
|
bad_files.sort(
|
||||||
|
key=lambda x: x[1], reverse=True
|
||||||
|
) # Sort files by length in descending order
|
||||||
|
for filename, length in bad_files:
|
||||||
|
print(f"{filename}: {length} lines")
|
||||||
|
|
||||||
|
sys.exit(1)
|
||||||
|
else:
|
||||||
|
sys.exit(0)
|
Loading…
Add table
Add a link
Reference in a new issue