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