170 lines
5.4 KiB
Python
Executable file
170 lines
5.4 KiB
Python
Executable file
#! /usr/bin/env nix-shell
|
|
#! nix-shell -i python -p python39 p7zip
|
|
|
|
# Execute in a directory of your choice, like this:
|
|
# > hwp2021pizzazz.py downloaded_all_submissions_from_moodle.zip
|
|
|
|
CORRECT_PDF = "Korrektur.pdf"
|
|
DONE_PDF = "Korrektur-export.pdf"
|
|
TARGET = "target"
|
|
GROUPS = {
|
|
"44": ["Gerhard Wilhelm", "Paul Quast"],
|
|
"45": ["Moussa Rais", "mohamad jaara"],
|
|
"46": ["Raoul Kirchner", "Paul Bachmann"],
|
|
"47": ["Dominik Masson", "Christopher Helbig"],
|
|
"48": ["Mats Brockmann", "Max Martin Freiberg"],
|
|
"49": ["Ahmad Alahmad", "Leo Dirk Stiebling"],
|
|
"50": ["Dorjan Domi", "Matthias Schütze - Stuhr"],
|
|
"51": ["Phil-Alexander Hofmann", "Leo Haßbargen"],
|
|
"52": ["Tom Wawerek", "Mose Schmiedel"],
|
|
"53": ["Cedric Klaus", "Martin Götze"],
|
|
"54": ["Dharshan Seesurn", "Anna Matusevich"],
|
|
"55": ["Yaowei Zhang", "Yuechunqiu Wu"],
|
|
"56": ["Mahmoud Sheikh-bakri", "Christopher Käding"],
|
|
"57": ["Melanie Qaiser", "William Albert"],
|
|
"58": ["Nick Meier", "Marco Zwillus"],
|
|
"59": ["Patric Riedel", "Rodrigue Ekango"],
|
|
}
|
|
|
|
import sys
|
|
import os
|
|
import pathlib
|
|
import subprocess
|
|
import shutil
|
|
|
|
def run(*args, cwd=None):
|
|
res = subprocess.run(list(args), cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
if res.returncode != 0:
|
|
print(*args, "failed with", res.returncode, "..")
|
|
print("-.- STDOUT -.-")
|
|
print(res.stdout)
|
|
print("-.- STDERR -.-")
|
|
print(res.stderr)
|
|
sys.exit(2)
|
|
|
|
def remove_tree(path):
|
|
shutil.rmtree(path)
|
|
|
|
def flatten(l):
|
|
new = []
|
|
for ll in l:
|
|
for el in ll:
|
|
new.append(el)
|
|
return new
|
|
|
|
def delete_garbage_dirs():
|
|
print("Removing much stuff (garbage and your home directory)..")
|
|
dirs = os.listdir(TARGET)
|
|
names = flatten(GROUPS.values())
|
|
for d in dirs:
|
|
found = False
|
|
for name in names:
|
|
if name in d:
|
|
found = True
|
|
break
|
|
if not found:
|
|
remove_tree(os.path.join(TARGET, d))
|
|
|
|
|
|
def unzip_main(main_file):
|
|
print("Unzipping main file..")
|
|
os.mkdir(TARGET)
|
|
extract_archive(main_file, TARGET)
|
|
|
|
|
|
def extract_archive(src, dst):
|
|
shutil.unpack_archive(src, dst)
|
|
#patoolib.extract_archive(src, outdir=dst)
|
|
|
|
|
|
def print_submission_info():
|
|
print("")
|
|
for group, usernames in GROUPS.items():
|
|
# Submission exists?
|
|
group_path = os.path.join(TARGET, group)
|
|
submissions = len(os.listdir(group_path))
|
|
if submissions == 0:
|
|
submission_reaction = "nope"
|
|
elif submissions == 1:
|
|
submission_reaction = "YEA!"
|
|
else:
|
|
submission_reaction = "wHaT"
|
|
# Corrected?
|
|
status = 0
|
|
for submission in os.listdir(group_path):
|
|
submission_path = os.path.join(group_path, submission)
|
|
if os.path.exists(os.path.join(submission_path, DONE_PDF)):
|
|
status = 2
|
|
elif os.path.exists(os.path.join(submission_path, CORRECT_PDF)) and status < 1:
|
|
status = 1
|
|
if status == 0:
|
|
print('{}: {} [ ]'.format(group, submission_reaction))
|
|
elif status == 1:
|
|
print('{}: {} [~]'.format(group, submission_reaction))
|
|
elif status == 2:
|
|
print('{}: {} [X]'.format(group, submission_reaction))
|
|
|
|
|
|
def sort_into_group_dirs():
|
|
print("Shuffling submissions..")
|
|
dirs = os.listdir(TARGET)
|
|
for group, usernames in GROUPS.items():
|
|
group_folder = os.path.join(TARGET, group)
|
|
os.mkdir(group_folder)
|
|
for d in dirs:
|
|
d_path = os.path.join(TARGET, d)
|
|
for username in usernames:
|
|
if username in d:
|
|
shutil.move(d_path, group_folder)
|
|
|
|
|
|
def extract_submissions():
|
|
print("Unzipping student presents..")
|
|
for group in GROUPS.keys():
|
|
group_path = os.path.join(TARGET, group)
|
|
for submission in os.listdir(group_path):
|
|
sub_path = os.path.join(group_path, submission)
|
|
for archive in os.listdir(sub_path):
|
|
archive_path = os.path.join(sub_path, archive)
|
|
extract_archive(archive_path, sub_path)
|
|
|
|
|
|
def copy_pdfs_for_correction():
|
|
print("Finding the pdfs..")
|
|
for group in GROUPS.keys():
|
|
group_path = os.path.join(TARGET, group)
|
|
for submission in os.listdir(group_path):
|
|
sub_path = os.path.join(group_path, submission)
|
|
pdfs = list(pathlib.Path(sub_path).glob("**/*.pdf"))
|
|
if len(pdfs) == 1:
|
|
shutil.copy(pdfs[0], os.path.join(sub_path, CORRECT_PDF))
|
|
|
|
def extractfiles(zipname, output_dir):
|
|
"""Extract files with 7z utils."""
|
|
res = subprocess.run(["7z", "x", "-aoa", "-bd", zipname, f"-o{output_dir}"], stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
|
|
return(res)
|
|
|
|
|
|
def register_extensions():
|
|
"""Register additionnal archive formats supported by 7zip in shutil."""
|
|
shutil.register_unpack_format('rar', ['.rar', '.RAR'], extractfiles)
|
|
shutil.register_unpack_format('7z', ['.7z', '.7Z'], extractfiles)
|
|
|
|
|
|
if not os.path.exists(TARGET):
|
|
if len(sys.argv) >= 2:
|
|
register_extensions()
|
|
main_file = sys.argv[1]
|
|
unzip_main(main_file)
|
|
delete_garbage_dirs()
|
|
sort_into_group_dirs()
|
|
extract_submissions()
|
|
copy_pdfs_for_correction()
|
|
else:
|
|
print("Give me much main zip.. moron!")
|
|
sys.exit(1)
|
|
else:
|
|
print("Skipping the zipping so no data lost for hooman!")
|
|
|
|
print_submission_info()
|