-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesize_guard.py
53 lines (40 loc) · 1.43 KB
/
filesize_guard.py
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
import fnmatch
import json
import os
import sys
DEFAULT_IGNORE_FILE = "./filesize_guard.ignore"
def load_ignore_patterns(ignore_file):
default_patterns = [".git/*"]
if not os.path.exists(ignore_file):
if ignore_file == DEFAULT_IGNORE_FILE:
return default_patterns
print(f"Error: The specified ignore file '{ignore_file}' does not exist.", file=sys.stderr)
sys.exit(1)
with open(ignore_file, "r") as f:
patterns = [line.strip() for line in f if line.strip() and not line.startswith("#")]
return default_patterns + patterns
def should_ignore(file, patterns):
return any(fnmatch.fnmatch(file, pattern) for pattern in patterns)
def load_files_from_json(file_paths):
files = []
for file_path in file_paths:
if not os.path.exists(file_path):
continue
with open(file_path, "r") as f:
files.extend(json.load(f))
return files
ignore_patterns = load_ignore_patterns(sys.argv[1])
max_size_kb = int(sys.argv[2])
files = load_files_from_json(sys.argv[3:])
exit_code = 0
for file in files:
if not file:
continue
if should_ignore(file, ignore_patterns):
continue
filesize_b = os.path.getsize(file)
filesize_kb = filesize_b / 1024
if filesize_kb >= max_size_kb:
print(f"Error: '{file}' is {filesize_kb:.2f} kB, which exceeds the allowed limit of {max_size_kb} kB.")
exit_code = 1
sys.exit(exit_code)