1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | import os, sys |
---|
4 | import MySQLdb |
---|
5 | from optparse import OptionParser |
---|
6 | |
---|
7 | if len(sys.argv) == 3: |
---|
8 | user, project = sys.argv[1:3] |
---|
9 | else: |
---|
10 | sys.exit("Usage %s user project" % sys.argv[0]) |
---|
11 | |
---|
12 | try: |
---|
13 | db = MySQLdb.connect(db='tbdb') |
---|
14 | except 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 | |
---|
22 | c = db.cursor() |
---|
23 | c.execute("select uid, uid_idx from users where uid=%s", (user,)) |
---|
24 | |
---|
25 | uids = c.fetchall() |
---|
26 | |
---|
27 | if len(uids) == 1: |
---|
28 | uid, uid_idx = uids[0] |
---|
29 | else: |
---|
30 | sys.exit("User is not unique??") |
---|
31 | |
---|
32 | |
---|
33 | c.execute("select pid, pid_idx, gid_idx from groups where pid=%s and pid = gid", (project,)) |
---|
34 | gids = c.fetchall() |
---|
35 | |
---|
36 | if len(gids) == 1: |
---|
37 | pid, pid_idx, gid_idx = gids[0] |
---|
38 | else: |
---|
39 | sys.exit("Group is not unique") |
---|
40 | |
---|
41 | c.execute("select uid from group_membership where uid=%s and pid=%s", (user, project)) |
---|
42 | |
---|
43 | uids = c.fetchall() |
---|
44 | if len(uids) != 0: |
---|
45 | sys.exit("%s already in %s" % (user, project)) |
---|
46 | |
---|
47 | c.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 | |
---|
52 | print "inserted %d rows" % c.rowcount |
---|