blob: ab8f496b753d4e3395cf22b5a81ac5963fbcafe0 [file] [log] [blame]
Dan Shia1ecd5c2013-06-06 11:21:31 -07001# 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 Shia1ecd5c2013-06-06 11:21:31 -07005
Alex Millerdadc2c22013-07-08 15:21:21 -07006import logging
7import re
Paul Drewsbef578d2013-09-24 15:10:36 -07008import urllib2
9import httplib
Alex Millerdadc2c22013-07-08 15:21:21 -070010
11from autotest_lib.client.common_lib import base_utils, global_config
Dan Shia1ecd5c2013-06-06 11:21:31 -070012from autotest_lib.server.cros.dynamic_suite import constants
13
14
Alex Millerdadc2c22013-07-08 15:21:21 -070015_SHERIFF_JS = global_config.global_config.get_config_value(
16 'NOTIFICATIONS', 'sheriffs', default='')
Fang Deng3197b392013-06-26 11:42:02 -070017_LAB_SHERIFF_JS = global_config.global_config.get_config_value(
18 'NOTIFICATIONS', 'lab_sheriffs', default='')
Alex Millerdadc2c22013-07-08 15:21:21 -070019_CHROMIUM_BUILD_URL = global_config.global_config.get_config_value(
20 'NOTIFICATIONS', 'chromium_build_url', default='')
21
22
Dan Shia1ecd5c2013-06-06 11:21:31 -070023def get_label_from_afe(hostname, label_prefix, afe):
24 """Retrieve a host's specific label from the AFE.
25
26 Looks for a host label that has the form <label_prefix>:<value>
27 and returns the "<value>" part of the label. None is returned
28 if there is not a label matching the pattern
29
30 @param hostname: hostname of given DUT.
31 @param label_prefix: prefix of label to be matched, e.g., |board:|
32 @param afe: afe instance.
33 @returns the label that matches the prefix or 'None'
34
35 """
36 labels = afe.get_labels(name__startswith=label_prefix,
37 host__hostname__in=[hostname])
38 if labels and len(labels) == 1:
39 return labels[0].name.split(label_prefix, 1)[1]
40
41
42def get_board_from_afe(hostname, afe):
43 """Retrieve given host's board from its labels in the AFE.
44
45 Looks for a host label of the form "board:<board>", and
46 returns the "<board>" part of the label. `None` is returned
47 if there is not a single, unique label matching the pattern.
48
49 @param hostname: hostname of given DUT.
50 @param afe: afe instance.
51 @returns board from label, or `None`.
52
53 """
54 return get_label_from_afe(hostname, constants.BOARD_PREFIX, afe)
55
56
57def get_build_from_afe(hostname, afe):
58 """Retrieve the current build for given host from the AFE.
59
60 Looks through the host's labels in the AFE to determine its build.
61
62 @param hostname: hostname of given DUT.
63 @param afe: afe instance.
64 @returns The current build or None if it could not find it or if there
65 were multiple build labels assigned to this host.
66
67 """
68 return get_label_from_afe(hostname, constants.VERSION_PREFIX, afe)
69
70
Fang Deng3197b392013-06-26 11:42:02 -070071def get_sheriffs(lab_only=False):
Alex Millerdadc2c22013-07-08 15:21:21 -070072 """
73 Polls the javascript file that holds the identity of the sheriff and
74 parses it's output to return a list of chromium sheriff email addresses.
75 The javascript file can contain the ldap of more than one sheriff, eg:
76 document.write('sheriff_one, sheriff_two').
77
Fang Deng3197b392013-06-26 11:42:02 -070078 @param lab_only: if True, only pulls lab sheriff.
79 @return: A list of chroium.org sheriff email addresses to cc on the bug.
80 An empty list if failed to parse the javascript.
Alex Millerdadc2c22013-07-08 15:21:21 -070081 """
82 sheriff_ids = []
Fang Deng3197b392013-06-26 11:42:02 -070083 sheriff_js_list = _LAB_SHERIFF_JS.split(',')
84 if not lab_only:
85 sheriff_js_list.extend(_SHERIFF_JS.split(','))
86
87 for sheriff_js in sheriff_js_list:
Alex Millerdadc2c22013-07-08 15:21:21 -070088 try:
89 url_content = base_utils.urlopen('%s%s'% (
90 _CHROMIUM_BUILD_URL, sheriff_js)).read()
91 except (ValueError, IOError) as e:
beeps4efdf032013-09-17 11:27:14 -070092 logging.warning('could not parse sheriff from url %s%s: %s',
93 _CHROMIUM_BUILD_URL, sheriff_js, str(e))
Paul Drewsbef578d2013-09-24 15:10:36 -070094 except (urllib2.URLError, httplib.HTTPException) as e:
95 logging.warning('unexpected error reading from url "%s%s": %s',
96 _CHROMIUM_BUILD_URL, sheriff_js, str(e))
Alex Millerdadc2c22013-07-08 15:21:21 -070097 else:
98 ldaps = re.search(r"document.write\('(.*)'\)", url_content)
99 if not ldaps:
beeps4efdf032013-09-17 11:27:14 -0700100 logging.warning('Could not retrieve sheriff ldaps for: %s',
101 url_content)
Alex Millerdadc2c22013-07-08 15:21:21 -0700102 continue
103 sheriff_ids += ['%s@chromium.org' % alias.replace(' ', '')
104 for alias in ldaps.group(1).split(',')]
105 return sheriff_ids