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

« back to all changes in this revision

Viewing changes to bin/ivle-enrolallusers

  • Committer: matt.giuca
  • Date: 2009-01-18 23:03:54 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1166
setup.install: Merged change from Storm branch (should have been committed
    directly to trunk). Small bugfix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
import optparse
35
35
import logging
36
36
 
37
 
import ivle.config
38
 
import ivle.database
 
37
import ivle.db
39
38
import ivle.pulldown_subj
40
39
 
41
40
p = optparse.OptionParser()
42
41
p.add_option('--user', '-u', metavar="<login>",
43
42
             help="Just perform enrolment for user <login>")
44
43
p.add_option('--verbose', '-v', action='store_true')
 
44
p.add_option('--year', '-y', metavar="<year>",
 
45
             help="If specified, year to make enrolments for (default: "
 
46
                  "current year)")
45
47
options, arguments = p.parse_args()
46
48
 
47
49
if options.verbose:
52
54
    print >> sys.stderr, "%s must be run as root" % sys.argv[0]
53
55
    sys.exit(1)
54
56
 
55
 
config = ivle.config.Config()
56
 
store = ivle.database.get_store(config)
57
 
 
58
 
if options.user is None:
59
 
    users = store.find(ivle.database.User).order_by(ivle.database.User.login)
60
 
else:
61
 
    users = [ivle.database.User.get_by_login(store, options.user)]
 
57
try:
 
58
    db = ivle.db.DB()
 
59
    if options.user is None:
 
60
        users = db.get_users()
 
61
    else:
 
62
        users = [db.get_user(options.user)]
 
63
    def repack(user):
 
64
        return (user.login, user.unixid)
 
65
    uids = dict(map(repack,users))
 
66
except Exception, message:
 
67
    logging.error(str(message))
 
68
    sys.exit(1)
62
69
 
63
70
if options.user is None:
64
71
    logging.info("enrolment started")
65
72
else:
66
73
    logging.info("enrolment started for user %s" % options.user)
67
74
 
 
75
if options.year is not None and not options.year.isdigit():
 
76
    logging.error("Year must be numeric")
 
77
    sys.exit(1)
 
78
 
 
79
users.sort(key=lambda user: user.login)
68
80
for user in users:
69
81
    try:
70
82
        # Get all subjects this user is enrolled in, and add them to the DB if
71
83
        # they match one of our local subject codes
72
 
        res = ivle.pulldown_subj.enrol_user(config, store, user)
 
84
        res = ivle.pulldown_subj.enrol_user(user.login, options.year)
73
85
        logging.info("Enrolled user %s in %d subject%s." % (user.login, res,
74
86
                        '' if res == 1 else 's'))
75
87
    except Exception, message:
76
88
        logging.warning(str(message))
77
89
        continue
78
90
    
79
 
store.commit()
80
91
logging.info("enrolment completed successfully")