Created
December 2, 2021 02:58
-
-
Save robert-mcdermott/40118616a86dd52881e04998cc5fb12e to your computer and use it in GitHub Desktop.
Username and Email address generation script
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 | |
""" | |
Account naming rules | |
Rule 1: firstname + lastname is less 18 or less characters long total | |
== firstname + "." + lastname (john.doe) | |
if conflict append an integer (starting with 2) to the end of the username | |
=== firstname + "." + lastname + int (john.doe2) | |
Rule 2: firstname + lastname is greater than 18 characters long total *and* | |
firstname_initial + lastname <= 18 characters long total | |
== firstname_initial + "." + lastname (jimmy reallylongname = j.reallylongname) | |
if conflict append an integer (starting with 2) to the end of the username | |
== firstname_initial + "." + lastname (johnny reallylongname = j.reallylongname2) | |
Rule 3: firstname + lastname is greater than 18 characters long total *and* | |
firstname_initial + lastname > 18 characters long total | |
== firstname_initial + "." + lastname[truncated to 17 chars] (jimmy reallyverylongname = j.reallyverylongnam) | |
if conflict append an integer (starting with 2) to the end of the username | |
== firstname_initial + "." + lastname[truncated to 17 chars] (jimbo reallyverylongname = j.reallyverylongnam2) | |
""" | |
import sys | |
def generate(first_name, last_name, email_domain): | |
""" | |
Take a first name, last name and based on the defined rules print the new users | |
unique username and email address. | |
""" | |
first_name = first_name.replace(" ", "").lower() | |
last_name = last_name.replace(" ", "").lower() | |
email_domain = email_domain.lower() | |
if len(first_name + last_name) <= 18: | |
username = dupe_fix("{}.{}".format(first_name, last_name)) | |
print("sAMAccountName: {}".format(username)) | |
print("Email Address: {}@{}".format(username, email_domain)) | |
elif len(first_name[0] + last_name) <=18: | |
username = dupe_fix("{}.{}".format(first_name[0], last_name)) | |
print("sAMAccountName: {}".format(username)) | |
print("Email Address: {}@{}".format(username, email_domain)) | |
else: | |
username = dupe_fix("{}.{}".format(first_name[0], last_name[:17])) | |
print("sAMAccountName: {}".format(username)) | |
print("Email Address: {}@{}".format(username, email_domain)) | |
def dupe_fix(username): | |
""" | |
Check to see if the generanted username is an exising username and if so generate | |
a non conflicting username by appeneding an integer until if finds a unique username. | |
""" | |
conflict = 0 | |
while True: | |
if username not in existing_usernames: | |
break | |
conflict += 1 | |
if username[-1].isdigit(): | |
username = "{}{}".format(username[:-1], conflict + 1) | |
else: | |
username = "{}{}".format(username, conflict + 1) | |
return(username) | |
#This is just to demo username collision detection, in production you'd need to retrive | |
#your alias/username list externally from this script | |
existing_usernames = ['john.doe', | |
'jane.doe', | |
'robert.mcdermott', | |
'robert.mcdermott2', | |
'c.trakarnsilpa', | |
'j.reallylongname', | |
'j.reallylongerlastn'] | |
if __name__ == "__main__": | |
if len(sys.argv) < 3: | |
print("Usage: {} firstname lastname".format(sys.argv[0])) | |
sys.exit(1) | |
email_domain = "fredhutch.org" | |
first_name = sys.argv[1] | |
last_name = sys.argv[2] | |
generate(first_name, last_name, email_domain) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment