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