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

« back to all changes in this revision

Viewing changes to enrolallusers.py

  • Committer: mattgiuca
  • Date: 2008-07-15 09:18:02 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:877
pulldown_subj/__init__.py: Added "enrol_user" function which pulls down a user
    and also enrols them in the database.
Added script enrolallusers.py which scriptulously performs automatic
enrolments of all users in the system, similar to remakeallusers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# IVLE - Informatics Virtual Learning Environment
 
3
# Copyright (C) 2007-2008 The University of Melbourne
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 
 
19
# Program: EnrolAllUsers
 
20
# Author:  Matt Giuca
 
21
# Date:    15/7/2008
 
22
 
 
23
# Script to add enrolments for all users on the system.
 
24
# Pulls from the configured subject pulldown module the subjects each student
 
25
# is enrolled in, and adds enrolments to the database.
 
26
# Does not remove any enrolments.
 
27
#
 
28
# Requires root to run.
 
29
 
 
30
import sys
 
31
import os
 
32
import common.db
 
33
import os.path
 
34
import pwd
 
35
import conf
 
36
import optparse
 
37
import logging
 
38
 
 
39
import pulldown_subj
 
40
 
 
41
p = optparse.OptionParser()
 
42
p.add_option('--user', '-u', metavar="<login>",
 
43
             help="Just perform enrolment for user <login>")
 
44
p.add_option('--verbose', '-v', action='store_true')
 
45
p.add_option('--year', '-y', metavar="<year>",
 
46
             help="If specified, year to make enrolments for (default: "
 
47
                  "current year)")
 
48
options, arguments = p.parse_args()
 
49
 
 
50
if options.verbose:
 
51
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
 
52
                        level=logging.DEBUG)
 
53
 
 
54
if os.getuid() != 0:
 
55
    print >> sys.stderr, "%s must be run as root" % sys.argv[0]
 
56
    sys.exit(1)
 
57
 
 
58
try:
 
59
    db = common.db.DB()
 
60
    if options.user is None:
 
61
        users = db.get_users()
 
62
    else:
 
63
        users = [db.get_user(options.user)]
 
64
    def repack(user):
 
65
        return (user.login, user.unixid)
 
66
    uids = dict(map(repack,users))
 
67
except Exception, message:
 
68
    logging.error(str(message))
 
69
    sys.exit(1)
 
70
 
 
71
if options.user is None:
 
72
    logging.info("enrolment started")
 
73
else:
 
74
    logging.info("enrolment started for user %s" % options.user)
 
75
 
 
76
if options.year is not None and not options.year.isdigit():
 
77
    logging.error("Year must be numeric")
 
78
    sys.exit(1)
 
79
 
 
80
users.sort(key=lambda user: user.login)
 
81
for user in users:
 
82
    try:
 
83
        # Get all subjects this user is enrolled in, and add them to the DB if
 
84
        # they match one of our local subject codes
 
85
        res = pulldown_subj.enrol_user(user.login, options.year)
 
86
        logging.info("Enrolled user %s in %d subject%s." % (user.login, res,
 
87
                        '' if res == 1 else 's'))
 
88
    except Exception, message:
 
89
        logging.warning(str(message))
 
90
        continue
 
91
    
 
92
logging.info("enrolment completed successfully")