fedora-accounts export-bugzilla.py,1.9,1.10
Toshio Ernie Kuratomi (toshio)
fedora-extras-commits at redhat.com
Thu Aug 23 23:32:27 UTC 2007
Author: toshio
Update of /cvs/fedora/fedora-accounts
In directory cvs-int.fedora.redhat.com:/tmp/cvs-serv24006
Modified Files:
export-bugzilla.py
Log Message:
Update to read from the bugzilla queue in the FAS db and add it to bugzilla
via an xmlrpc call.
Index: export-bugzilla.py
===================================================================
RCS file: /cvs/fedora/fedora-accounts/export-bugzilla.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- export-bugzilla.py 9 Aug 2007 03:31:11 -0000 1.9
+++ export-bugzilla.py 23 Aug 2007 23:32:25 -0000 1.10
@@ -1,123 +1,71 @@
-#!/usr/bin/python2
+#!/usr/bin/python -t
-import sys, os, errno
-import website, crypt
-import getopt, re
-
-GRANT_DIRECT = 0
-GRANT_DERIVED = 1
-GRANT_REGEXP = 2
-
-def derive_groups(dbh, user_id, user_email):
- assert user_id
-
- dbc = dbh.cursor()
- dbc2 = dbh.cursor()
- dbh.commit()
- dbc.execute("LOCK TABLE profiles, user_group_map IN ROW EXCLUSIVE MODE")
- dbc.execute("LOCK TABLE group_group_map, groups IN ROW SHARE MODE")
- dbc.execute("SELECT NOW()")
- now_time = dbc.fetchone()[0]
- dbc.execute("DELETE FROM user_group_map WHERE user_id = %s AND grant_type != %s",
- (user_id, GRANT_DIRECT))
- groupidsadded = {}
- dbc.execute("SELECT id, userregexp FROM groups WHERE userregexp != ''")
- while 1:
- arow = dbc.fetchone()
- if not arow: break
-
- if re.search(arow[1], user_email):
- dbc2.execute("INSERT INTO user_group_map (user_id, group_id, isbless, grant_type) VALUES (%s, %s, 0, %s)",
- (user_id, arow[0], GRANT_REGEXP))
- groupidsadded[arow[0]] = 1
- groupidschecked = {}
- dbc.execute("SELECT group_id FROM user_group_map WHERE user_id = %s", (user_id, ))
- groupidstocheck = []
- while 1:
- arow = dbc.fetchone()
- if not arow: break
- groupidstocheck.append(arow[0])
-
- while len(groupidstocheck):
- for I in groupidstocheck: groupidschecked[I] = 1
- dbc2.execute("SELECT grantor_id FROM group_group_map WHERE member_id IN %s AND isbless = 0",
- (groupidstocheck, ))
- groupidstocheck = []
- while 1:
- arow = dbc2.fetchone()
- if not arow: break
- if not groupidschecked.has_key(arow[0]):
- groupidstocheck.append(arow[0])
- if not groupidsadded.has_key(arow[0]):
- groupidsadded[arow[0]] = 1
- dbc.execute("INSERT INTO user_group_map (user_id, group_id, isbless, grant_type) VALUES (%s, %s, 0, %s)",
- (user_id, arow[0], GRANT_DERIVED))
- dbc.execute("UPDATE profiles SET refreshed_when = %s WHERE userid = %s",
- (now_time, user_id))
- dbh.commit()
-
-opts, args = getopt.getopt(sys.argv[1:], '', ('usage', 'help'))
-if len(args) != 2 or ('--usage','') in opts or ('--help','') in opts:
- print """
-Usage: export-bugzilla.py GROUP BUGZILLA_GROUP
-"""
- sys.exit(1)
-our_group = args[0]
-bz_group = args[1]
-
-bzdbh = website.get_dbh('bugs', 'bugs')
-bzdbc = bzdbh.cursor()
-
-bzdbc.execute("SELECT id FROM groups WHERE name = %s", (bz_group,))
-arow = bzdbc.fetchone()
-assert arow
-bz_group_id = arow[0]
-
-dbh = website.get_dbh(dbctx='live')
-dbc = dbh.cursor()
-
-qry = """
-SELECT project_group.id, person.username, person.email, person.password, project_group.prerequisite_id,
-person.human_name
-FROM role, person, project_group WHERE person.id = role.person_id AND project_group.id = role.project_group_id
-AND person.approval_status = 'approved' AND role.role_status = 'approved' AND project_group.name = %s
-"""
-
-dbc.execute(qry, (our_group, ))
-
-bzdbh.commit()
-checkme = {}
-bzdbc.execute("SELECT user_group_map.user_id, profiles.login_name FROM user_group_map, profiles WHERE user_group_map.user_id = profiles.userid AND group_id = %s AND isbless = 0 AND isderived = 0 AND grant_type = %s",
- (bz_group_id, GRANT_DIRECT))
-while 1:
- arow = bzdbc.fetchone()
- if not arow: break
- checkme[arow[0]] = arow[1]
-bzdbc.execute("DELETE FROM user_group_map WHERE group_id = %s AND isbless = 0 AND isderived = 0 AND grant_type = %s",
- (bz_group_id, GRANT_DIRECT))
-while 1:
- arow = dbc.fetchone()
- if not arow: break
- if arow[4] and not website.have_group(dbh, arow[1], arow[4]):
- continue
-
- user_email = arow[2].lower()
- bzdbc.execute("SELECT userid FROM profiles WHERE login_name = %s", (user_email,))
- if not bzdbc.rowcount:
- print "Creating BZ account for", user_email
- bzdbc.execute("INSERT INTO profiles (userid, login_name, cryptpassword, realname, mybugslink, refreshed_when) VALUES (nextval('profiles_userid_seq'), %s, %s, %s, 1, NOW())", (user_email, crypt.crypt(arow[3], 'aq'), arow[5]))
- bzdbc.execute("SELECT userid FROM profiles WHERE login_name = %s", (user_email,))
- assert bzdbc.rowcount
- bz_user_id = bzdbc.fetchone()[0]
-
- bzdbc.execute("SELECT user_id FROM user_group_map WHERE user_id = %s AND group_id = %s", (bz_user_id, bz_group_id))
- if not bzdbc.rowcount:
-# print "Adding membership for", user_email, "in ", bz_group
- bzdbc.execute("INSERT INTO user_group_map (user_id, group_id, isbless, isderived, grant_type) VALUES (%s, %s, 0, 0, %s)",
- (bz_user_id, bz_group_id, GRANT_DIRECT))
- checkme[bz_user_id] = user_email
-bzdbh.commit()
-
-for I in checkme.keys():
- derive_groups(bzdbh, I, checkme[I])
-bzdbh.commit()
+import sys
+import website
+import getopt
+import xmlrpclib
+
+BZSERVER = 'https://bugdev.devel.redhat.com/bugzilla-cvs/xmlrpc.cgi'
+#BZSERVER = 'https://bugzilla.redhat.com/bugzilla/xmlrpc.cgi'
+BZUSER=''
+BZPASS=''
+
+if __name__ == '__main__':
+ opts, args = getopt.getopt(sys.argv[1:], '', ('usage', 'help'))
+ if len(args) != 2 or ('--usage','') in opts or ('--help','') in opts:
+ print """
+ Usage: export-bugzilla.py GROUP BUGZILLA_GROUP
+ """
+ sys.exit(1)
+ our_group = args[0]
+ bz_group = args[1]
+
+ # Set up the connection to our servers
+ dbh = website.get_dbh(dbctx='live')
+ dbc = dbh.cursor()
+ server = xmlrpclib.Server(bzServer)
+
+ # Select all queued bugzilla changes for this group
+ qry = "SELECT p.email, p.human_name, b.action, p.id, g.id" \
+ " from bugzilla_queue as b, person as p, project_group as g" \
+ " where b.person_id = p.id and b.project_group_id = g.id" \
+ " and g.name = %s"
+ dbc.execute(qry, (our_group, ))
+
+ for record in dbc.fetchall():
+ # Make sure we have a record for this user in bugzilla
+ if record[2] == 'r':
+ # Remove the user's bugzilla group
+ try:
+ server.updatePerms(record[0], 'remove', (bz_group,),
+ BZUSER, BZPASS)
+ except xmlrpclib.Fault, e:
+ if e.faultString.startswith('No such user'):
+ # It's okay, not having this user is equivalent to setting
+ # them to not have this group.
+ pass
+ else:
+ raise
+
+ elif record[2] == 'a':
+ # Try to create the user
+ try:
+ server.addUser(record[0], record[1], BZUSER, BZPASS)
+ except xmlrpclib.Fault, e:
+ ### FIXME: We want to make sure the error is for user already exists
+ if e.faultString.startswith('User exists'):
+ # It's okay, we just need to make sure the user has an
+ # account.
+ pass
+ else:
+ raise
+ server.updatePerms(record[0], 'add', (bz_group,), BZuSER, BZPASS)
+ else:
+ print 'Unrecognized action code: %s %s %s %s %s' % (record[2],
+ record[0], record[1], record[3], record[4])
+
+ # Remove them from the queue
+ dbc.execute("delete from bugzilla_queue as b"
+ " where b.person_id = %s and b.project_group_id = %s",
+ record[3], record[4])
+ dbh.commit()
More information about the fedora-extras-commits
mailing list