forked from magnusrodseth/unzipper-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunzip.py
executable file
·134 lines (106 loc) · 3.79 KB
/
unzip.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import sys
import zipfile
import os
def get_username(assignment: str) -> str:
"""
Gets the student username from the assignment filename.
:param assignment:
:return:
"""
username = ""
# Get student username from auto-generated filename from BlackBoard
try:
username = assignment.split("_")[1]
except:
# We should still continue the routine
print("> Could not simplify student's directory name.")
return username
def unzip(argv) -> bool:
"""
Unzips the global .zip file and structures all student deliverables.
:param argv: is the command line arguments.
:return: a boolean value indicating whether the unzipping was successful.
"""
# argv should only process 2 arguments
if len(argv) != 2:
print("> Invalid input. Please use the following format:")
print(f'$ python3 unzip.py zipped-filename destination-directory')
return False
# Get command line arguments
zipped = argv[0]
destination = argv[1]
# Only handle .zip files
if zipped[-3:] != "zip":
print(f"> {zipped} is not a zip file.")
print()
return False
# Unzip assignment file
try:
with zipfile.ZipFile(zipped, 'r') as read:
read.extractall(destination)
print(f"> Unzipped {zipped}.")
except:
print(f"> Could not unzip {zipped}.")
# Navigate to the destination directory
try:
os.chdir(destination)
except:
print(f"> Could not navigate to /{destination}.")
# Due to new Blackboard folder structure, cd into folder
try:
os.chdir(zipped[:-4])
except:
print(f"> Could not navigate to /{zipped[:-4]}.")
# Make a feedback directory to store each student's feedback files
if not os.path.exists('feedback'):
os.mkdir("feedback")
# Make a deliverables directory to store each student's deliverables files
if not os.path.exists('deliverables'):
os.mkdir("deliverables")
# Remove redundant .txt files
try:
os.system("rm *.txt")
except:
# We should still continue the routine
print("> Could not remove .txt files, or no .txt files was found.")
# Keep track of how many files we can unzip.
count = 0
# Target is an integer determined by the number of .zip files in the destination directory.
target = len([filename for filename in os.listdir() if filename[-3:] == "zip"])
# For each zipped assignment in the destination directory
for assignment in os.listdir():
# Only handle .zip files
if assignment[-3:] != "zip":
continue
username = get_username(assignment)
# Unzip student file
try:
with zipfile.ZipFile(assignment, 'r') as read:
read.extractall(f'deliverables/{username}')
count += 1
print(f"> Unzipped {username}'s deliverable. ({count} / {target})")
except:
print("> Could not unzip student's zipped assignment.")
continue
reserved = ["deliverables", "feedback"]
for assignment in os.listdir():
if assignment in reserved:
continue
username = get_username(assignment)
with open(f'feedback/{username}.txt', 'w') as f:
f.write(f"Tilbakemelding til {username} (__%)")
# Remove redundant .txt files
try:
os.system("rm *.zip")
except:
# We should still continue the routine
print("> Could not remove .zip files, or no .zip files was found.")
return True
if __name__ == '__main__':
# Syntax: python3 unzip.py zipped destination
succeeded = unzip(sys.argv[1:])
print(
"> Finished unzipping files!"
if succeeded
else "> An error occurred when trying to unzip files!"
)