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 | |
| 5 | import logging |
| 6 | |
| 7 | from autotest_lib.server.cros.dynamic_suite import constants |
| 8 | |
| 9 | |
| 10 | def get_label_from_afe(hostname, label_prefix, afe): |
| 11 | """Retrieve a host's specific label from the AFE. |
| 12 | |
| 13 | Looks for a host label that has the form <label_prefix>:<value> |
| 14 | and returns the "<value>" part of the label. None is returned |
| 15 | if there is not a label matching the pattern |
| 16 | |
| 17 | @param hostname: hostname of given DUT. |
| 18 | @param label_prefix: prefix of label to be matched, e.g., |board:| |
| 19 | @param afe: afe instance. |
| 20 | @returns the label that matches the prefix or 'None' |
| 21 | |
| 22 | """ |
| 23 | labels = afe.get_labels(name__startswith=label_prefix, |
| 24 | host__hostname__in=[hostname]) |
| 25 | if labels and len(labels) == 1: |
| 26 | return labels[0].name.split(label_prefix, 1)[1] |
| 27 | |
| 28 | |
| 29 | def get_board_from_afe(hostname, afe): |
| 30 | """Retrieve given host's board from its labels in the AFE. |
| 31 | |
| 32 | Looks for a host label of the form "board:<board>", and |
| 33 | returns the "<board>" part of the label. `None` is returned |
| 34 | if there is not a single, unique label matching the pattern. |
| 35 | |
| 36 | @param hostname: hostname of given DUT. |
| 37 | @param afe: afe instance. |
| 38 | @returns board from label, or `None`. |
| 39 | |
| 40 | """ |
| 41 | return get_label_from_afe(hostname, constants.BOARD_PREFIX, afe) |
| 42 | |
| 43 | |
| 44 | def get_build_from_afe(hostname, afe): |
| 45 | """Retrieve the current build for given host from the AFE. |
| 46 | |
| 47 | Looks through the host's labels in the AFE to determine its build. |
| 48 | |
| 49 | @param hostname: hostname of given DUT. |
| 50 | @param afe: afe instance. |
| 51 | @returns The current build or None if it could not find it or if there |
| 52 | were multiple build labels assigned to this host. |
| 53 | |
| 54 | """ |
| 55 | return get_label_from_afe(hostname, constants.VERSION_PREFIX, afe) |
| 56 | |
| 57 | |