Clean up "logging".
Print is bad and I should feel bad. Use the logging module instead. Will follow up by adding a persistent log destination instead of just the console. Change-Id: I396ff10712f88a03f8d8183b6de29ea273815962
This commit is contained in:
parent
14af27a147
commit
a4061cddbe
3 changed files with 37 additions and 77 deletions
|
@ -12,7 +12,6 @@ Dependencies
|
|||
* [Google API Client Library](https://developers.google.com/api-client-library/python/start/installation)
|
||||
* [jenkinsapi](https://pypi.python.org/pypi/jenkinsapi)
|
||||
* [Requests](http://docs.python-requests.org/en/latest/)
|
||||
* [termcolor](https://pypi.python.org/pypi/termcolor)
|
||||
|
||||
Setup
|
||||
-----
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
# limitations under the License.
|
||||
#
|
||||
import json
|
||||
import logging
|
||||
import requests
|
||||
import termcolor
|
||||
|
||||
import gerrit
|
||||
|
||||
|
@ -43,7 +43,7 @@ def handle_build_message():
|
|||
ref = params['REF']
|
||||
patch_set = ref.split('/')[-1]
|
||||
|
||||
print '{} #{} {}: {}'.format(name, number, status, full_url)
|
||||
logging.debug('%s #%s %s: %s', name, number, status, full_url)
|
||||
|
||||
# bionic-lint is always broken, so we don't want to reject changes for
|
||||
# those failures until we clean things up.
|
||||
|
@ -69,19 +69,19 @@ def handle_build_message():
|
|||
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)
|
||||
logging.debug('POST %s: %s', url, request_data)
|
||||
requests.post(url, headers=headers, json=request_data)
|
||||
elif name == 'clean-bionic-presubmit':
|
||||
request_data = {'message': 'out/ directory removed'}
|
||||
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)
|
||||
logging.debug('POST %s: %s', url, request_data)
|
||||
requests.post(url, headers=headers, json=request_data)
|
||||
elif name == 'bionic-lint':
|
||||
print 'IGNORED'
|
||||
logging.warning('Result for bionic-lint ignored')
|
||||
else:
|
||||
print '{}: {}'.format(termcolor.colored('red', 'UNKNOWN'), name)
|
||||
logging.error('Unknown project: %s', name)
|
||||
return ''
|
||||
|
||||
|
||||
|
@ -100,17 +100,17 @@ def drop_rejection():
|
|||
bb_review = 0
|
||||
|
||||
if bb_review >= 0:
|
||||
print 'No rejection to drop: {} {}'.format(change_id, patch_set)
|
||||
logging.info('No rejection to drop: %s %s', change_id, patch_set)
|
||||
return ''
|
||||
|
||||
print 'Dropping rejection: {} {}'.format(change_id, patch_set)
|
||||
logging.info('Dropping rejection: %s %s', 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)
|
||||
logging.debug('POST %s: %s', url, request_data)
|
||||
requests.post(url, headers=headers, json=request_data)
|
||||
return ''
|
||||
|
||||
|
||||
|
|
|
@ -19,10 +19,10 @@ import httplib
|
|||
import httplib2
|
||||
import jenkinsapi
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import requests
|
||||
import termcolor
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
|
@ -141,7 +141,7 @@ def get_gerrit_info(body):
|
|||
return info
|
||||
|
||||
|
||||
def clean_project(gerrit_info, dry_run):
|
||||
def clean_project(dry_run):
|
||||
username = config.jenkins_credentials['username']
|
||||
password = config.jenkins_credentials['password']
|
||||
jenkins_url = config.jenkins_url
|
||||
|
@ -154,16 +154,9 @@ def clean_project(gerrit_info, dry_run):
|
|||
url = job.get_build().baseurl
|
||||
else:
|
||||
url = 'DRY_RUN_URL'
|
||||
print '{}({}): {} {}'.format(
|
||||
termcolor.colored('CLEAN', 'green'),
|
||||
gerrit_info['MessageType'],
|
||||
build,
|
||||
url)
|
||||
logging.info('Cleaning: %s %s', build, url)
|
||||
else:
|
||||
print '{}({}): {}'.format(
|
||||
termcolor.colored('CLEAN', 'red'),
|
||||
gerrit_info['MessageType'],
|
||||
termcolor.colored(build, 'red'))
|
||||
logging.error('Failed to clean: could not find project %s', build)
|
||||
return True
|
||||
|
||||
|
||||
|
@ -194,21 +187,9 @@ def build_project(gerrit_info, dry_run, lunch_target=None):
|
|||
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
|
||||
raise RuntimeError('Bad project mapping: {} => {}'.format(
|
||||
project, project_path))
|
||||
ref = gerrit.ref_for_change(change_id)
|
||||
params = {
|
||||
'REF': ref,
|
||||
'CHANGE_ID': change_id,
|
||||
|
@ -223,20 +204,10 @@ def build_project(gerrit_info, dry_run, lunch_target=None):
|
|||
url = 'URL UNAVAILABLE'
|
||||
else:
|
||||
url = 'DRY_RUN_URL'
|
||||
print '{}({}): {} => {} {} {}'.format(
|
||||
termcolor.colored('BUILD', 'green'),
|
||||
gerrit_info['MessageType'],
|
||||
project,
|
||||
build,
|
||||
url,
|
||||
change_id)
|
||||
logging.info('Building: %s => %s %s %s', project, build, url,
|
||||
change_id)
|
||||
else:
|
||||
print '{}({}): {} => {} {}'.format(
|
||||
termcolor.colored('BUILD', 'red'),
|
||||
gerrit_info['MessageType'],
|
||||
project,
|
||||
termcolor.colored(build, 'red'),
|
||||
change_id)
|
||||
logging.error('Unknown build: %s => %s %s', project, build, change_id)
|
||||
return True
|
||||
|
||||
|
||||
|
@ -259,13 +230,9 @@ def drop_rejection(gerrit_info, dry_run):
|
|||
try:
|
||||
requests.post(url, headers=headers, data=json.dumps(request_data))
|
||||
except requests.exceptions.ConnectionError as ex:
|
||||
print '{}(drop-rejection): {}'.format(
|
||||
termcolor.colored('ERROR', 'red'), ex)
|
||||
logging.error('Failed to drop rejection: %s', ex)
|
||||
return False
|
||||
print '{}({}): {}'.format(
|
||||
termcolor.colored('CHECK', 'green'),
|
||||
gerrit_info['MessageType'],
|
||||
gerrit_info['Change-Id'])
|
||||
logging.info('Dropped rejection: %s', gerrit_info['Change-Id'])
|
||||
return True
|
||||
|
||||
|
||||
|
@ -277,7 +244,7 @@ def handle_comment(gerrit_info, body, dry_run):
|
|||
return True
|
||||
|
||||
command_map = {
|
||||
'clean': lambda: clean_project(gerrit_info, dry_run),
|
||||
'clean': lambda: clean_project(dry_run),
|
||||
'retry': lambda: build_project(gerrit_info, dry_run),
|
||||
|
||||
'arm': lambda: build_project(gerrit_info, dry_run,
|
||||
|
@ -310,11 +277,11 @@ def handle_comment(gerrit_info, body, dry_run):
|
|||
|
||||
|
||||
def skip_handler(gerrit_info, _, __):
|
||||
print '{}({}): {}'.format(
|
||||
termcolor.colored('SKIP', 'yellow'),
|
||||
gerrit_info['MessageType'],
|
||||
gerrit_info['Change-Id'])
|
||||
logging.info('Skipping %s: %s', gerrit_info['MessageType'],
|
||||
gerrit_info['Change-Id'])
|
||||
return True
|
||||
|
||||
|
||||
handle_abandon = skip_handler
|
||||
handle_merge_failed = skip_handler
|
||||
handle_merged = skip_handler
|
||||
|
@ -327,28 +294,22 @@ def process_message(msg, dry_run):
|
|||
body = get_body(msg)
|
||||
gerrit_info = get_gerrit_info(body)
|
||||
if not gerrit_info:
|
||||
print termcolor.colored('No info found: {}'.format(msg['id']),
|
||||
'red')
|
||||
logging.fatal('No Gerrit info found: %s', msg.subject)
|
||||
msg_type = gerrit_info['MessageType']
|
||||
handler = 'handle_{}'.format(
|
||||
gerrit_info['MessageType'].replace('-', '_'))
|
||||
if handler in globals():
|
||||
return globals()[handler](gerrit_info, body, dry_run)
|
||||
else:
|
||||
print termcolor.colored(
|
||||
'MessageType {} unhandled.'.format(msg_type), 'red')
|
||||
print
|
||||
logging.warning('MessageType %s unhandled.', msg_type)
|
||||
return False
|
||||
except NotImplementedError as ex:
|
||||
print ex
|
||||
logging.error("%s", ex)
|
||||
return False
|
||||
except gerrit.GerritError as ex:
|
||||
if ex.code == 404:
|
||||
print '{}(404): {}!'.format(
|
||||
termcolor.colored('ERROR', 'red'), ex)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
change_id = gerrit_info['Change-Id']
|
||||
logging.error('Gerrit error (%d): %s %s', ex.code, change_id, ex.url)
|
||||
return ex.code == 404
|
||||
|
||||
|
||||
def main(argc, argv):
|
||||
|
@ -376,10 +337,10 @@ def main(argc, argv):
|
|||
msg_service.trash(userId='me', id=msg['id']).execute()
|
||||
time.sleep(60 * 5)
|
||||
except GmailError as ex:
|
||||
print '{}: {}!'.format(termcolor.colored('ERROR', 'red'), ex)
|
||||
logging.error('Gmail error: %s', ex)
|
||||
time.sleep(60 * 5)
|
||||
except apiclient.errors.HttpError as ex:
|
||||
print '{}: {}!'.format(termcolor.colored('ERROR', 'red'), ex)
|
||||
logging.error('API Client HTTP error: %s', ex)
|
||||
time.sleep(60 * 5)
|
||||
except httplib.BadStatusLine:
|
||||
pass
|
||||
|
|
Loading…
Reference in a new issue