Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new arguments and reorder some of the code #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 71 additions & 70 deletions perryt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# -*- coding: utf-8 -*-

from argparse import ArgumentParser
import ConfigParser
from dateparser import parser as timedelta_parser
from datetime import datetime
import json
import subprocess
import ConfigParser


def query(gerritURL, query):
Expand Down Expand Up @@ -223,70 +223,62 @@ def __repr__(self):
self.reviewer)


def execute_search(search, format_output):
information = list(query('gerrit.ovirt.org', search))
def execute_search(search, format_output, url):
information = list(query(url, search))
queryInfo = information.pop()
changes = [Change(**change) for change in information]
changes = sorted(changes, key=lambda change: change.lastUpdated)
print u'Results: %s(time: %sµs)' % (queryInfo['rowCount'],
queryInfo['runTimeMilliseconds'])
print '=' * 80 + '\n'
print(u'Results: %s(time: %sµs)' % (queryInfo['rowCount'],
queryInfo['runTimeMilliseconds']))
print('=' * 80 + '\n')
format_output(changes)


def owner(owner, patchsets=None, status=None, since=None):
search = 'status:%s owner:%s' % (status or 'open', owner)
if since:
cut_date = datetime.now() - timedelta_parser.parse(since)

def format_output(changes):
for change in changes:
if patchsets == 'last':
patchSets = [change.patchSets[-1]]
else:
patchSets = change.patchSets
if cut_date:
patchSets = [patchSet for patchSet in patchSets if
cut_date <= patchSet.createdOn]
if patchSets:
print '%r' % change
print '\t%s' % change.url
for patchSet in patchSets:
print '\t%r' % patchSet
print '\n'

execute_search(search, format_output)


def reviewer(reviewer, patchsets=None, reviewed=None, verified=None,
status=None, since=None):
search = 'status:%s reviewer:%s' % (status or 'open', reviewer)
if since:
def owner_format_output(changes):
for change in changes:
if patchsets == 'last':
patchSets = [change.patchSets[-1]]
else:
patchSets = change.patchSets
if cut_date:
patchSets = [patchSet for patchSet in patchSets if
cut_date <= patchSet.createdOn]
if patchSets:
print '%r' % change
print '\t%s' % change.url
for patchSet in patchSets:
print '\t%r' % patchSet
print '\n'

def reviewer_format_output(changes):
for change in changes:
if patchsets == 'last':
patchSets = [change.patchSets[-1]]
else:
patchSets = change.patchSets
if cut_date:
patchSets = [patchSet for patchSet in patchSets if
cut_date <= patchSet.createdOn]
if reviewed is not None:
patchSets = [patchSet for patchSet in patchSets if
patchSet.reviewed(reviewed)]
if verified is not None:
patchSets = [patchSet for patchSet in patchSets if
patchSet.verified(verified)]
if patchSets:
print '%r' % change
print '\t%s' % change.url
for patchSet in patchSets:
print '\t%r' % patchSet
print '\n'


def run(action, name, url, args):
"""Execute chosen action."""
search = 'status:%s %s:%s' % (args.status or 'open', action, name)
if args.since:
cut_date = datetime.now() - timedelta_parser.parse(since)

def format_output(changes):
for change in changes:
if patchsets == 'last':
patchSets = [change.patchSets[-1]]
else:
patchSets = change.patchSets
if cut_date:
patchSets = [patchSet for patchSet in patchSets if
cut_date <= patchSet.createdOn]
if reviewed is not None:
patchSets = [patchSet for patchSet in patchSets if
patchSet.reviewed(reviewed)]
if verified is not None:
patchSets = [patchSet for patchSet in patchSets if
patchSet.verified(verified)]
if patchSets:
print '%r' % change
print '\t%s' % change.url
for patchSet in patchSets:
print '\t%r' % patchSet
print '\n'

execute_search(search, format_output)
execute_search(search, globals()[action + '_format_output'], url)


def check_server():
Expand All @@ -297,13 +289,19 @@ def check_server():
config.add_section('server')
server_url = raw_input('Enter the gerrit server url to save to '
'perryt.cfg: ')
config.set('server', 'url', server_url)
with open(CONF_FILE, 'w') as conf:
config.write(conf)
if server_url:
config.set('server', 'url', server_url)
with open(CONF_FILE, 'w') as conf:
config.write(conf)
return config

if __name__ == '__main__':

def create_parser():
"""Returns ArgumentParser instance."""
parser = ArgumentParser()
parser.add_argument('--url', help='The URL of the Gerrit server')
parser.add_argument('--key', help='SSH key to use for the connection')

subparsers = parser.add_subparsers(
title='actions', description='available gerrit actions.',
help='owner searches by owner and reviewer by reviewer.',
Expand All @@ -316,7 +314,8 @@ def check_server():
'patch sets for a change.')
owner_parser.add_argument(
'--status', choices=('open', 'reviewed', 'submitted', 'merged',
'closed', 'abandoned'), default='open', help='The state of the change')
'closed', 'abandoned'), default='open',
help='The state of the change')
owner_parser.add_argument('--since', help='Human max age of the patch '
'set, e.g. 1week,2days')

Expand All @@ -328,18 +327,20 @@ def check_server():
'patch sets for a change.')
rev_parser.add_argument(
'--status', choices=('open', 'reviewed', 'submitted', 'merged',
'closed', 'abandoned'), default='open', help='The state of the change')
'closed', 'abandoned'), default='open',
help='The state of the change')
rev_parser.add_argument('--reviewed', help='Filter results by reviews '
'done by id.')
rev_parser.add_argument('--verified', help='Filter results by '
'verifications done by id.')
rev_parser.add_argument('--since', help='Human max age of the patch '
'set, e.g. 1week,2days')

return parser

if __name__ == '__main__':

parser = create_parser()
args = parser.parse_args()
action = args.action
delattr(args, 'action')
if action == 'owner':
owner(**vars(args))
elif action == 'reviewer':
reviewer(**vars(args))

run(args.action, args.owner or args.reviewer, args.url, args)