1 #!/usr/bin/env python 2 3 """ 4 Add users to a MoinMoin wiki. 5 6 User details are read from standard input as a tab-separated sequence using the 7 following format: 8 9 USERNAME FULLNAME EMAIL IMAGE-URL 10 11 User details are written to standard output in a tab-separated sequence using 12 the following format: 13 14 USERNAME FULLNAME EMAIL IMAGE-URL PASSWORD 15 """ 16 17 from os.path import split 18 from subprocess import call 19 import random, string 20 import sys 21 import codecs 22 23 def randompass(): 24 return "".join(random.sample(string.ascii_letters, 10)) 25 26 def add_user(wiki, username, fullname, email, password): 27 cmd = ["moin", "--config-dir=%s" % wiki, "account", "create", 28 "--name=%s" % username, 29 "--email=%s" % email, 30 "--password=%s" % password] + \ 31 (fullname and ["--alias=%s" % fullname] or []) 32 call(cmd) 33 34 def main(): 35 progname = split(sys.argv[0])[-1] 36 37 try: 38 wiki = sys.argv[1] 39 except (IndexError, ValueError): 40 print >>sys.stderr, "%s <wiki configuration directory>" % progname 41 print >>sys.stderr 42 print >>sys.stderr, "Example: %s wiki" % progname 43 sys.exit(1) 44 45 stdin = codecs.getreader("utf-8")(sys.stdin) 46 stdout = codecs.getwriter("utf-8")(sys.stdout) 47 line = stdin.readline() 48 while line: 49 username, fullname, email, image = line.strip("\n ").split("\t") 50 password = randompass() 51 add_user(wiki, username, fullname, email, password) 52 print >>stdout, "\t".join([username, fullname, email, image, password]) 53 54 line = stdin.readline() 55 56 if __name__ == "__main__": 57 main() 58 59 # vim: tabstop=4 expandtab shiftwidth=4