-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_usernames.py
executable file
·50 lines (40 loc) · 1.66 KB
/
get_usernames.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# USAGE: python get_usernames.py participants.txt > badges.tsv
import codecs, os, random, string, sys, unicodedata
fn = sys.argv[1]
UTF8Reader = codecs.getreader('utf8')
handle = UTF8Reader(open(fn))
N=5
homebase = "/home/" # /ngschool/users/
gid = 1100
badgeline = "%s\t%s\t%s\t%s\n"
unameline = "%s:%s:%s:%s:%s,,,:%s:/bin/bash\n"
def normalize_char(c):
try:
cname = unicodedata.name(c)
cname = cname[:cname.index(' WITH')]
return unicodedata.lookup(cname)
except (ValueError, KeyError):
return c
def normalize(s):
return ''.join(normalize_char(c) for c in s)
# get writer
UTF8Writer = codecs.getwriter('utf8')
out1, out2 = open(fn+".badges.tsv", "w"), open(fn+".newusers", "w")
out1, out2 = UTF8Writer(out1), UTF8Writer(out2)
# process
#for uid, full_name in enumerate(sys.stdin, gid+1):
# first, family = full_name.lower().split()[:2]
for uid, info in enumerate(handle, gid+1):
first, family, affiliation = info[:-1].split('\t')
full_name = " ".join([first, family])
uname = normalize("%s%s"%(first[0].split()[0], family.split()[0]))
uname = uname.lower()
pwd = ''.join(random.sample(string.ascii_uppercase + string.ascii_lowercase + string.digits, N))
out1.write(badgeline%(full_name.strip(), uname, pwd, affiliation))#, uid, gid, os.path.join(homebase, uname)))
out2.write(unameline%(uname, pwd, uid, gid, full_name.strip(), os.path.join(homebase, uname)))
sys.stderr.write("""Now execute
- to create badges:
./tsv2badges.py badges.svg %s.badges.tsv
- to create user accounts:
while read line; do echo $line; echo $line | sudo newusers; done < %s.newusers\n"""%(fn, fn))