diff --git a/scripts/validate_groups b/scripts/validate_groups index df8be23..6b6f02c 100755 --- a/scripts/validate_groups +++ b/scripts/validate_groups @@ -1,265 +1,266 @@ #!/usr/bin/env python3 import os import subprocess import mailer import argparse import getpass import random import Slides.class_helper as ch config = ch.get_class_config() parser = argparse.ArgumentParser( description='Validate groups for homeworks') parser.add_argument('-n', '--homework', type=str, help='specify the homework label', required=True) parser.add_argument('-t', '--target', type=str, help='Specify the name of a mail box' ' to send test messages,' 'default : guillaume.anciaux@epfl.ch', default=config['teachers'][0]) parser.add_argument('-u', '--username', type=str, help='Username to log to the SMTP server', default=None) args = parser.parse_args() students_list = config['students'] try: args.homework = int(args.homework) except: pass + homework = config['homeworks'][args.homework] group_list = os.path.join(ch.get_git_root(), homework['groups']) target = args.target username = args.username password = None if username: print('login:', username) password = getpass.getpass() lines = open(group_list).readlines() students_registered_in_group = set() groups = {} for l in lines[1:]: columns = l.split('|')[1:] if len(columns) < 6: continue columns = [e.strip() for e in columns] # print(columns) columns[5] = columns[5].replace('https://c4science.ch', 'ssh://git@c4science.ch') # print(columns[5]) email1 = columns[2] email2 = columns[4] emails = set() emails.add(email1) emails.add(email2) if len(emails) < 2: # raise RuntimeError( # 'invalid group: ' + str(emails)) print(emails) emails = list(emails) emails.sort() group_key = '_'.join(emails) # print(group_key) groups[group_key] = columns students_registered_in_group.add(email1) students_registered_in_group.add(email2) # print(groups) # print(len(students_registered_in_group)) # ############################################################### # prepare to clone try: os.mkdir('homeworks') except Exception: pass # ############################################################### # search for failing to clone groups groups_failing_to_clone = set() for key, val in groups.items(): dirname = './homeworks/' + key if os.path.isdir(dirname): print('Success: group ' + key) continue cmd = val[5] + ' ' + dirname # print(cmd) process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = process.communicate() output = [i.decode().strip() for i in output if i.decode().strip() != ''] output = '\n'.join(output) ret = process.returncode if ret != 0: print('Failed: group ' + key) print(cmd) print(output) groups_failing_to_clone.add(key) else: print('Success: group ' + key) for g in groups_failing_to_clone: group = groups[g] group_emails = [group[2], group[4]] repo = group[5] if username: mailer.mail( username=username, password=password, sender_email=target, subject='SP4E homeworks: error in cloning your project', teachers=teachers, message=""" Dear Students, Apparently we cannot clone your repository: {0} Please fix the permissions. Best regards, The teaching team. """.format(repo), target_emails=group_emails ) # ############################################################### # search for unregistered students lines = open(students_list).readlines() students = set() for l in lines[2:]: columns = [i.strip() for i in l.split('|') if i.strip() != ''] if len(columns) < 5: continue print(columns) students.add(columns[5]) illegally_registered_student = ( students_registered_in_group - students_registered_in_group.intersection(students)) ################################################################ # construct invalid groups invalid_groups = set() for g in groups: group = groups[g] group_emails = set([group[2], group[4]]) if len(group_emails.intersection(illegally_registered_student)) > 0: students_registered_in_group -= group_emails invalid_groups.add(g) if username: for g in invalid_groups: group = groups[g] group_emails = set([group[2], group[4]]) mailer.mail( username=username, password=password, sender_email=target, subject='SP4E homeworks: invalid group', message=""" Dear Students, Your group is composed with at least a student not officially registered for the class. Therefore I have to ask you to change the composition of the group. You have to understand that the grading of so many projects is a lot of work. Therefore we will do it only for the registered students. With my best regards, The teaching team. """, teachers=teachers, target_emails=group_emails ) unregistered_list = students - students_registered_in_group ################################################################ # send email to unregistered people unregistered_list = list(unregistered_list) # print(unregistered_list) random.shuffle(unregistered_list) # print(unregistered_list) if len(unregistered_list): print('aa', unregistered_list) if len(unregistered_list) % 2 == 0: random_groups = [e for e in zip( unregistered_list[::2], unregistered_list[1::2])] else: random_groups = [e for e in zip( unregistered_list[:-1:2], unregistered_list[1:-1:2])] random_groups[-1] = random_groups[-1][0], random_groups[-1][1], unregistered_list[-1] random_groups = [" - {0}".format(", ".join(b)) for b in random_groups] print('bb', random_groups) if username and len(unregistered_list) > 0: mailer.mail( username=username, password=password, sender_email=target, subject='SP4E homeworks: not registered in a group', message=""" Dear Students, Apparently you did not registered yet to any group. Several reasons might explain this. If it happens that you need to find a pair, please find below the automatically created groups. {0} If the situation does not suit you, please inform us as quickly as possible. You still have to create a repository to store your homework. Please inform us of your repository URI, for instance by filling the [form](https://docs.google.com/forms/d/e/1FAIpQLSfymokpXgx4rhYY8msKLKXpY2sAbcv5n6cBiv1Ngx3Lu0Ob1A/viewform?vc=0&c=0&w=1&flr=0&usp=mail_form_link) With my best regards, The teaching team. """.format('\n'.join(random_groups)), teachers=teachers, target_emails=unregistered_list, markdown=True ) ################################################################ # output info print('total students:', len(students)) print('registered students:', len(students_registered_in_group)) print('unregistered students:', len(unregistered_list)) print('illegally registered students:', len(illegally_registered_student)) print('invalid groups:', len(invalid_groups))