-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
61 lines (43 loc) · 1.64 KB
/
script.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
54
55
56
57
58
59
60
61
import zipfile
import os
from glob import glob
import sys
import re
C: int = len(sys.argv)
if C == 1:
path: str = "."
elif C == 2:
path: str = sys.argv[1]
else:
print("Usage: python script.py path_to_root")
sys.exit(1)
os.chdir(path)
try:
with open(".scriptignore", "r") as file:
# getting all patterns from scriptignore
lines = map(lambda x: x.strip(), file.readlines())
patterns: list[str] = [line for line in lines if line] # patterns
to_ignore: set = set() # all files/folders that match patterns in scriptignore
for pattern in patterns:
matches = glob(pattern, include_hidden=True)
to_ignore.update(matches)
#compressed file
compressed = zipfile.ZipFile("project.zip", "w")
for i,(folder, subfolder, filename) in enumerate(os.walk(".")):
if i != 0: #dont want a . folder among compressed
if folder in to_ignore:
continue
#for folders that in directory
#get the first subdirectory with regex and see if in scriptignore
folder_name = re.search(r"\.\\(\.?\w+)\.?(\w)*\\*", folder).group(1)
if folder_name in to_ignore:
continue
compressed.write(folder)
for file in filename:
if file in to_ignore or filename[-4:] == ".zip":
continue
compressed.write(os.path.join(folder, file))
compressed.close()
except FileNotFoundError:
print(".scriptignore file not found")
sys.exit(0)