Split some code out of reviewers.py.

Split some code out of reviewers.py that I'd like to use in another
script.
This commit is contained in:
Russell Bryant 2013-05-28 14:28:19 -04:00
parent 9934b7f08d
commit 26ea0a61fb
2 changed files with 94 additions and 58 deletions

View File

@ -18,21 +18,18 @@
import calendar
import cPickle as pickle
import datetime
import glob
import json
import optparse
import os
import os.path
import paramiko
from pprint import pprint
import prettytable
import sys
import time
import utils
CACHE_AGE = 3600 # Seconds
optparser = optparse.OptionParser()
optparser.add_option('-p', '--project', default='nova.json',
@ -46,65 +43,14 @@ optparser.add_option('-k', '--key', default=None, help='ssh key for gerrit')
options, args = optparser.parse_args()
if options.all:
files = glob.glob('./*.json')
else:
files = [options.project]
projects = []
for fn in files:
if os.path.isfile(fn):
with open(fn, 'r') as f:
project = json.loads(f.read())
projects.append(project)
projects = utils.get_projects_info(options.project, options.all)
if not projects:
print "Please specify a project."
sys.exit(1)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()
all_changes = []
for project in projects:
changes = []
pickle_fn = '%s-changes.pickle' % project['name']
if os.path.isfile(pickle_fn):
mtime = os.stat(pickle_fn).st_mtime
if (time.time() - mtime) <= CACHE_AGE:
with open(pickle_fn, 'r') as f:
changes = pickle.load(f)
def projects_q(project):
return ('(' +
' OR '.join(['project:' + p for p in project['subprojects']]) +
')')
if len(changes) == 0:
while True:
client.connect('review.openstack.org', port=29418,
key_filename=options.key, username=options.user)
cmd = ('gerrit query %s --all-approvals --patch-sets --format JSON' %
projects_q(project))
if len(changes) > 0:
cmd += ' resume_sortkey:%s' % changes[-2]['sortKey']
stdin, stdout, stderr = client.exec_command(cmd)
for l in stdout:
changes += [json.loads(l)]
if changes[-1]['rowCount'] == 0:
break
with open(pickle_fn, 'w') as f:
pickle.dump(changes, f)
all_changes.extend(changes)
all_changes = utils.get_changes(projects, options.user, options.key)
reviews = []
@ -150,7 +96,7 @@ else:
if options.all:
print '** -- Member of at least one core reviewer team'
else:
print '** -- %s-core team member' % project['name']
print '** -- %s-core team member' % projects[0]['name']
table = prettytable.PrettyTable(('Reviewer', 'Reviews (-2|-1|+1|+2) (+/- ratio)'))
total = 0
for k, v in reviewers:

90
utils.py Normal file
View File

@ -0,0 +1,90 @@
#
# Copyright (C) 2011 - Soren Hansen
# Copyright (C) 2013 - Red Hat, Inc.
#
# 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 cPickle as pickle
import glob
import json
import paramiko
import os
import time
CACHE_AGE = 3600 # Seconds
def get_projects_info(project=None, all_projects=False):
if all_projects:
files = glob.glob('./*.json')
else:
files = [project]
projects = []
for fn in files:
if os.path.isfile(fn):
with open(fn, 'r') as f:
project = json.loads(f.read())
projects.append(project)
return projects
def projects_q(project):
return ('(' +
' OR '.join(['project:' + p for p in project['subprojects']]) +
')')
def get_changes(projects, ssh_user, ssh_key):
all_changes = []
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()
for project in projects:
changes = []
pickle_fn = '%s-changes.pickle' % project['name']
if os.path.isfile(pickle_fn):
mtime = os.stat(pickle_fn).st_mtime
if (time.time() - mtime) <= CACHE_AGE:
with open(pickle_fn, 'r') as f:
changes = pickle.load(f)
if len(changes) == 0:
while True:
client.connect('review.openstack.org', port=29418,
key_filename=ssh_key, username=ssh_user)
cmd = ('gerrit query %s --all-approvals --patch-sets --format JSON' %
projects_q(project))
if len(changes) > 0:
cmd += ' resume_sortkey:%s' % changes[-2]['sortKey']
stdin, stdout, stderr = client.exec_command(cmd)
for l in stdout:
changes += [json.loads(l)]
if changes[-1]['rowCount'] == 0:
break
with open(pickle_fn, 'w') as f:
pickle.dump(changes, f)
all_changes.extend(changes)
return all_changes