Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 5 | |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 6 | import logging |
| 7 | import re |
| 8 | |
| 9 | from autotest_lib.client.common_lib import base_utils, global_config |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 10 | from autotest_lib.server.cros.dynamic_suite import constants |
| 11 | |
| 12 | |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 13 | _SHERIFF_JS = global_config.global_config.get_config_value( |
| 14 | 'NOTIFICATIONS', 'sheriffs', default='') |
Fang Deng | 3197b39 | 2013-06-26 11:42:02 -0700 | [diff] [blame] | 15 | _LAB_SHERIFF_JS = global_config.global_config.get_config_value( |
| 16 | 'NOTIFICATIONS', 'lab_sheriffs', default='') |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 17 | _CHROMIUM_BUILD_URL = global_config.global_config.get_config_value( |
| 18 | 'NOTIFICATIONS', 'chromium_build_url', default='') |
| 19 | |
| 20 | |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 21 | def get_label_from_afe(hostname, label_prefix, afe): |
| 22 | """Retrieve a host's specific label from the AFE. |
| 23 | |
| 24 | Looks for a host label that has the form <label_prefix>:<value> |
| 25 | and returns the "<value>" part of the label. None is returned |
| 26 | if there is not a label matching the pattern |
| 27 | |
| 28 | @param hostname: hostname of given DUT. |
| 29 | @param label_prefix: prefix of label to be matched, e.g., |board:| |
| 30 | @param afe: afe instance. |
| 31 | @returns the label that matches the prefix or 'None' |
| 32 | |
| 33 | """ |
| 34 | labels = afe.get_labels(name__startswith=label_prefix, |
| 35 | host__hostname__in=[hostname]) |
| 36 | if labels and len(labels) == 1: |
| 37 | return labels[0].name.split(label_prefix, 1)[1] |
| 38 | |
| 39 | |
| 40 | def get_board_from_afe(hostname, afe): |
| 41 | """Retrieve given host's board from its labels in the AFE. |
| 42 | |
| 43 | Looks for a host label of the form "board:<board>", and |
| 44 | returns the "<board>" part of the label. `None` is returned |
| 45 | if there is not a single, unique label matching the pattern. |
| 46 | |
| 47 | @param hostname: hostname of given DUT. |
| 48 | @param afe: afe instance. |
| 49 | @returns board from label, or `None`. |
| 50 | |
| 51 | """ |
| 52 | return get_label_from_afe(hostname, constants.BOARD_PREFIX, afe) |
| 53 | |
| 54 | |
| 55 | def get_build_from_afe(hostname, afe): |
| 56 | """Retrieve the current build for given host from the AFE. |
| 57 | |
| 58 | Looks through the host's labels in the AFE to determine its build. |
| 59 | |
| 60 | @param hostname: hostname of given DUT. |
| 61 | @param afe: afe instance. |
| 62 | @returns The current build or None if it could not find it or if there |
| 63 | were multiple build labels assigned to this host. |
| 64 | |
| 65 | """ |
| 66 | return get_label_from_afe(hostname, constants.VERSION_PREFIX, afe) |
| 67 | |
| 68 | |
Fang Deng | 3197b39 | 2013-06-26 11:42:02 -0700 | [diff] [blame] | 69 | def get_sheriffs(lab_only=False): |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 70 | """ |
| 71 | Polls the javascript file that holds the identity of the sheriff and |
| 72 | parses it's output to return a list of chromium sheriff email addresses. |
| 73 | The javascript file can contain the ldap of more than one sheriff, eg: |
| 74 | document.write('sheriff_one, sheriff_two'). |
| 75 | |
Fang Deng | 3197b39 | 2013-06-26 11:42:02 -0700 | [diff] [blame] | 76 | @param lab_only: if True, only pulls lab sheriff. |
| 77 | @return: A list of chroium.org sheriff email addresses to cc on the bug. |
| 78 | An empty list if failed to parse the javascript. |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 79 | """ |
| 80 | sheriff_ids = [] |
Fang Deng | 3197b39 | 2013-06-26 11:42:02 -0700 | [diff] [blame] | 81 | sheriff_js_list = _LAB_SHERIFF_JS.split(',') |
| 82 | if not lab_only: |
| 83 | sheriff_js_list.extend(_SHERIFF_JS.split(',')) |
| 84 | |
| 85 | for sheriff_js in sheriff_js_list: |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 86 | try: |
| 87 | url_content = base_utils.urlopen('%s%s'% ( |
| 88 | _CHROMIUM_BUILD_URL, sheriff_js)).read() |
| 89 | except (ValueError, IOError) as e: |
beeps | 4efdf03 | 2013-09-17 11:27:14 -0700 | [diff] [blame^] | 90 | logging.warning('could not parse sheriff from url %s%s: %s', |
| 91 | _CHROMIUM_BUILD_URL, sheriff_js, str(e)) |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 92 | else: |
| 93 | ldaps = re.search(r"document.write\('(.*)'\)", url_content) |
| 94 | if not ldaps: |
beeps | 4efdf03 | 2013-09-17 11:27:14 -0700 | [diff] [blame^] | 95 | logging.warning('Could not retrieve sheriff ldaps for: %s', |
| 96 | url_content) |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 97 | continue |
| 98 | sheriff_ids += ['%s@chromium.org' % alias.replace(' ', '') |
| 99 | for alias in ldaps.group(1).split(',')] |
| 100 | return sheriff_ids |