|
| 1 | +import os |
| 2 | +import argparse |
| 3 | +import shutil |
| 4 | +import secrets |
| 5 | + |
| 6 | +def getArgs(): |
| 7 | + parser = argparse.ArgumentParser() |
| 8 | + parser.add_argument("semester", help="The semester_id where assignments will be placed (i.e. s20).") |
| 9 | + parser.add_argument("course", help="The course_id where assignments will be placed (i.e. sample).") |
| 10 | + parser.add_argument("gradeable", help="The gradeable_id where assignments will be placed (i.e. grades_released_homework_autohiddenEC).") |
| 11 | + args = parser.parse_args() |
| 12 | + return args |
| 13 | + |
| 14 | +def generateHashPrefix(nbytes = 8): |
| 15 | + return secrets.token_hex(nbytes) |
| 16 | + |
| 17 | +if __name__ == "__main__": |
| 18 | + args = getArgs() |
| 19 | + gradeable_path = f'/var/local/submitty/courses/{args.semester}/{args.course}/submissions/{args.gradeable}' |
| 20 | + assignments_path = f'{os.path.dirname(os.path.realpath(__file__))}/assignments' |
| 21 | + |
| 22 | + if not os.path.isdir(assignments_path) or not os.path.isdir(gradeable_path): |
| 23 | + raise SystemExit('The assignments directory or the gradeable does not exist. Please create those.') |
| 24 | + |
| 25 | + files_in_assign = os.listdir(assignments_path) |
| 26 | + users_for_placement = os.listdir(gradeable_path) |
| 27 | + num_to_place = min(len(users_for_placement), len(files_in_assign)) |
| 28 | + a_ind = 0 |
| 29 | + |
| 30 | + hash_prefix = generateHashPrefix() |
| 31 | + print(f'Assignment prefix is: {hash_prefix} (regex: {hash_prefix}_*)') |
| 32 | + for user_id in users_for_placement: |
| 33 | + current_path = f'{gradeable_path}/{user_id}' |
| 34 | + res = list(filter(lambda x: os.path.isdir(f'{current_path}/{x}'), os.listdir(current_path))) |
| 35 | + highest = len(res) + 1 |
| 36 | + new_path = f'{current_path}/{highest}' |
| 37 | + os.mkdir(new_path) |
| 38 | + ext = os.path.splitext(files_in_assign[a_ind])[1] |
| 39 | + shutil.copy2(f'assignments/{files_in_assign[a_ind]}', f'{new_path}/{hash_prefix}_{a_ind}{ext}') |
| 40 | + a_ind += 1 |
| 41 | + if a_ind == num_to_place: |
| 42 | + break |
0 commit comments