blob: f3acdb784e5a3ab992ec443dc0c7541f248c9160 [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
8
9from autotest_lib.client.common_lib import base_utils, global_config
Dan Shia1ecd5c2013-06-06 11:21:31 -070010from autotest_lib.server.cros.dynamic_suite import constants
11
12
Alex Millerdadc2c22013-07-08 15:21:21 -070013_SHERIFF_JS = global_config.global_config.get_config_value(
14 'NOTIFICATIONS', 'sheriffs', default='')
Fang Deng3197b392013-06-26 11:42:02 -070015_LAB_SHERIFF_JS = global_config.global_config.get_config_value(
16 'NOTIFICATIONS', 'lab_sheriffs', default='')
Alex Millerdadc2c22013-07-08 15:21:21 -070017_CHROMIUM_BUILD_URL = global_config.global_config.get_config_value(
18 'NOTIFICATIONS', 'chromium_build_url', default='')
19
20
Dan Shia1ecd5c2013-06-06 11:21:31 -070021def 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
40def 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
55def 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 Deng3197b392013-06-26 11:42:02 -070069def get_sheriffs(lab_only=False):
Alex Millerdadc2c22013-07-08 15:21:21 -070070 """
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 Deng3197b392013-06-26 11:42:02 -070076 @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 Millerdadc2c22013-07-08 15:21:21 -070079 """
80 sheriff_ids = []
Fang Deng3197b392013-06-26 11:42:02 -070081 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 Millerdadc2c22013-07-08 15:21:21 -070086 try:
87 url_content = base_utils.urlopen('%s%s'% (
88 _CHROMIUM_BUILD_URL, sheriff_js)).read()
89 except (ValueError, IOError) as e:
beeps4efdf032013-09-17 11:27:14 -070090 logging.warning('could not parse sheriff from url %s%s: %s',
91 _CHROMIUM_BUILD_URL, sheriff_js, str(e))
Alex Millerdadc2c22013-07-08 15:21:21 -070092 else:
93 ldaps = re.search(r"document.write\('(.*)'\)", url_content)
94 if not ldaps:
beeps4efdf032013-09-17 11:27:14 -070095 logging.warning('Could not retrieve sheriff ldaps for: %s',
96 url_content)
Alex Millerdadc2c22013-07-08 15:21:21 -070097 continue
98 sheriff_ids += ['%s@chromium.org' % alias.replace(' ', '')
99 for alias in ldaps.group(1).split(',')]
100 return sheriff_ids