diff --git a/repos_file_generator b/repos_file_generator new file mode 100755 index 0000000000000000000000000000000000000000..372b0f938b4f32f5de3409ff9381efa775dbb37d --- /dev/null +++ b/repos_file_generator @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import argparse +import yaml +import re +import logging + + +email_regex = re.compile(r'^([A-Za-z0-9]+[.-_])*[A-Za-z0-9]+@[A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+$') +def is_valid_email(email: str) -> bool: + is_valid = re.fullmatch(email_regex, email) is not None + if not is_valid: + logging.warning(f'"{email.strip()}": is not a valid email -> not added in a group') + return is_valid + + +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') + # 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] + yaml.safe_dump(group_list, args.repos_file)