blob: 8e9010f660e2c86dbc3955fa2ea4aa193479e87e [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='')
15_CHROMIUM_BUILD_URL = global_config.global_config.get_config_value(
16 'NOTIFICATIONS', 'chromium_build_url', default='')
17
18
Dan Shia1ecd5c2013-06-06 11:21:31 -070019def get_label_from_afe(hostname, label_prefix, afe):
20 """Retrieve a host's specific label from the AFE.
21
22 Looks for a host label that has the form <label_prefix>:<value>
23 and returns the "<value>" part of the label. None is returned
24 if there is not a label matching the pattern
25
26 @param hostname: hostname of given DUT.
27 @param label_prefix: prefix of label to be matched, e.g., |board:|
28 @param afe: afe instance.
29 @returns the label that matches the prefix or 'None'
30
31 """
32 labels = afe.get_labels(name__startswith=label_prefix,
33 host__hostname__in=[hostname])
34 if labels and len(labels) == 1:
35 return labels[0].name.split(label_prefix, 1)[1]
36
37
38def get_board_from_afe(hostname, afe):
39 """Retrieve given host's board from its labels in the AFE.
40
41 Looks for a host label of the form "board:<board>", and
42 returns the "<board>" part of the label. `None` is returned
43 if there is not a single, unique label matching the pattern.
44
45 @param hostname: hostname of given DUT.
46 @param afe: afe instance.
47 @returns board from label, or `None`.
48
49 """
50 return get_label_from_afe(hostname, constants.BOARD_PREFIX, afe)
51
52
53def get_build_from_afe(hostname, afe):
54 """Retrieve the current build for given host from the AFE.
55
56 Looks through the host's labels in the AFE to determine its build.
57
58 @param hostname: hostname of given DUT.
59 @param afe: afe instance.
60 @returns The current build or None if it could not find it or if there
61 were multiple build labels assigned to this host.
62
63 """
64 return get_label_from_afe(hostname, constants.VERSION_PREFIX, afe)
65
66
Alex Millerdadc2c22013-07-08 15:21:21 -070067def get_sheriffs():
68 """
69 Polls the javascript file that holds the identity of the sheriff and
70 parses it's output to return a list of chromium sheriff email addresses.
71 The javascript file can contain the ldap of more than one sheriff, eg:
72 document.write('sheriff_one, sheriff_two').
73
74 @return: A list of chroium.org sheriff email addresses to cc on the bug
75 if the suite that failed was the bvt suite. An empty list otherwise.
76 """
77 sheriff_ids = []
78 for sheriff_js in _SHERIFF_JS.split(','):
79 try:
80 url_content = base_utils.urlopen('%s%s'% (
81 _CHROMIUM_BUILD_URL, sheriff_js)).read()
82 except (ValueError, IOError) as e:
83 logging.error('could not parse sheriff from url %s%s: %s',
84 _CHROMIUM_BUILD_URL, sheriff_js, str(e))
85 else:
86 ldaps = re.search(r"document.write\('(.*)'\)", url_content)
87 if not ldaps:
88 logging.error('Could not retrieve sheriff ldaps for: %s',
89 url_content)
90 continue
91 sheriff_ids += ['%s@chromium.org' % alias.replace(' ', '')
92 for alias in ldaps.group(1).split(',')]
93 return sheriff_ids