Merge "Make indentation match the style guide."

This commit is contained in:
Dan Albert 2015-01-10 06:45:20 +00:00 committed by Gerrit Code Review
commit d7f935a05b
4 changed files with 375 additions and 335 deletions

View File

@ -1,6 +1,19 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# pylint: disable=bad-indentation #
# vim: set sw=2 ts=2: # Copyright (C) 2015 The Android Open Source Project
#
# 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 json import json
import requests import requests
import termcolor import termcolor
@ -12,93 +25,94 @@ app = Flask(__name__)
def gerrit_url(endpoint): def gerrit_url(endpoint):
gerrit_base_url = 'https://android-review.googlesource.com' gerrit_base_url = 'https://android-review.googlesource.com'
return gerrit_base_url + endpoint return gerrit_base_url + endpoint
@app.route('/', methods=['POST']) @app.route('/', methods=['POST'])
def handle_build_message(): def handle_build_message():
result = json.loads(request.data) result = json.loads(request.data)
name = result['name'] name = result['name']
number = result['build']['number'] number = result['build']['number']
status = result['build']['status'] status = result['build']['status']
go_url = 'http://go/bionicbb/' + result['build']['url'] go_url = 'http://go/bionicbb/' + result['build']['url']
full_url = result['build']['full_url'] full_url = result['build']['full_url']
params = result['build']['parameters'] params = result['build']['parameters']
change_id = params['CHANGE_ID'] change_id = params['CHANGE_ID']
ref = params['REF'] ref = params['REF']
patch_set = ref.split('/')[-1] patch_set = ref.split('/')[-1]
print '{} #{} {}: {}'.format(name, number, status, full_url) print '{} #{} {}: {}'.format(name, number, status, full_url)
# bionic-lint is always broken, so we don't want to reject changes for those # bionic-lint is always broken, so we don't want to reject changes for
# failures until we clean things up. # those failures until we clean things up.
if name == 'bionic-presubmit': if name == 'bionic-presubmit':
message_lines = ['{} #{} checkbuild {}: {}'.format( message_lines = ['{} #{} checkbuild {}: {}'.format(
name, number, status, go_url)] name, number, status, go_url)]
if status == 'FAILURE': if status == 'FAILURE':
message_lines += ['If you believe this Verified-1 was in error, +1 the ' message_lines += ['If you believe this Verified-1 was in error, '
'change and bionicbb will remove the -1 shortly.'] '+1 the change and bionicbb will remove the -1 '
'shortly.']
request_data = { request_data = {
'message': '\n'.join(message_lines) 'message': '\n'.join(message_lines)
} }
label = 'Verified' label = 'Verified'
if status == 'FAILURE': if status == 'FAILURE':
request_data['labels'] = {label: -1} request_data['labels'] = {label: -1}
elif status == 'SUCCESS': elif status == 'SUCCESS':
request_data['labels'] = {label: +1} request_data['labels'] = {label: +1}
url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id, url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
patch_set)) patch_set))
headers = {'Content-Type': 'application/json;charset=UTF-8'} headers = {'Content-Type': 'application/json;charset=UTF-8'}
print 'POST {}: {}'.format(url, request_data) print 'POST {}: {}'.format(url, request_data)
print requests.post(url, headers=headers, json=request_data) print requests.post(url, headers=headers, json=request_data)
elif name == 'clean-bionic-presubmit': elif name == 'clean-bionic-presubmit':
request_data = {'message': 'out/ directory removed'} request_data = {'message': 'out/ directory removed'}
url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id, url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
patch_set)) patch_set))
headers = {'Content-Type': 'application/json;charset=UTF-8'} headers = {'Content-Type': 'application/json;charset=UTF-8'}
print 'POST {}: {}'.format(url, request_data) print 'POST {}: {}'.format(url, request_data)
print requests.post(url, headers=headers, json=request_data) print requests.post(url, headers=headers, json=request_data)
elif name == 'bionic-lint': elif name == 'bionic-lint':
print 'IGNORED' print 'IGNORED'
else: else:
print '{}: {}'.format(termcolor.colored('red', 'UNKNOWN'), name) print '{}: {}'.format(termcolor.colored('red', 'UNKNOWN'), name)
return '' return ''
@app.route('/drop-rejection', methods=['POST']) @app.route('/drop-rejection', methods=['POST'])
def drop_rejection(): def drop_rejection():
revision_info = json.loads(request.data) revision_info = json.loads(request.data)
change_id = revision_info['changeid'] change_id = revision_info['changeid']
patch_set = revision_info['patchset'] patch_set = revision_info['patchset']
bb_email = 'bionicbb@android.com' bb_email = 'bionicbb@android.com'
labels = gerrit.get_labels(change_id, patch_set) labels = gerrit.get_labels(change_id, patch_set)
if bb_email in labels['Verified']: if bb_email in labels['Verified']:
bb_review = labels['Verified'][bb_email] bb_review = labels['Verified'][bb_email]
else: else:
bb_review = 0 bb_review = 0
if bb_review >= 0: if bb_review >= 0:
print 'No rejection to drop: {} {}'.format(change_id, patch_set) print 'No rejection to drop: {} {}'.format(change_id, patch_set)
return ''
print 'Dropping rejection: {} {}'.format(change_id, patch_set)
request_data = {'labels': {'Verified': 0}}
url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
patch_set))
headers = {'Content-Type': 'application/json;charset=UTF-8'}
print 'POST {}: {}'.format(url, request_data)
print requests.post(url, headers=headers, json=request_data)
return '' return ''
print 'Dropping rejection: {} {}'.format(change_id, patch_set)
request_data = {'labels': {'Verified': 0}}
url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
patch_set))
headers = {'Content-Type': 'application/json;charset=UTF-8'}
print 'POST {}: {}'.format(url, request_data)
print requests.post(url, headers=headers, json=request_data)
return ''
if __name__ == "__main__": if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True) app.run(host='0.0.0.0', debug=True)

View File

@ -1,56 +1,69 @@
# pylint: disable=bad-indentation #
# vim: set sw=2 ts=2: # Copyright (C) 2015 The Android Open Source Project
#
# 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 json import json
import requests import requests
class GerritError(RuntimeError): class GerritError(RuntimeError):
def __init__(self, code, url): def __init__(self, code, url):
self.code = code self.code = code
self.url = url self.url = url
super(GerritError, self).__init__('Error {}: {}'.format(code, url)) super(GerritError, self).__init__('Error {}: {}'.format(code, url))
def call(endpoint, method='GET'): def call(endpoint, method='GET'):
if method != 'GET': if method != 'GET':
raise NotImplementedError('Currently only HTTP GET is supported.') raise NotImplementedError('Currently only HTTP GET is supported.')
gerrit_url = 'https://android-review.googlesource.com' gerrit_url = 'https://android-review.googlesource.com'
url = gerrit_url + endpoint url = gerrit_url + endpoint
response = requests.get(url) response = requests.get(url)
if response.status_code != 200: if response.status_code != 200:
raise GerritError(response.status_code, url) raise GerritError(response.status_code, url)
return response.text[5:] return response.text[5:]
def ref_for_change(change_id): def ref_for_change(change_id):
endpoint = '/changes/{}/detail?o=CURRENT_REVISION'.format(change_id) endpoint = '/changes/{}/detail?o=CURRENT_REVISION'.format(change_id)
change = json.loads(call(endpoint)) change = json.loads(call(endpoint))
commit = change['current_revision'] commit = change['current_revision']
return change['revisions'][commit]['fetch']['http']['ref'] return change['revisions'][commit]['fetch']['http']['ref']
def get_labels(change_id, patch_set): def get_labels(change_id, patch_set):
"""Returns labels attached to a revision. """Returns labels attached to a revision.
Returned data is in the following format: Returned data is in the following format:
{ {
'Code-Review': { 'Code-Review': {
<email>: <value>, <email>: <value>,
... ...
}, },
'Verified': { 'Verified': {
<email>: <value>, <email>: <value>,
... ...
} }
} }
""" """
details = call('/changes/{}/revisions/{}/review'.format( details = call('/changes/{}/revisions/{}/review'.format(
change_id, patch_set)) change_id, patch_set))
labels = {'Code-Review': {}, 'Verified': {}} labels = {'Code-Review': {}, 'Verified': {}}
for review in details['labels']['Code-Review']['all']: for review in details['labels']['Code-Review']['all']:
if 'value' in review and 'email' in review: if 'value' in review and 'email' in review:
labels['Code-Review'][review['email']] = int(review['value']) labels['Code-Review'][review['email']] = int(review['value'])
for review in details['labels']['Verified']['all']: for review in details['labels']['Verified']['all']:
if 'value' in review and 'email' in review: if 'value' in review and 'email' in review:
labels['Verified'][review['email']] = int(review['value']) labels['Verified'][review['email']] = int(review['value'])
return labels return labels

View File

@ -1,6 +1,19 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# pylint: disable=bad-indentation #
# vim: set sw=2 ts=2: # Copyright (C) 2015 The Android Open Source Project
#
# 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 base64 import base64
import httplib import httplib
import httplib2 import httplib2
@ -20,228 +33,229 @@ import gerrit
class GmailError(RuntimeError): class GmailError(RuntimeError):
def __init__(self, message): def __init__(self, message):
super(GmailError, self).__init__(message) super(GmailError, self).__init__(message)
def get_gerrit_label(labels): def get_gerrit_label(labels):
for label in labels: for label in labels:
if label['name'] == 'gerrit': if label['name'] == 'gerrit':
return label['id'] return label['id']
return None return None
def get_headers(msg): def get_headers(msg):
headers = {} headers = {}
for hdr in msg['payload']['headers']: for hdr in msg['payload']['headers']:
headers[hdr['name']] = hdr['value'] headers[hdr['name']] = hdr['value']
return headers return headers
def build_service(): def build_service():
from apiclient.discovery import build from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage from oauth2client.file import Storage
from oauth2client.tools import run from oauth2client.tools import run
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.modify' OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.modify'
STORAGE = Storage('oauth.storage') STORAGE = Storage('oauth.storage')
# Start the OAuth flow to retrieve credentials # Start the OAuth flow to retrieve credentials
flow = flow_from_clientsecrets(config.client_secret_file, scope=OAUTH_SCOPE) flow = flow_from_clientsecrets(config.client_secret_file,
http = httplib2.Http() scope=OAUTH_SCOPE)
http = httplib2.Http()
# Try to retrieve credentials from storage or run the flow to generate them # Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get() credentials = STORAGE.get()
if credentials is None or credentials.invalid: if credentials is None or credentials.invalid:
credentials = run(flow, STORAGE, http=http) credentials = run(flow, STORAGE, http=http)
http = credentials.authorize(http) http = credentials.authorize(http)
return build('gmail', 'v1', http=http) return build('gmail', 'v1', http=http)
def get_all_messages(service, label): def get_all_messages(service, label):
msgs = [] msgs = []
response = service.users().messages().list(
userId='me', labelIds=label).execute()
if 'messages' in response:
msgs.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list( response = service.users().messages().list(
userId='me', pageToken=page_token).execute() userId='me', labelIds=label).execute()
msgs.extend(response['messages']) if 'messages' in response:
return msgs msgs.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(
userId='me', pageToken=page_token).execute()
msgs.extend(response['messages'])
return msgs
def get_body(msg): def get_body(msg):
if 'attachmentId' in msg['payload']['body']: if 'attachmentId' in msg['payload']['body']:
raise NotImplementedError('Handling of messages contained in ' raise NotImplementedError('Handling of messages contained in '
'attachments not yet implemented.') 'attachments not yet implemented.')
b64_body = msg['payload']['body']['data'] b64_body = msg['payload']['body']['data']
return base64.urlsafe_b64decode(b64_body.encode('ASCII')) return base64.urlsafe_b64decode(b64_body.encode('ASCII'))
def get_gerrit_info(body): def get_gerrit_info(body):
info = {} info = {}
gerrit_pattern = r'^Gerrit-(\S+): (.+)$' gerrit_pattern = r'^Gerrit-(\S+): (.+)$'
for match in re.finditer(gerrit_pattern, body, flags=re.MULTILINE): for match in re.finditer(gerrit_pattern, body, flags=re.MULTILINE):
info[match.group(1)] = match.group(2).strip() info[match.group(1)] = match.group(2).strip()
return info return info
def clean_project(gerrit_info, dry_run): def clean_project(gerrit_info, dry_run):
username = config.jenkins_credentials['username'] username = config.jenkins_credentials['username']
password = config.jenkins_credentials['password'] password = config.jenkins_credentials['password']
jenkins_url = config.jenkins_url jenkins_url = config.jenkins_url
jenkins = jenkinsapi.api.Jenkins(jenkins_url, username, password) jenkins = jenkinsapi.api.Jenkins(jenkins_url, username, password)
build = 'clean-bionic-presubmit' build = 'clean-bionic-presubmit'
if build in jenkins: if build in jenkins:
if not dry_run: if not dry_run:
job = jenkins[build].invoke() job = jenkins[build].invoke()
url = job.get_build().baseurl url = job.get_build().baseurl
else:
url = 'DRY_RUN_URL'
print '{}({}): {} {}'.format(
termcolor.colored('CLEAN', 'green'),
gerrit_info['MessageType'],
build,
url)
else: else:
url = 'DRY_RUN_URL' print '{}({}): {}'.format(
print '{}({}): {} {}'.format( termcolor.colored('CLEAN', 'red'),
termcolor.colored('CLEAN', 'green'), gerrit_info['MessageType'],
gerrit_info['MessageType'], termcolor.colored(build, 'red'))
build, return True
url)
else:
print '{}({}): {}'.format(
termcolor.colored('CLEAN', 'red'),
gerrit_info['MessageType'],
termcolor.colored(build, 'red'))
return True
def build_project(gerrit_info, dry_run): def build_project(gerrit_info, dry_run):
project_to_jenkins_map = { project_to_jenkins_map = {
'platform/bionic': 'bionic-presubmit', 'platform/bionic': 'bionic-presubmit',
'platform/build': 'bionic-presubmit', 'platform/build': 'bionic-presubmit',
'platform/external/jemalloc': 'bionic-presubmit', 'platform/external/jemalloc': 'bionic-presubmit',
'platform/external/libcxx': 'bionic-presubmit', 'platform/external/libcxx': 'bionic-presubmit',
'platform/external/libcxxabi': 'bionic-presubmit', 'platform/external/libcxxabi': 'bionic-presubmit',
'platform/external/compiler-rt': 'bionic-presubmit', 'platform/external/compiler-rt': 'bionic-presubmit',
}
username = config.jenkins_credentials['username']
password = config.jenkins_credentials['password']
jenkins_url = config.jenkins_url
jenkins = jenkinsapi.api.Jenkins(jenkins_url, username, password)
project = gerrit_info['Project']
change_id = gerrit_info['Change-Id']
if project in project_to_jenkins_map:
build = project_to_jenkins_map[project]
else:
build = 'bionic-presubmit'
if build in jenkins:
project_path = '/'.join(project.split('/')[1:])
if not project_path:
raise RuntimeError('bogus project: {}'.format(project))
if project_path.startswith('platform/'):
print '{}({}): {} => {}'.format(
termcolor.colored('ERROR', 'red'),
'project',
project,
project_path)
return False
try:
ref = gerrit.ref_for_change(change_id)
except gerrit.GerritError as ex:
print '{}({}): {} {}'.format(
termcolor.colored('GERRIT-ERROR', 'red'),
ex.code,
change_id,
ex.url)
return False
params = {
'REF': ref,
'CHANGE_ID': change_id,
'PROJECT': project_path
} }
if not dry_run:
job = jenkins[build].invoke(build_params=params) username = config.jenkins_credentials['username']
url = job.get_build().baseurl password = config.jenkins_credentials['password']
jenkins_url = config.jenkins_url
jenkins = jenkinsapi.api.Jenkins(jenkins_url, username, password)
project = gerrit_info['Project']
change_id = gerrit_info['Change-Id']
if project in project_to_jenkins_map:
build = project_to_jenkins_map[project]
else: else:
url = 'DRY_RUN_URL' build = 'bionic-presubmit'
print '{}({}): {} => {} {} {}'.format(
termcolor.colored('BUILD', 'green'), if build in jenkins:
gerrit_info['MessageType'], project_path = '/'.join(project.split('/')[1:])
project, if not project_path:
build, raise RuntimeError('bogus project: {}'.format(project))
url, if project_path.startswith('platform/'):
change_id) print '{}({}): {} => {}'.format(
else: termcolor.colored('ERROR', 'red'),
print '{}({}): {} => {} {}'.format( 'project',
termcolor.colored('BUILD', 'red'), project,
gerrit_info['MessageType'], project_path)
project, return False
termcolor.colored(build, 'red'), try:
change_id) ref = gerrit.ref_for_change(change_id)
return True except gerrit.GerritError as ex:
print '{}({}): {} {}'.format(
termcolor.colored('GERRIT-ERROR', 'red'),
ex.code,
change_id,
ex.url)
return False
params = {
'REF': ref,
'CHANGE_ID': change_id,
'PROJECT': project_path
}
if not dry_run:
job = jenkins[build].invoke(build_params=params)
url = job.get_build().baseurl
else:
url = 'DRY_RUN_URL'
print '{}({}): {} => {} {} {}'.format(
termcolor.colored('BUILD', 'green'),
gerrit_info['MessageType'],
project,
build,
url,
change_id)
else:
print '{}({}): {} => {} {}'.format(
termcolor.colored('BUILD', 'red'),
gerrit_info['MessageType'],
project,
termcolor.colored(build, 'red'),
change_id)
return True
def handle_change(gerrit_info, _, dry_run): def handle_change(gerrit_info, _, dry_run):
return build_project(gerrit_info, dry_run) return build_project(gerrit_info, dry_run)
handle_newchange = handle_change handle_newchange = handle_change
handle_newpatchset = handle_change handle_newpatchset = handle_change
def drop_rejection(gerrit_info, dry_run): def drop_rejection(gerrit_info, dry_run):
request_data = { request_data = {
'changeid': gerrit_info['Change-Id'], 'changeid': gerrit_info['Change-Id'],
'patchset': gerrit_info['PatchSet'] 'patchset': gerrit_info['PatchSet']
} }
url = '{}/{}'.format(config.build_listener_url, 'drop-rejection') url = '{}/{}'.format(config.build_listener_url, 'drop-rejection')
headers = {'Content-Type': 'application/json;charset=UTF-8'} headers = {'Content-Type': 'application/json;charset=UTF-8'}
if not dry_run: if not dry_run:
try: try:
requests.post(url, headers=headers, data=json.dumps(request_data)) requests.post(url, headers=headers, data=json.dumps(request_data))
except requests.exceptions.ConnectionError as ex: except requests.exceptions.ConnectionError as ex:
print '{}(drop-rejection): {}'.format( print '{}(drop-rejection): {}'.format(
termcolor.colored('ERROR', 'red'), ex) termcolor.colored('ERROR', 'red'), ex)
return False return False
print '{}({}): {}'.format( print '{}({}): {}'.format(
termcolor.colored('CHECK', 'green'), termcolor.colored('CHECK', 'green'),
gerrit_info['MessageType'], gerrit_info['MessageType'],
gerrit_info['Change-Id']) gerrit_info['Change-Id'])
return True return True
def handle_comment(gerrit_info, body, dry_run): def handle_comment(gerrit_info, body, dry_run):
if 'Verified+1' in body: if 'Verified+1' in body:
drop_rejection(gerrit_info, dry_run) drop_rejection(gerrit_info, dry_run)
command_map = { command_map = {
'clean': lambda: clean_project(gerrit_info, dry_run), 'clean': lambda: clean_project(gerrit_info, dry_run),
'retry': lambda: build_project(gerrit_info, dry_run), 'retry': lambda: build_project(gerrit_info, dry_run),
} }
def handle_unknown_command(): def handle_unknown_command():
pass # TODO(danalbert): should complain to the commenter. pass # TODO(danalbert): should complain to the commenter.
commands = [match.group(1).strip() for match in commands = [match.group(1).strip() for match in
re.finditer(r'^bionicbb:\s*(.+)$', body, flags=re.MULTILINE)] re.finditer(r'^bionicbb:\s*(.+)$', body, flags=re.MULTILINE)]
for command in commands: for command in commands:
if command in command_map: if command in command_map:
command_map[command]() command_map[command]()
else: else:
handle_unknown_command() handle_unknown_command()
return True return True
def skip_handler(gerrit_info, _, __): def skip_handler(gerrit_info, _, __):
print '{}({}): {}'.format( print '{}({}): {}'.format(
termcolor.colored('SKIP', 'yellow'), termcolor.colored('SKIP', 'yellow'),
gerrit_info['MessageType'], gerrit_info['MessageType'],
gerrit_info['Change-Id']) gerrit_info['Change-Id'])
return True return True
handle_abandon = skip_handler handle_abandon = skip_handler
handle_merged = skip_handler handle_merged = skip_handler
handle_restore = skip_handler handle_restore = skip_handler
@ -249,62 +263,63 @@ handle_revert = skip_handler
def process_message(msg, dry_run): def process_message(msg, dry_run):
try: try:
body = get_body(msg) body = get_body(msg)
gerrit_info = get_gerrit_info(body) gerrit_info = get_gerrit_info(body)
if not gerrit_info: if not gerrit_info:
print termcolor.colored('No info found: {}'.format(msg['id']), 'red') print termcolor.colored('No info found: {}'.format(msg['id']),
msg_type = gerrit_info['MessageType'] 'red')
handler = 'handle_{}'.format(gerrit_info['MessageType']) msg_type = gerrit_info['MessageType']
if handler in globals(): handler = 'handle_{}'.format(gerrit_info['MessageType'])
return globals()[handler](gerrit_info, body, dry_run) if handler in globals():
else: return globals()[handler](gerrit_info, body, dry_run)
print termcolor.colored( else:
'MessageType {} unhandled.'.format(msg_type), 'red') print termcolor.colored(
print 'MessageType {} unhandled.'.format(msg_type), 'red')
return False print
except NotImplementedError as ex: return False
print ex except NotImplementedError as ex:
return False print ex
return False
def main(argc, argv): def main(argc, argv):
dry_run = False dry_run = False
if argc == 2 and argv[1] == '--dry-run': if argc == 2 and argv[1] == '--dry-run':
dry_run = True dry_run = True
elif argc > 2: elif argc > 2:
sys.exit('usage: python {} [--dry-run]'.format(argv[0])) sys.exit('usage: python {} [--dry-run]'.format(argv[0]))
gmail_service = build_service() gmail_service = build_service()
msg_service = gmail_service.users().messages() msg_service = gmail_service.users().messages()
while True: while True:
try: try:
labels = gmail_service.users().labels().list(userId='me').execute() labels = gmail_service.users().labels().list(userId='me').execute()
if not labels['labels']: if not labels['labels']:
raise GmailError('Could not retrieve Gmail labels') raise GmailError('Could not retrieve Gmail labels')
label_id = get_gerrit_label(labels['labels']) label_id = get_gerrit_label(labels['labels'])
if not label_id: if not label_id:
raise GmailError('Could not find gerrit label') raise GmailError('Could not find gerrit label')
for msg in get_all_messages(gmail_service, label_id): for msg in get_all_messages(gmail_service, label_id):
msg = msg_service.get(userId='me', id=msg['id']).execute() msg = msg_service.get(userId='me', id=msg['id']).execute()
if process_message(msg, dry_run) and not dry_run: if process_message(msg, dry_run) and not dry_run:
msg_service.trash(userId='me', id=msg['id']).execute() msg_service.trash(userId='me', id=msg['id']).execute()
time.sleep(60 * 5) time.sleep(60 * 5)
except GmailError as ex: except GmailError as ex:
print '{}: {}!'.format(termcolor.colored('ERROR', 'red'), ex) print '{}: {}!'.format(termcolor.colored('ERROR', 'red'), ex)
time.sleep(60 * 5) time.sleep(60 * 5)
except apiclient.errors.HttpError as ex: except apiclient.errors.HttpError as ex:
print '{}: {}!'.format(termcolor.colored('ERROR', 'red'), ex) print '{}: {}!'.format(termcolor.colored('ERROR', 'red'), ex)
time.sleep(60 * 5) time.sleep(60 * 5)
except httplib.BadStatusLine: except httplib.BadStatusLine:
pass pass
except httplib2.ServerNotFoundError: except httplib2.ServerNotFoundError:
pass pass
except socket.error: except socket.error:
pass pass
if __name__ == '__main__': if __name__ == '__main__':
main(len(sys.argv), sys.argv) main(len(sys.argv), sys.argv)

View File

@ -1,2 +0,0 @@
[pep8]
ignore = E111