In manage_projects find group uuids sanely.

manage_projects should use a direct connection to mysql to more safely
find group uuids. Add a gerritdb module to jeepyb to handle some of the
mysql connection boilerplate, and use that module to talk to mysql
directly.

Change-Id: If05a60cd7f5e8f6978226c6785792328388469f2
Reviewed-on: https://review.openstack.org/17516
Reviewed-by: Jeremy Stanley <fungi@yuggoth.org>
Approved: James E. Blair <corvus@inaugust.com>
Reviewed-by: James E. Blair <corvus@inaugust.com>
Tested-by: Jenkins
diff --git a/jeepyb/cmd/manage_projects.py b/jeepyb/cmd/manage_projects.py
index 0421dcb..73067af 100644
--- a/jeepyb/cmd/manage_projects.py
+++ b/jeepyb/cmd/manage_projects.py
@@ -50,6 +50,8 @@
 import github
 import gerritlib.gerrit
 
+import jeepyb.gerritdb
+
 logging.basicConfig(level=logging.ERROR)
 log = logging.getLogger("manage_projects")
 
@@ -136,12 +138,12 @@
 
 
 def _get_group_uuid(gerrit, group):
-    query = "select group_uuid from account_groups where name = '%s'" % group
-    data = gerrit.dbQuery(query)
+    cursor = jeepyb.gerritdb.connect().cursor()
+    query = "SELECT group_uuid FROM account_groups WHERE name = %s"
+    cursor.execute(query, group)
+    data = cursor.fetchone()
     if data:
-        for row in data:
-            if row["type"] == "row":
-                return row["columns"]["group_uuid"]
+        return data[0]
     return None
 
 
diff --git a/jeepyb/gerritdb.py b/jeepyb/gerritdb.py
new file mode 100644
index 0000000..a395565
--- /dev/null
+++ b/jeepyb/gerritdb.py
@@ -0,0 +1,55 @@
+#! /usr/bin/env python
+# Copyright (C) 2011 OpenStack, LLC.
+# Copyright (c) 2012 Hewlett-Packard Development Company, L.P.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import ConfigParser
+import MySQLdb
+import os
+import StringIO
+
+
+GERRIT_CONFIG = os.environ.get(
+    'GERRIT_CONFIG',
+    '/home/gerrit2/review_site/etc/gerrit.config')
+GERRIT_SECURE_CONFIG = os.environ.get(
+    'GERRIT_SECURE_CONFIG',
+    '/home/gerrit2/review_site/etc/secure.config')
+db_connection = None
+
+
+def get_broken_config(filename):
+    """ gerrit config ini files are broken and have leading tabs """
+    text = ""
+    for line in open(filename, "r"):
+        text += line.lstrip()
+
+    fp = StringIO.StringIO(text)
+    c = ConfigParser.ConfigParser()
+    c.readfp(fp)
+    return c
+
+
+def connect():
+    global db_connection
+    if not db_connection:
+        gerrit_config = get_broken_config(GERRIT_CONFIG)
+        secure_config = get_broken_config(GERRIT_SECURE_CONFIG)
+
+        DB_USER = gerrit_config.get("database", "username")
+        DB_PASS = secure_config.get("database", "password")
+        DB_DB = gerrit_config.get("database", "database")
+
+        db_connection = MySQLdb.connect(user=DB_USER, passwd=DB_PASS, db=DB_DB)
+    return db_connection