Attachment 'usercreationtool_1.7.2_(09-03-12).py'

Download

   1 #!/usr/bin/env python
   2 # -*- coding: iso-8859-1 -*-
   3 """
   4 MoinMoin - batch user creation tool
   5 GPL code written by TheAnarcat, december 2005
   6 GPL code apdated by Renard, november 2008 (for moinmoin v 1.7.2)
   7 
   8 This tool allows batch creation of users in a wiki. You need to modify a few
   9 settings below in order to get going, see below.
  10 
  11 Generally, the idea is to populate a page with a list of users, or a
  12 dictionnary (UserName:: email) of users, and run this script on it. See the -h
  13 flag for more information.
  14 """
  15 
  16 import sys, re
  17 
  18 # ----------------------------------------------------------------------------
  19 # CHECK THESE SETTINGS, then remove or comment out the following line:
  20 #print "Check the settings in the script first, please!" ; sys.exit(1)
  21 
  22 # this is where your moinmoin code is (if you installed it using
  23 # setup.py into your python site-packages, then you don't need that setting):
  24 #sys.path.insert(0, 'C:\tools\moin\MoinMoin')
  25 
  26 # this is where your wikiconfig.py is:
  27 wiki_url = 'C:\tools\moin\mywiki'
  28 sys.path.insert(0, wiki_url)
  29 
  30 # if you include other stuff in your wikiconfig, you might need additional
  31 # pathes in your search path. Put them here:
  32 #sys.path.insert(0, '/org/wiki')
  33 
  34 # This is the list of accounts to create
  35 magicpage = "UserToCreateMagicPage"
  36 
  37 # ----------------------------------------------------------------------------
  38 
  39 from MoinMoin.user import *
  40 from MoinMoin import config, wikiutil, Page
  41 #commented for 1.7.2 from MoinMoin.script import _util
  42 from MoinMoin.request import request_cli
  43 from MoinMoin.wikidicts import Group, Dict
  44 from os import popen3
  45 from sys import stderr, stdout
  46 
  47 def random_pass(passlen = 8):
  48     """Generate a random password using pwgen or some homebrewed recipe."""
  49     stdin, stdout, stderr = popen3("pwgen")
  50     out = stdout.readline() or ""
  51     if not out:
  52         from random import choice
  53         for i in range(passlen):
  54            out += chr(choice(range(ord('0'), ord('z'))))
  55     return out.strip()
  56 
  57 def run():
  58     disableuser = save = dict = group = 0
  59 
  60     if "--disableuser" in sys.argv:  disableuser = 1
  61     if "--save" in sys.argv or "-s" in sys.argv:  save = 1
  62     if "--group" in sys.argv or "-g" in sys.argv: group = 1
  63     if "--dict" in sys.argv or "-d" in sys.argv: dict = 1
  64 
  65     if "--help" in sys.argv or "-h" in sys.argv or not dict and not group:
  66         print """%s
  67 Options:
  68         --disableuser       disable the user with user id uid
  69                             this can't be combined with the options above!
  70         -s --save           if specified, save the accounts to disk
  71                             if not specified, no user will be added
  72         -g --group          parse the list in the magicpage
  73         -d --dict           parse the dict in the magicpage
  74 
  75 One of -g or -d must be specified.
  76     """ % sys.argv[0]
  77         return
  78         
  79 #commented for 1.7.2    if wiki_url:
  80 #commented for 1.7.2        request = Request(wiki_url)
  81 #commented for 1.7.2    else:
  82     request = request_cli.Request()
  83 
  84     # look for usernames to create in magicpage
  85     page = Page.Page(request, magicpage)
  86     if not page.exists():
  87         raise ValueError("page " + magicpage + " does not exist")
  88 
  89     # load users from the dict and/or groups in the magic page
  90     users = {}
  91     if dict:
  92         g = Dict(request, "Testgroup")
  93         g.initFromText(page.get_raw_body())
  94         users.update(g)
  95     if group:
  96         g = Group(request, "TestGroup")
  97         g.initFromText(page.get_raw_body())
  98         users.update(g)
  99 
 100     for username, email in users.iteritems():
 101         username = username.strip()
 102         u = User(request, None, username)
 103         # output a formal list of treated usernames
 104         stdout.write(" * " + u.name)
 105         # skip existing users
 106         if u.exists():
 107             stdout.flush()
 108             # mark skipped usernames on stderr so that the list is still valid
 109             stderr.write(" (skipped)")
 110             stdout.write("\n")
 111             continue
 112 
 113         # we need to recreate the user because the id will not be autogenerated
 114         # if the usesname is passed as arg
 115         u = User(request, None, None, random_pass())
 116         # set various parameters
 117         u.name = username
 118         u.disabled = disableuser
 119         if email != 1:
 120                 u.email = email
 121         # close this line
 122         stdout.write("\n")
 123         if save:
 124             u.save()
 125 
 126     if not save:
 127         print "not saved"
 128 
 129 if __name__ == "__main__":
 130     run()
 131 
 132 # EOF

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-11-19 18:25:13, 4.4 KB) [[attachment:usercreationtool_1.5.0_(may_be).py]]
  • [get | view] (2008-11-19 18:25:30, 4.5 KB) [[attachment:usercreationtool_1.7.2_(08-11-19).py]]
  • [get | view] (2009-03-12 06:04:07, 4.5 KB) [[attachment:usercreationtool_1.7.2_(09-03-12).py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.