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

« back to all changes in this revision

Viewing changes to makeuser.py

  • Committer: mattgiuca
  • Date: 2008-02-05 07:33:46 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:421
apps/fileservice: Added code to call interpret on the trampoline version of
    fileservice. (Disabled at the moment as it doesn't work).
common.interpret: Bugfix on filenames starting with '/' (relating to the
    previous revision).

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
import sys
31
31
import os
32
32
import os.path
33
 
import getopt
34
 
import random
 
33
import pwd
35
34
# Import modules from the website is tricky since they're in the www
36
35
# directory.
37
36
sys.path.append(os.path.join(os.getcwd(), 'www'))
38
37
import conf
39
 
import common.makeuser, common.db
40
 
 
41
 
# Requireds and optionals will be used to display the usage message
42
 
# AND do argument processing
43
 
# The names here must correspond to the fields in the database.
44
 
requireds = ["login", "fullname", "rolenm"]
45
 
optionals = [
46
 
    ('p', 'password', "Cleartext password for this user"),
47
 
    ('n', 'nick', "Display name (defaults to <fullname>)"),
48
 
    ('e', 'email', "Email address"),
49
 
    ('s', 'studentid', "Student ID")
50
 
]
51
 
 
52
 
if len(sys.argv) <= 3:
53
 
    # Nicely format the usage message using the optionals
54
 
    print ("Usage: python makeuser.py [OPTIONS] %s\n    OPTIONS"
55
 
        % ' '.join(['<%s>' % x for x in requireds]))
56
 
    for short, long, desc in optionals:
57
 
        t = "        -%s | --%s" % (short, long)
58
 
        print t + (' ' * max(28 - len(t), 2)) + desc
59
 
    sys.exit(1)
 
38
import common.makeuser
 
39
 
 
40
if len(sys.argv) <= 6:
 
41
    print "Usage: python makeuser.py <username> <password> <nick> " \
 
42
        "<fullname> <rolenm> <studentid>"
 
43
    sys.exit()
60
44
 
61
45
if os.getuid() != 0:
62
46
    print "Must run makeuser.py as root."
63
 
    sys.exit(1)
64
 
 
65
 
shorts = ''.join([o[0] + ":" for o in optionals])
66
 
longs = [o[1] + "=" for o in optionals]
67
 
opts, args = getopt.gnu_getopt(sys.argv[1:], shorts, longs)
68
 
opts = dict(opts)
69
 
 
70
 
# Get the dictionary of fields from opts and args
71
 
user = {}
72
 
for i in range(0, len(requireds)):
73
 
    user[requireds[i]] = args[i]
74
 
for short, long, _ in optionals:
 
47
    sys.exit()
 
48
 
 
49
username = sys.argv[1]
 
50
password = sys.argv[2]
 
51
nick = sys.argv[3]
 
52
fullname = sys.argv[4]
 
53
rolenm = sys.argv[5]
 
54
studentid = sys.argv[6]
 
55
 
 
56
try:
 
57
    # Resolve the user's username into a UID
 
58
    # Create the user if it does not exist
75
59
    try:
76
 
        user[long] = opts['-' + short]
 
60
        (_,_,uid,_,_,_,_) = pwd.getpwnam(username)
77
61
    except KeyError:
 
62
        if os.system("useradd '%s'" % username) != 0:
 
63
            raise Exception("Failed to add Unix user account")
78
64
        try:
79
 
            user[long] = opts['--' + long]
 
65
            (_,_,uid,_,_,_,_) = pwd.getpwnam(username)
80
66
        except KeyError:
81
 
            pass
82
 
login = user['login']
83
 
if 'nick' not in user:
84
 
    user['nick'] = user['fullname']
85
 
 
86
 
try:
 
67
            raise Exception("Failed to add Unix user account")
 
68
    # Make the user's jail
 
69
    common.makeuser.make_jail(username, uid)
87
70
    # Make the user's database entry
88
 
    common.makeuser.make_user_db(**user)
 
71
    common.makeuser.make_user_db(username, uid, password, nick, fullname,
 
72
        rolenm, studentid)
89
73
except Exception, message:
90
74
    print "Error: " + str(message)
91
75
    sys.exit(1)
92
76
 
93
 
print "Successfully created user %s (%s)." % (login, user['fullname'])
 
77
print "Successfully created user %s (%s)." % (username, fullname)