-
Notifications
You must be signed in to change notification settings - Fork 2
/
moodle-extractor.py
executable file
·199 lines (152 loc) · 5.71 KB
/
moodle-extractor.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# small script for orderly unzipping of moodle excercise submission downloads
# basic usage: call with path to zipfile
# TODO: change this fuckup with tmpdir, copy it to folder where zipfile is
# located
# TODO: add functions to extract: tarballs, rar
# TODO: detect if UNIX formatted file, if not "dos2unix"
# TODO: handle .rar and files which are not compressed
# TODO: make
import sys
import os
import argparse
import shutil
import zipfile
import hashlib
import random
# import argparse
# from os.path import expanduser
parser = argparse.ArgumentParser(prog="Moodle Extractor", prefix_chars="-")
parser.add_argument("zipfile",
nargs=1,
type=argparse.FileType('rU'),
help="zip file to extract")
parser.add_argument("-s", "--single",
action="store_true",
default=False,
help="Single User Mode, default is Group mode")
parser.add_argument("-ng", "--no-grading-file",
action="store_true",
default=False,
help="Do not generate grading-file")
parser.add_argument("-v", "--version",
action="version",
version="version 0.3.1")
GRADING_FILE_NAME = "gradingfile.csv"
SINGLE_MODE_CSV_HEADER = "Vollständiger Name,Bewertung,Feedback als Kommentar\n"
GROUP_MODE_CSV_HEADER = "Gruppe,Bewertung,Feedback als Kommentar\n"
# deal with stupid filenames...
ENC = "latin-1"
# iterate through files and delete duplicates based on filehash
def remove_duplicates(directory, ENC="latin-1"):
unique = []
for filename in os.listdir(directory):
f = open(filename, encoding=ENC)
if os.path.isfile(filename):
filehash = hashlib.md5(f.read().encode(ENC)).hexdigest()
if filehash not in unique:
unique.append(filehash)
else:
os.remove(filename)
# check if Archivefile
# true if zip
# false if somethingelse
def is_zip(filename):
# there is a method: RTFM ;)
# https://docs.python.org/3/library/zipfile.html
return zipfile.is_zipfile(filename)
# take zip archive , make folder, extract it, deletes archive
def ex_zip(zip):
expath = zip.split("--")[1]
try:
os.makedirs(expath)
except FileExistsError:
os.makedirs(expath+str(random.randint(1, 1001)))
z = zipfile.ZipFile(zip)
z.extractall(path=os.path.join(os.getcwd(), expath))
os.remove(os.path.join(os.getcwd(), zip))
def create_grading_file(list, mode="GROUP"):
gradingfile = open(GRADING_FILE_NAME, 'w')
if mode == "GROUP":
gradingfile.write(GROUP_MODE_CSV_HEADER)
elif mode == "SINGLE":
gradingfile.write(SINGLE_MODE_CSV_HEADER)
for item in list:
gradingfile.write(item+",,\n")
gradingfile.close()
def parse_zipfilename(args):
print(str(args.zipfile))
if args.zipfile[0] is not None:
file_name = args.zipfile[0].name
else:
raise Exception
return os.path.split(file_name)[-1][0:-4].replace(' ', '')
def create_unzip_folder(curr_path, unzip_path):
if not os.path.isdir(unzip_path):
# pathnotexisting
os.makedirs(unzip_path)
else:
# path existing, delete and create
shutil.rmtree(os.path.join(unzip_path))
os.makedirs(unzip_path)
def handle_group_mode(zipfilename, curr_path, unzip_path, args):
create_unzip_folder(curr_path, unzip_path)
# unpack zip
os.chdir(unzip_path)
zip = zipfile.ZipFile(os.path.join(curr_path, args.zipfile[0].name))
zip.extractall()
# iterate trough files
remove_duplicates(os.getcwd(), ENC)
# get group names for grading file. does not work after unzipping
groups_unsorted = [file.split("--")[1] for file in os.listdir(unzip_path)]
groups = sorted(list(set(groups_unsorted)))
# iterate through archives and extract
zips = [file for file in os.listdir() if zipfile.is_zipfile(file)]
for zip in zips:
ex_zip(zip)
# ich soll keine doppelte verneinung nicht verwenden...
if not args.no_grading_file:
create_grading_file(groups)
def handle_single_mode(zipfilename, curr_path, unzip_path, args):
create_unzip_folder(curr_path, unzip_path)
# change into folder
os.chdir(unzip_path)
zip = zipfile.ZipFile(os.path.join(os.path.split(os.getcwd())[0],
args.zipfile[0].name))
namelist = zip.namelist()
namelist = [name.split("_")[0] for name in namelist]
# remove duplicates
unique_names = []
[unique_names.append(x) for x in namelist if x not in unique_names]
# extrctall
zip.extractall()
# create user folders
for name in unique_names:
os.makedirs(name)
# move files into folders
dirfilelist = next(os.walk(os.getcwd()))[2]
for filename in dirfilelist:
if filename.split("_")[0] in unique_names:
shutil.move(filename, filename.split("_")[0])
if not args.no_grading_file:
create_grading_file(unique_names, "SINGLE")
def main():
args = parser.parse_args()
if len(sys.argv) == 1:
print("usage: command.py zipfile")
print("use in folder where your Zip is located")
sys.exit(1)
ZIPFILENAME = parse_zipfilename(args)
CURR_PATH = os.getcwd()
UNZIP_PATH = os.path.join(CURR_PATH, ZIPFILENAME)
if not args.single:
handle_group_mode(ZIPFILENAME, CURR_PATH, UNZIP_PATH, args)
else: # single user mode
handle_single_mode(ZIPFILENAME, CURR_PATH, UNZIP_PATH,args)
# create gradingfile for single users
# extract whole folder
# take first.. create folder from name
# if next startwith=same as last folder... extract into
# store takefirst in gradinglist
if __name__ == "__main__":
main()