diff --git a/repos_file_generator b/repos_file_generator index 14eab0f99e255cc88c42a970d302bb32bb958152..325df97322b9e3bfde3c52301367764f31eb3797 100755 --- a/repos_file_generator +++ b/repos_file_generator @@ -4,6 +4,8 @@ import argparse import yaml import re import logging +import itertools +import collections email_regex = re.compile(r"^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$") @@ -17,10 +19,27 @@ def is_valid_email(email: str) -> bool: if __name__ == '__main__': parser = argparse.ArgumentParser(description='Repos file generator - create the PWM repos file from a list of emails') parser.add_argument('in_file', type=argparse.FileType('r'), default='-', help='name of the file containing a list of email adresses (- for stdin)') - parser.add_argument('repos_file', type=argparse.FileType('w'), default='repos_file.yml', help='name of the repos file') + parser.add_argument('repos_file', type=argparse.FileType('w'), default='-', help='name of the repos file') + parser.add_argument('--group-size', '-s', type=int, default=1, help='number of persons per group, if the number of emails is not divisible by the group-size, one group will have a smaller number of participant (unless --big-group is specified).') + parser.add_argument('--prefix', '-p', type=str, default=None, help='add a prefix to group names, automatically create group names from first email') + parser.add_argument('--big-group', '-b', action='store_true', help='if the number of emails is not divisible by the group-size, make a last group with more emails') + # In the case we want to implement input file parsers: # parser.add_argument('-f', '--field', help='name of the field containing emails if the input as several fields (e.g. CSV)') args = parser.parse_args() - email_list = [line.strip() for line in args.in_file if is_valid_email(line.strip())] # I do not get why I need to strip for validating email -> the regexp should take care of line return with $ - group_list = [{'emails': [email]} for email in email_list] + + # Get the email list and filter invalid emails + email_list = [line.strip() for line in args.in_file if is_valid_email(line.strip())] # TODO: I do not get why I need to strip for validating email -> the regexp should take care of line return with $ + + # Group emails according to group sizes + group_list = [list(itertools.islice(email_list, i, i+args.group_size)) for i in range(0, len(email_list), args.group_size)] + if args.big_group and len(group_list[-1]) < args.group_size and len(group_list) > 1: + group_list[-2].extend(group_list[-1]) + group_list.pop() + + # Format to YAML + if args.prefix: + group_list = [{'name': args.prefix + g[0].split('@',1)[0] ,'emails': g} for g in group_list] + else: + group_list = [{'emails': g} for g in group_list] yaml.safe_dump(group_list, args.repos_file)