Last active
June 9, 2016 19:15
-
-
Save dhardtke/b8ef18ac9c0bc23a29f8b9bfc033f3fa to your computer and use it in GitHub Desktop.
Simple Python 3 script that generates zip files for submission for the tu darmstadt informatik moodle's CER course
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
__author__ = "Dominik Hardtke" | |
__license__ = "MIT" | |
__version__ = "1.0.0" | |
__email__ = "[email protected]" | |
__status__ = "Production" | |
import os | |
import shutil | |
import zipfile | |
members = { | |
"1234567": "Max Mustermann", | |
"7654321": "Erika Mustermann", | |
"7162534": "Natasha Raskolnikowa" | |
} | |
src_dir = os.path.join(".", "files") | |
dst_dir = os.path.join(".", "output") | |
# end of config reached | |
# simple validity checks | |
if len(members) < 2 or len(members) > 3: | |
raise ValueError("Invalid length of members dict: %s" % len(members)) | |
if not os.path.isdir(src_dir): | |
raise ValueError("%s does not exist or could not be read" % src_dir) | |
# create output folder if necessary | |
if not os.path.isdir(dst_dir): | |
os.makedirs(dst_dir) | |
# truncate output folder | |
files = [f for f in os.listdir(dst_dir)] | |
for f in files: | |
os.remove(os.path.join(dst_dir, f)) | |
def get_identifier(mnr, name): | |
components = name.split(" ") | |
return components[0][0].upper() + components[1][0].upper() + mnr.strip()[-2:] | |
# function based on http://stackoverflow.com/questions/14568647/create-zip-in-python/14569017#14569017 | |
def make_zip(matrikelnummer_m, src, dst, filename): | |
zf = zipfile.ZipFile(os.path.join(dst, "%s.zip" % (filename)), "w", zipfile.ZIP_DEFLATED) | |
abs_src = os.path.abspath(src) | |
for dirname, subdirs, files in os.walk(src): | |
for filename in files: | |
absname = os.path.abspath(os.path.join(dirname, filename)) | |
arcname = absname[len(abs_src) + 1:] | |
# do not zip 'matrikelnummer.m' since we write it into the zip manually (see below) | |
if arcname == "matrikelnummer.m": | |
continue | |
zf.write(absname, arcname) | |
# write 'matrikelnummer.m' | |
zf.writestr("matrikelnummer.m", matrikelnummer_m) | |
zf.close() | |
for mnr in members: | |
name = members[mnr] | |
others = members.copy() | |
others.pop(mnr) | |
zip_filename = get_identifier(mnr, name) | |
for other_mnr in others: | |
zip_filename += "_" + get_identifier(other_mnr, members[other_mnr]) | |
matrikelnummer_m = "\n".join([ | |
"%-----------------------------------------------------------------", | |
"% Die nachfolgende Funktion liefert als Rückgabewert die Matrikelnummer", | |
"% und den Namen des/der abgebenden Studenten/Studentin.", | |
"%-----------------------------------------------------------------", | |
"function [m, n, g] = matrikelnummer()", | |
" % Bitte tragen Sie hier Ihre Matrikelnummer ein:", | |
" m = %s;" % mnr, | |
" % Bitte tragen Sie hier Ihren Namen ein:", | |
" n = '%s';" % name, | |
" % Bitte tragen Sie hier zusätzlich die Matrikelnummern der Gruppen-", | |
" % mitglieder ein, mit denen Sie zusammengearbeitet haben oder lassen Sie", | |
" % die Variable unverändert:", | |
" g = [m, %s];" % ", ".join(others.keys()), | |
"end" | |
]) | |
make_zip(matrikelnummer_m, src_dir, dst_dir, zip_filename) | |
print(zip_filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment