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