-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathignore.py
More file actions
55 lines (46 loc) · 1.36 KB
/
ignore.py
File metadata and controls
55 lines (46 loc) · 1.36 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
"""
python3 ignore.py
"""
import os
def search(in_dir = None):
ignored_files = []
add_files = []
for f in os.listdir(in_dir):
if f == '.git': continue
f = os.path.join(in_dir, f)
if os.path.isfile(f):
size = os.path.getsize(f)
if size > 2 * 1000 * 1000: # 2 MB
ignored_files.append((f, size))
else:
add_files.append(f)
else:
v1, v2 = search(in_dir = f)
if len(v1) == 0:
ignored_files.append((f, 0)) # add directory
add_files.extend(v1)
ignored_files.extend(v2)
return add_files, ignored_files
def write(ignore_files, ignore_path = '.gitignore'):
with open(ignore_path, 'w') as f:
ss = ['**/legacy/', '*.DS_Store', '*.zip', '*.dat', '.idea/', '.git/', '**/__pycache__/']
ss = '\n'.join(ss)
f.write(ss + '\n\n')
for i, (file, size) in enumerate(ignore_files):
if i %100 == 0: print(f'{file}, {int(size)/(1000*1000):.2f}MB')
f.write(file + '\n')
def add(add_files):
# for i, file in enumerate(add_files):
# cmd = f"git add -f \"{file}\""
# if i%100 == 0: print(file)
# os.system(cmd)
ss = ' '.join([f"\"{v}\"" for v in add_files])
cmd = f"git add -f \"{ss}\""
os.system(cmd)
if __name__ == '__main__':
add_files, ignored_files = search(in_dir='.')
print(len(add_files), len(ignored_files))
print(ignored_files[:10])
add(add_files)
write(ignored_files, ignore_path='.gitignore')
print(len(ignored_files))