Chromium Code Reviews| Index: appengine/chromium_try_flakes/findit/findit.py |
| diff --git a/appengine/chromium_try_flakes/findit/findit.py b/appengine/chromium_try_flakes/findit/findit.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b98d5f5a07fb412055274b5d586c0d3a77718360 |
| --- /dev/null |
| +++ b/appengine/chromium_try_flakes/findit/findit.py |
| @@ -0,0 +1,89 @@ |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Provides API wrapper for FindIt""" |
| + |
| +import httplib2 |
| +import logging |
| +import time |
| + |
| +from apiclient import discovery |
| +from apiclient.errors import HttpError |
| +from oauth2client.appengine import AppAssertionCredentials |
| + |
| + |
| +# TODO(akuegel): Do we want to use a different timeout? Do we want to use a |
| +# cache? See documentation here: |
| +# https://github.com/jcgregorio/httplib2/blob/master/python2/httplib2/__init__.py#L1142 |
| +def _createHttpObject(scope): |
| + credentials = AppAssertionCredentials(scope=scope) |
| + return credentials.authorize(httplib2.Http()) |
| + |
| + |
| +def _buildClient(api_name, api_version, http, discovery_url): |
| + # This occassionally hits a 503 "Backend Error". Hopefully a simple retry |
| + # can recover. |
| + tries_left = 5 |
| + tries_wait = 10 |
| + while tries_left: |
| + tries_left -= 1 |
| + try: |
| + client = discovery.build( |
| + api_name, api_version, |
| + discoveryServiceUrl=discovery_url, |
| + http=http) |
| + break |
| + except HttpError as e: |
| + if tries_left: |
| + logging.error( |
| + 'apiclient.discovery.build() failed for %s: %s', api_name, e) |
| + logging.error( |
| + 'Retrying apiclient.discovery.build() in %s seconds.', tries_wait) |
| + time.sleep(tries_wait) |
| + else: |
| + logging.exception( |
| + 'apiclient.discovery.build() failed for %s too many times.', |
| + api_name) |
| + raise e |
| + return client |
| + |
| + |
| +class FindItAPI(object): |
| + """A wrapper around the FindIt api.""" |
| + def __init__(self): |
| + self.client = _buildClient( |
| + 'findit', 'v1', |
| + _createHttpObject('https://www.googleapis.com/auth/userinfo.email'), |
| + 'https://findit-for-me.appspot.com/_ah/api/discovery/v1/apis/{api}/' |
| + '{apiVersion}/rest') |
| + |
| + def _retry_api_call(self, request, num_retries=5): |
| + retries = 0 |
| + while True: |
| + try: |
| + return request.execute() |
| + except HttpError as e: |
| + # This retries internal server (500, 503) and quota (403) errors. |
| + if retries == num_retries or e.resp.status not in [403, 500, 503]: |
| + raise |
| + time.sleep(2**retries) |
| + retries += 1 |
| + |
| + def flake(self, flake, flaky_runs): |
| + body = {} |
| + body['name'] = flake.name |
| + body['is_step'] = flake.is_step |
| + body['bug_id'] = flake.issue_id |
| + body['build_steps'] = [] |
| + for flaky_run in flaky_runs: |
| + failure_run = flaky_run.failure_run.get() |
| + patchset_build_run = failure_run.parent.get() |
| + for occurrence in flaky_run.flakes: |
| + body['build_steps'].append({ |
| + 'master_name': patchset_build_run.master, |
| + 'builder_name': patchset_build_run.builder, |
| + 'build_number': failure_run.buildnumber, |
| + 'step_name': occurrence.name |
|
stgao
2016/10/03 19:24:35
To double check:
is it the original step name as s
Sergiy Byelozyorov
2016/10/04 08:58:22
This is original step name. The normalized step na
stgao
2016/10/05 00:28:47
Acknowledged.
|
| + }) |
| + self._retry_api_call(self.client.flake(body=body)) |