source: fedd/user_to_project.py @ 19a3e06

axis_examplecompt_changesinfo-opsversion-2.00version-3.01version-3.02
Last change on this file since 19a3e06 was ff2d928, checked in by Ted Faber <faber@…>, 16 years ago

mysql error handling

  • Property mode set to 100755
File size: 1.2 KB
Line 
1#!/usr/local/bin/python
2
3import os, sys
4import MySQLdb
5from optparse import OptionParser
6
7if len(sys.argv) == 3:
8    user, project = sys.argv[1:3]
9else:
10    sys.exit("Usage %s user project" % sys.argv[0])
11
12try:
13    db = MySQLdb.connect(db='tbdb')
14except Exception, e:
15    if len(e.args) == 2:
16        num, str = e.args
17    else:
18        str = unicode(e)
19        num = -1
20    sys.exit("Cannot connect: %s (%d)" % (str, num))
21
22c = db.cursor()
23c.execute("select uid, uid_idx from users where uid=%s", (user,))
24
25uids = c.fetchall()
26
27if len(uids) == 1:
28    uid, uid_idx = uids[0]
29else:
30    sys.exit("User is not unique??")
31
32
33c.execute("select pid, pid_idx, gid_idx from groups where pid=%s and pid = gid", (project,))
34gids = c.fetchall()
35
36if len(gids) == 1:
37    pid, pid_idx, gid_idx = gids[0]
38else:
39    sys.exit("Group is not unique")
40
41c.execute("select uid from group_membership where uid=%s and pid=%s", (user, project))
42
43uids = c.fetchall()
44if len(uids) != 0:
45    sys.exit("%s already in %s" % (user, project))
46
47c.execute("insert into group_membership (uid, uid_idx, gid, gid_idx, pid, " + \
48        "pid_idx, trust, date_applied, date_approved) " + \
49        "values (%s, %s, %s, %s, %s, %s, 'none', now(), now())",
50        (uid, uid_idx, pid, gid_idx, pid, pid_idx))
51
52print "inserted %d rows" % c.rowcount
Note: See TracBrowser for help on using the repository browser.