From 876ba7a23baf96d3672ce124dff925e86e081e3f Mon Sep 17 00:00:00 2001
From: Jeremy Stanley <fungi@yuggoth.org>
Date: Tue, 24 Jun 2014 19:07:49 +0000
Subject: [PATCH] ATC listing script is importable

* tools/atc/email_stats.py: Move procedural code out of the global
scope to make this file safely importable, and clean up a couple of
unused variables flake8 noticed in the newly importable main
codeblock.

Change-Id: I1a29c84e067eea875ba57a16196723f65f60ff01
---
 tools/atc/email_stats.py | 183 ++++++++++++++++++++-------------------
 1 file changed, 94 insertions(+), 89 deletions(-)
 mode change 100755 => 100644 tools/atc/email_stats.py

diff --git a/tools/atc/email_stats.py b/tools/atc/email_stats.py
old mode 100755
new mode 100644
index a8b805b723..1f863c638c
--- a/tools/atc/email_stats.py
+++ b/tools/atc/email_stats.py
@@ -28,7 +28,6 @@ import re
 
 MAILTO_RE = re.compile('mailto:(.*)')
 USERNAME_RE = re.compile('username:(.*)')
-accounts = {}
 
 
 class Account(object):
@@ -39,108 +38,114 @@ class Account(object):
         self.username = None
 
 
-def get_account(num):
+def get_account(accounts, num):
     a = accounts.get(num)
     if not a:
         a = Account(num)
         accounts[num] = a
     return a
 
-for row in csv.reader(open('emails.csv')):
-    num, email, pw, external = row
-    num = int(num)
-    a = get_account(num)
-    if email and email != '\\N' and email not in a.emails:
-        a.emails.append(email)
-    m = MAILTO_RE.match(external)
-    if m:
-        if m.group(1) not in a.emails:
-            a.emails.append(m.group(1))
-    m = USERNAME_RE.match(external)
-    if m:
-        if a.username:
-            print a.num
-            print a.username
-            raise Exception("Already a username")
-        a.username = m.group(1)
 
-for row in csv.reader(open('accounts.csv')):
-    num = int(row[-1])
-    name = row[1]
-    a = get_account(num)
-    a.full_name = name
+def main():
+    accounts = {}
 
-username_accounts = {}
-for a in accounts.values():
-    username_accounts[a.username] = a
+    for row in csv.reader(open('emails.csv')):
+        num, email, pw, external = row
+        num = int(num)
+        a = get_account(accounts, num)
+        if email and email != '\\N' and email not in a.emails:
+            a.emails.append(email)
+        m = MAILTO_RE.match(external)
+        if m:
+            if m.group(1) not in a.emails:
+                a.emails.append(m.group(1))
+        m = USERNAME_RE.match(external)
+        if m:
+            if a.username:
+                print a.num
+                print a.username
+                raise Exception("Already a username")
+            a.username = m.group(1)
 
-atcs = []
+    for row in csv.reader(open('accounts.csv')):
+        num = int(row[-1])
+        name = row[1]
+        a = get_account(accounts, num)
+        a.full_name = name
 
-optparser = optparse.OptionParser()
-optparser.add_option(
-    '-p', '--project', default='nova', help='Project to generate stats for')
-optparser.add_option(
-    '-o', '--output', default='out.csv', help='Output file')
-options, args = optparser.parse_args()
+    username_accounts = {}
+    for a in accounts.values():
+        username_accounts[a.username] = a
 
-QUERY = "project:%s status:merged" % options.project
+    atcs = []
 
-client = paramiko.SSHClient()
-client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
-client.load_system_host_keys()
-client.connect(
-    'review.openstack.org', port=29418,
-    key_filename='/home/corvus/.ssh/id_rsa', username='CHANGME')
-stdin, stdout, stderr = client.exec_command(
-    'gerrit query %s --all-approvals --format JSON' % QUERY)
-changes = []
+    optparser = optparse.OptionParser()
+    optparser.add_option(
+        '-p', '--project', default='nova',
+        help='Project to generate stats for')
+    optparser.add_option(
+        '-o', '--output', default='out.csv', help='Output file')
+    options, args = optparser.parse_args()
 
-done = False
-last_sortkey = ''
-tz = datetime.tzinfo
-start_date = datetime.datetime(2012, 9, 27, 0, 0, 0)
-end_date = datetime.datetime(2013, 7, 30, 0, 0, 0)
+    QUERY = "project:%s status:merged" % options.project
 
-count = 0
-earliest = datetime.datetime.now()
-while not done:
-    for l in stdout:
-        data = json.loads(l)
-        if 'rowCount' in data:
-            if data['rowCount'] < 500:
-                done = True
-            continue
-        count += 1
-        last_sortkey = data['sortKey']
-        if 'owner' not in data:
-            continue
-        if 'username' not in data['owner']:
-            continue
-        account = username_accounts[data['owner']['username']]
-        approved = False
-        for ps in data['patchSets']:
-            if 'approvals' not in ps:
+    client = paramiko.SSHClient()
+    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+    client.load_system_host_keys()
+    client.connect(
+        'review.openstack.org', port=29418,
+        key_filename='/home/corvus/.ssh/id_rsa', username='CHANGME')
+    stdin, stdout, stderr = client.exec_command(
+        'gerrit query %s --all-approvals --format JSON' % QUERY)
+
+    done = False
+    last_sortkey = ''
+    start_date = datetime.datetime(2012, 9, 27, 0, 0, 0)
+    end_date = datetime.datetime(2013, 7, 30, 0, 0, 0)
+
+    count = 0
+    earliest = datetime.datetime.now()
+    while not done:
+        for l in stdout:
+            data = json.loads(l)
+            if 'rowCount' in data:
+                if data['rowCount'] < 500:
+                    done = True
                 continue
-            for aprv in ps['approvals']:
-                if aprv['type'] != 'SUBM':
+            count += 1
+            last_sortkey = data['sortKey']
+            if 'owner' not in data:
+                continue
+            if 'username' not in data['owner']:
+                continue
+            account = username_accounts[data['owner']['username']]
+            approved = False
+            for ps in data['patchSets']:
+                if 'approvals' not in ps:
                     continue
-                ts = datetime.datetime.fromtimestamp(aprv['grantedOn'])
-                if ts < start_date or ts > end_date:
-                    continue
-                approved = True
-                if ts < earliest:
-                    earliest = ts
-        if approved and account not in atcs:
-            atcs.append(account)
-    if not done:
-        stdin, stdout, stderr = client.exec_command(
-            'gerrit query %s resume_sortkey:%s --all-approvals'
-            ' --format JSON' % (QUERY, last_sortkey))
+                for aprv in ps['approvals']:
+                    if aprv['type'] != 'SUBM':
+                        continue
+                    ts = datetime.datetime.fromtimestamp(aprv['grantedOn'])
+                    if ts < start_date or ts > end_date:
+                        continue
+                    approved = True
+                    if ts < earliest:
+                        earliest = ts
+            if approved and account not in atcs:
+                atcs.append(account)
+        if not done:
+            stdin, stdout, stderr = client.exec_command(
+                'gerrit query %s resume_sortkey:%s --all-approvals'
+                ' --format JSON' % (QUERY, last_sortkey))
 
-print 'project: %s' % options.project
-print 'examined %s changes' % count
-print 'earliest timestamp: %s' % earliest
-writer = csv.writer(open(options.output, 'w'))
-for a in atcs:
-    writer.writerow([a.username, a.full_name] + a.emails)
-print
+    print 'project: %s' % options.project
+    print 'examined %s changes' % count
+    print 'earliest timestamp: %s' % earliest
+    writer = csv.writer(open(options.output, 'w'))
+    for a in atcs:
+        writer.writerow([a.username, a.full_name] + a.emails)
+    print
+
+if __name__ == "__main__":
+    main()