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
This commit is contained in:
Jeremy Stanley 2014-06-24 19:07:49 +00:00
parent 1c608c8143
commit 876ba7a23b

183
tools/atc/email_stats.py Executable file → Normal file
View File

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