~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to bin/ivle-remakeuser

  • Committer: David Coles
  • Date: 2010-08-30 03:26:13 UTC
  • Revision ID: coles.david@gmail.com-20100830032613-d14vng0jkelniu3l
python-console: Fix globals broken with new JSON library.

simplejson always returns unicode strings. cJSON would return ordinary strings 
if possible. cPickle.loads() only accepts strings. At present we use pickle 
version 0 so they should all works as ASCII strings. Higher versions of pickle 
are not plain ASCII and are likely to break this and so this should be fixed 
at some point.

Also replaced unconditional exception with one that catches Pickle errors. Not 
sure the best way to report failures of these functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import os
29
29
import optparse
30
30
import logging
 
31
import traceback
31
32
 
32
 
import ivle.db
 
33
import ivle.config
 
34
import ivle.database
33
35
import ivle.makeuser
34
36
 
35
37
p = optparse.OptionParser()
48
50
                          os.path.basename(sys.argv[0])
49
51
    sys.exit(1)
50
52
 
51
 
try:
52
 
    db = ivle.db.DB()
53
 
    if options.all:
54
 
        list = db.get_users()
55
 
    else:
56
 
        if len(arguments) == 0:
57
 
            print >> sys.stderr, "must be run with -a or a username"
58
 
            sys.exit(1)
59
 
        list = [db.get_user(arguments[0])]
60
 
    res = db.get_all('login', ['login', 'unixid'])
61
 
    def repack(flds):
62
 
        return (flds['login'], flds['unixid'])
63
 
    uids = dict(map(repack,res))
64
 
except Exception, message:
65
 
    logging.error(str(message))
66
 
    sys.exit(1)
 
53
config = ivle.config.Config()
 
54
store = ivle.database.get_store(config)
 
55
 
 
56
if options.all:
 
57
    users = store.find(ivle.database.User).order_by(ivle.database.User.login)
 
58
else:
 
59
    if len(arguments) == 0:
 
60
        print >> sys.stderr, "must be run with -a or a username"
 
61
        sys.exit(1)
 
62
    users = [ivle.database.User.get_by_login(store, arguments[0])]
67
63
 
68
64
logging.info("rebuild started")
69
65
 
70
 
list.sort(key=lambda user: user.login)
71
 
for user in list:
72
 
    login = user.login
73
 
 
 
66
for user in users:
74
67
    try:
75
 
        # Resolve the user's login into a UID
76
 
        try:
77
 
            uid = uids[login]
78
 
        except KeyError:
79
 
            raise Exception("user %s does not have a unixid in the database"
80
 
                % login)
81
 
        # Remake the user's jail
82
 
        ivle.makeuser.make_jail(login, uid)
 
68
        ivle.makeuser.make_jail(user, config)
83
69
    except Exception, message:
84
 
        logging.warning(str(message))
 
70
        logging.error("Failed to recreate jail for %s.\n%s" % 
 
71
                      (user.login, traceback.format_exc()))
85
72
        continue
86
73
 
87
 
    logging.debug("recreated user %s's jail." % login)
 
74
    logging.debug("recreated user %s's jail." % user.login)
88
75
    
89
76
logging.info("rebuild completed successfully")