Last active
May 12, 2021 16:54
-
-
Save MalteT/1f9815798a3530c87cfa8f2879d06d30 to your computer and use it in GitHub Desktop.
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 | |
| # Execute in a directory of your choice, like this: | |
| # > hwp2021pizzazz.py downloaded_all_submissions_from_moodle.zip | |
| # Dependencies | |
| # - patool (system) | |
| # - pyunpack (python) | |
| CORRECT_PDF = "Korrektur.pdf" | |
| TARGET = "target" | |
| GROUPS = { | |
| "99": ["", ""], | |
| } | |
| import sys | |
| import os | |
| import pathlib | |
| import subprocess | |
| import shutil | |
| from pyunpack import Archive | |
| 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): | |
| Archive(src).extractall(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, CORRECT_PDF)): | |
| status = 1 | |
| elif os.path.exists(os.path.join(submission_path, "done_here")): | |
| status = 2 | |
| 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)) | |
| if not os.path.exists(TARGET): | |
| if len(sys.argv) >= 2: | |
| 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment