r22880 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r22879‎ | r22880 | r22881 >
Date:13:31, 10 June 2007
Author:Mark Bergsma
Status:old
Tags:
Comment:
- Slightly better error messages
- Check for empty passwords (prompt)
- More elaborate help screens
- Allow db path as argument
Modified paths:
  • /trunk/wmfmailadmin/wmfmailadmin.py (modified) (history)

Diff [purge]

Index: trunk/wmfmailadmin/wmfmailadmin.py
@@ -5,9 +5,9 @@
66 Written by Mark Bergsma <mark@wikimedia.org>
77 """
88
9 -import sys, os
 9+import sys, os, sqlite3
1010
11 -dbname = 'user.db'
 11+dbname = '/var/vmail/user.db'
1212 conn = None
1313
1414 actions = {
@@ -20,14 +20,14 @@
2121 longactions = {}
2222
2323 fieldmappings = {
24 - # Option: ( fieldname, description, default )
25 - 'e': ('email', "E-mail address", None),
26 - 'p': ('password', "Password", '-'),
27 - 'r': ('realname', "Real name", None),
28 - 'i': ('id', "Id", None),
29 - 'q': ('quota', "Quota", 2**30/1024),
30 - 'a': ('active', "Active", True),
31 - 'f': ('filter', "Filter", None)
 24+ # Option: ( fieldname, description, default, explanation )
 25+ 'e': ('email', "E-mail address", None, None),
 26+ 'p': ('password', "Password", '-', "Password hash or '-' for prompting"),
 27+ 'r': ('realname', "Real name", None, None),
 28+ 'i': ('id', "Id", None, None),
 29+ 'q': ('quota', "Quota", 2**30/1024, None),
 30+ 'a': ('active', "Active", True, None),
 31+ 'f': ('filter', "Filter", None, None)
3232 }
3333 longmappings = {}
3434
@@ -161,13 +161,13 @@
162162
163163 def password_input(fields):
164164 """
165 - Checks if the password argument on the commandline was "-",
 165+ Checks if the password argument on the commandline was "-" or empty,
166166 and prompts for a password if that is the case
167167 """
168168
169169 global supported_hash_algorithms
170170
171 - if fields['password'] != '-': return
 171+ if fields['password'] not in ('', '-'): return
172172
173173 # Simply outsource to dovecotpw
174174 pipe = os.popen('dovecotpw -s sha1', 'r')
@@ -195,7 +195,6 @@
196196 Creates a connection to the database
197197 """
198198
199 - import sqlite3
200199 global conn
201200
202201 conn = sqlite3.connect(dbname)
@@ -208,7 +207,7 @@
209208
210209 global actions, fieldmappings
211210
212 - print "Usage:", sys.argv[0], "[ACTION] [FIELDS]"
 211+ print "Usage:", sys.argv[0], "[ACTION] [FIELDS] [dbfile]"
213212
214213 print "\nActions:"
215214 for a, action in actions.iteritems():
@@ -216,7 +215,7 @@
217216
218217 print "\nFields:"
219218 for f, field in fieldmappings.iteritems():
220 - print " -%s <...> --%-10s\t%s" % (f, field[0], field[1])
 219+ print " -%s <...> --%-10s\t%s" % (f, field[0], field[3] or field[1])
221220
222221 def parse_arguments():
223222 """
@@ -224,7 +223,7 @@
225224 """
226225
227226 import getopt
228 - global actions, fieldmappings
 227+ global actions, fieldmappings, dbname
229228
230229 # Build option list
231230 options = "".join(actions.keys() + [c+':' for c in fieldmappings.keys()]) + "h"
@@ -238,6 +237,10 @@
239238 # Print help information and exit
240239 print_usage()
241240 sys.exit(2)
 241+
 242+ # (First, optional) argument should be dbfile
 243+ if len(args) > 0 and args[0] != "":
 244+ dbname = args[0]
242245
243246 # Parse options
244247 action, fields = None, {}
@@ -266,36 +269,51 @@
267270 print_usage()
268271 sys.exit(2)
269272
270 - return action, fields
 273+ if action is None:
 274+ print_usage()
 275+ sys.exit(2)
 276+ else:
 277+ return action, fields
271278
272279 def main():
273280 """
274281 Main function
275282 """
276283
277 - global longactions, longmappings
 284+ global longactions, longmappings, dbname
278285
279286 longactions, longmappings = add_index(actions, 0), add_index(fieldmappings, 0)
280287 action, fields = parse_arguments()
281288
282289 if action is not None:
283 - connect_db()
 290+ try:
 291+ connect_db()
 292+ except sqlite3.OperationalError, e:
 293+ print >> sys.stderr, "Can't open database file %s: %s" % (dbname, e.message)
 294+ sys.exit(2)
284295
285296 # Split e-mail address into localpart and domain fields
286297 if 'email' in fields: split_email(fields)
287298
288 - if action == 'list':
289 - list_accounts(fields)
290 - elif action == 'create':
291 - create_account(fields)
292 - print "Account added:"
293 - list_accounts(fields)
294 - elif action == 'delete':
295 - delete_account(fields)
296 - elif action == 'update':
297 - update_account(fields)
298 - print "Account updated:"
299 - list_accounts(fields)
 299+ try:
 300+ if action == 'list':
 301+ list_accounts(fields)
 302+ elif action == 'create':
 303+ create_account(fields)
 304+ print "Account added:"
 305+ list_accounts(fields)
 306+ elif action == 'delete':
 307+ delete_account(fields)
 308+ elif action == 'update':
 309+ update_account(fields)
 310+ print "Account updated:"
 311+ list_accounts(fields)
 312+ except sqlite3.IntegrityError, e:
 313+ print >> sys.stderr, "SQL integrity error. Account does already exist? (%s)" % e.message
 314+ sys.exit(2)
 315+ except Exception, e:
 316+ print >> sys.stderr, "Error:", e.message
 317+ sys.exit(2)
300318
301319 if __name__ == "__main__":
302320 main()
\ No newline at end of file

Status & tagging log