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:
Dan Albert 2015-04-16 14:20:13 -07:00
parent 14af27a147
commit a4061cddbe
3 changed files with 37 additions and 77 deletions

View File

@ -12,7 +12,6 @@ Dependencies
* [Google API Client Library](https://developers.google.com/api-client-library/python/start/installation) * [Google API Client Library](https://developers.google.com/api-client-library/python/start/installation)
* [jenkinsapi](https://pypi.python.org/pypi/jenkinsapi) * [jenkinsapi](https://pypi.python.org/pypi/jenkinsapi)
* [Requests](http://docs.python-requests.org/en/latest/) * [Requests](http://docs.python-requests.org/en/latest/)
* [termcolor](https://pypi.python.org/pypi/termcolor)
Setup Setup
----- -----

View File

@ -15,8 +15,8 @@
# limitations under the License. # limitations under the License.
# #
import json import json
import logging
import requests import requests
import termcolor
import gerrit import gerrit
@ -43,7 +43,7 @@ def handle_build_message():
ref = params['REF'] ref = params['REF']
patch_set = ref.split('/')[-1] 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 # bionic-lint is always broken, so we don't want to reject changes for
# those failures until we clean things up. # those failures until we clean things up.
@ -69,19 +69,19 @@ def handle_build_message():
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) logging.debug('POST %s: %s', url, request_data)
print requests.post(url, headers=headers, json=request_data) 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) logging.debug('POST %s: %s', url, request_data)
print requests.post(url, headers=headers, json=request_data) requests.post(url, headers=headers, json=request_data)
elif name == 'bionic-lint': elif name == 'bionic-lint':
print 'IGNORED' logging.warning('Result for bionic-lint ignored')
else: else:
print '{}: {}'.format(termcolor.colored('red', 'UNKNOWN'), name) logging.error('Unknown project: %s', name)
return '' return ''
@ -100,17 +100,17 @@ def drop_rejection():
bb_review = 0 bb_review = 0
if 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 '' return ''
print 'Dropping rejection: {} {}'.format(change_id, patch_set) logging.info('Dropping rejection: %s %s', change_id, patch_set)
request_data = {'labels': {'Verified': 0}} request_data = {'labels': {'Verified': 0}}
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) logging.debug('POST %s: %s', url, request_data)
print requests.post(url, headers=headers, json=request_data) requests.post(url, headers=headers, json=request_data)
return '' return ''

View File

@ -19,10 +19,10 @@ import httplib
import httplib2 import httplib2
import jenkinsapi import jenkinsapi
import json import json
import logging
import os import os
import re import re
import requests import requests
import termcolor
import socket import socket
import sys import sys
import time import time
@ -141,7 +141,7 @@ def get_gerrit_info(body):
return info return info
def clean_project(gerrit_info, dry_run): def clean_project(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
@ -154,16 +154,9 @@ def clean_project(gerrit_info, dry_run):
url = job.get_build().baseurl url = job.get_build().baseurl
else: else:
url = 'DRY_RUN_URL' url = 'DRY_RUN_URL'
print '{}({}): {} {}'.format( logging.info('Cleaning: %s %s', build, url)
termcolor.colored('CLEAN', 'green'),
gerrit_info['MessageType'],
build,
url)
else: else:
print '{}({}): {}'.format( logging.error('Failed to clean: could not find project %s', build)
termcolor.colored('CLEAN', 'red'),
gerrit_info['MessageType'],
termcolor.colored(build, 'red'))
return True return True
@ -194,21 +187,9 @@ def build_project(gerrit_info, dry_run, lunch_target=None):
if not project_path: if not project_path:
raise RuntimeError('bogus project: {}'.format(project)) raise RuntimeError('bogus project: {}'.format(project))
if project_path.startswith('platform/'): if project_path.startswith('platform/'):
print '{}({}): {} => {}'.format( raise RuntimeError('Bad project mapping: {} => {}'.format(
termcolor.colored('ERROR', 'red'), project, project_path))
'project', ref = gerrit.ref_for_change(change_id)
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 = { params = {
'REF': ref, 'REF': ref,
'CHANGE_ID': change_id, 'CHANGE_ID': change_id,
@ -223,20 +204,10 @@ def build_project(gerrit_info, dry_run, lunch_target=None):
url = 'URL UNAVAILABLE' url = 'URL UNAVAILABLE'
else: else:
url = 'DRY_RUN_URL' url = 'DRY_RUN_URL'
print '{}({}): {} => {} {} {}'.format( logging.info('Building: %s => %s %s %s', project, build, url,
termcolor.colored('BUILD', 'green'), change_id)
gerrit_info['MessageType'],
project,
build,
url,
change_id)
else: else:
print '{}({}): {} => {} {}'.format( logging.error('Unknown build: %s => %s %s', project, build, change_id)
termcolor.colored('BUILD', 'red'),
gerrit_info['MessageType'],
project,
termcolor.colored(build, 'red'),
change_id)
return True return True
@ -259,13 +230,9 @@ def drop_rejection(gerrit_info, 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( logging.error('Failed to drop rejection: %s', ex)
termcolor.colored('ERROR', 'red'), ex)
return False return False
print '{}({}): {}'.format( logging.info('Dropped rejection: %s', gerrit_info['Change-Id'])
termcolor.colored('CHECK', 'green'),
gerrit_info['MessageType'],
gerrit_info['Change-Id'])
return True return True
@ -277,7 +244,7 @@ def handle_comment(gerrit_info, body, dry_run):
return True return True
command_map = { command_map = {
'clean': lambda: clean_project(gerrit_info, dry_run), 'clean': lambda: clean_project(dry_run),
'retry': lambda: build_project(gerrit_info, dry_run), 'retry': lambda: build_project(gerrit_info, dry_run),
'arm': 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, _, __): def skip_handler(gerrit_info, _, __):
print '{}({}): {}'.format( logging.info('Skipping %s: %s', gerrit_info['MessageType'],
termcolor.colored('SKIP', 'yellow'), gerrit_info['Change-Id'])
gerrit_info['MessageType'],
gerrit_info['Change-Id'])
return True return True
handle_abandon = skip_handler handle_abandon = skip_handler
handle_merge_failed = skip_handler handle_merge_failed = skip_handler
handle_merged = skip_handler handle_merged = skip_handler
@ -327,28 +294,22 @@ def process_message(msg, dry_run):
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']), logging.fatal('No Gerrit info found: %s', msg.subject)
'red')
msg_type = gerrit_info['MessageType'] msg_type = gerrit_info['MessageType']
handler = 'handle_{}'.format( handler = 'handle_{}'.format(
gerrit_info['MessageType'].replace('-', '_')) gerrit_info['MessageType'].replace('-', '_'))
if handler in globals(): if handler in globals():
return globals()[handler](gerrit_info, body, dry_run) return globals()[handler](gerrit_info, body, dry_run)
else: else:
print termcolor.colored( logging.warning('MessageType %s unhandled.', msg_type)
'MessageType {} unhandled.'.format(msg_type), 'red')
print
return False return False
except NotImplementedError as ex: except NotImplementedError as ex:
print ex logging.error("%s", ex)
return False return False
except gerrit.GerritError as ex: except gerrit.GerritError as ex:
if ex.code == 404: change_id = gerrit_info['Change-Id']
print '{}(404): {}!'.format( logging.error('Gerrit error (%d): %s %s', ex.code, change_id, ex.url)
termcolor.colored('ERROR', 'red'), ex) return ex.code == 404
return True
else:
return False
def main(argc, argv): def main(argc, argv):
@ -376,10 +337,10 @@ def main(argc, argv):
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) logging.error('Gmail error: %s', 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) logging.error('API Client HTTP error: %s', ex)
time.sleep(60 * 5) time.sleep(60 * 5)
except httplib.BadStatusLine: except httplib.BadStatusLine:
pass pass