C.A.T./generateUserlist.py

73 lines
3.3 KiB
Python

# Generate Userlist
#
# Rev 1.1, 10/22/22
# - changed the -y (year of enrolement) parameter to a more general -s (suffix)
# - updated the usage message to include that change
# - added a lot of comments, especially regarding the default values
# - removed the unused fileinput import
# - changed the defaults to fit more common scenarios
# Rev 1.0, 10/20/22
# - a simple script to generate CSV-files suitable as input
# for the importUsers script.
# - this script is rather specifically designed to accomodate
# the needs of our school's user naming scheme. Different
# naming schemes should be easy to implement though.
# - the script generates default passwords by picking three
# words from a file called WordList.txt living in the same
# directory as the script file.
# Options
# -d,--domain: domain name for the email address, this is required
# -g,--groups: comma-separated list of groups added to every user
# -s,--suffix: added to every username and the resulting filename
# in this release, the suffix is added to the last group name as well
# -f,--file: file of usernames, first and last name separated by comma
import sys, getopt
import csv
import os,io
import random
def main(argv):
wordList = open("WordList.txt").read().splitlines()
# add some default values for the options
groups = "Users" # add default groups for all imports here
suffix = ""
domainname="example.com"
filename = "names.csv"
# next is a list of characters common in users actual names but unsuitable for login names
# for this script, those are specific to german names. Spaces get replaced with dashes.
char_map = {ord("ä"):"ae", ord("ü"):"ue", ord("ö"):"oe", ord("ß"):"ss", ord(" "):"-"}
try:
opts, args = getopt.getopt(argv,"hg:d:s:f:",["help","groups=","domain=","suffix=","file="])
except getopt.GetoptError:
print("Usage: importUsers.py -d <domainname> [-g <groups>] [-s <common suffix for this import]> -f <datafile>")
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print("Usage: importUsers.py -d <domainname> [-g <groups>] -s <common suffix for this import> -f <datafile> ")
elif opt in ('-d', '--domain'):
domainname = arg
elif opt in ('-g', '--groups'):
groups = groups + "," + arg # as per importUsers specs, the groups are comma-separated
elif opt in ('-f', '--file'):
filename = arg
elif opt in ('-s', '--suffix'):
suffix = arg
groups = groups + suffix
outputFileName = "users" + suffix + ".csv"
with open(outputFileName,"w+",newline='') as outputFile:
header = ["first_name","last_name","email_address","sis_username","user_groups","password"]
writer = csv.DictWriter(outputFile,fieldnames=header,delimiter=";", quoting=csv.QUOTE_MINIMAL)
writer.writeheader()
users = open(filename,"r").read().splitlines()
for user in users:
first_name,last_name = tuple(user.split(","))
username = first_name.casefold().translate(char_map) + "." + last_name.casefold().translate(char_map) + suffix
address = username + "@" + domainname
password = "-".join(random.sample(wordList,3)) # pick three words at ramdom, join them with a dash
writer.writerow({"first_name":first_name,"last_name":last_name,"email_address":address,"sis_username":username,"user_groups":groups,"password":password})
if __name__ == "__main__":
main(sys.argv[1:])