1d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone# Use of this source code is governed by a BSD-style license that can be
3d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone# found in the LICENSE file.
4d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
5b493555db2d43e79d96e793cae9d1ffb822dd6c1Chris Masonefrom autotest_lib.server.cros.dynamic_suite import frontend_wrappers
6d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masonefrom autotest_lib.server import frontend
7d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
8511a9e3a1cfc2c6dd1263c8f6cb667ab12cd7a6fAlex Millerfrom constants import Labels
9511a9e3a1cfc2c6dd1263c8f6cb667ab12cd7a6fAlex Miller
10d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
11d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masoneclass EnumeratorException(Exception):
12d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone    """Base class for exceptions from this module."""
13d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone    pass
14d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
15d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
16d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masoneclass EnumerateException(EnumeratorException):
17d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone    """Raised when an error is returned from the AFE during enumeration."""
18d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone    pass
19d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
20d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
213fba86fc2184ddee87e7422370671abb18bd0792Chris Masoneclass NoBoardException(EnumeratorException):
22d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone    """Raised when an error is returned from the AFE during enumeration."""
23d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
24d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
25d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone    def __init__(self):
263fba86fc2184ddee87e7422370671abb18bd0792Chris Masone        super(NoBoardException, self).__init__('No supported boards.')
27d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
28d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
293fba86fc2184ddee87e7422370671abb18bd0792Chris Masoneclass BoardEnumerator(object):
303fba86fc2184ddee87e7422370671abb18bd0792Chris Masone    """Talks to the AFE and enumerates the boards it knows about.
31d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
32d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone    @var _afe: a frontend.AFE instance used to talk to autotest.
33d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone    """
34d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
35d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
36d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone    def __init__(self, afe=None):
37d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone        """Constructor
38d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
39d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone        @param afe: an instance of AFE as defined in server/frontend.py.
40d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone        """
41d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone        self._afe = afe
42d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
43d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
44d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone    def Enumerate(self):
453fba86fc2184ddee87e7422370671abb18bd0792Chris Masone        """Enumerate currently supported boards.
46d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
47d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone        Lists all labels known to the AFE that start with self._LABEL_PREFIX,
483fba86fc2184ddee87e7422370671abb18bd0792Chris Masone        as this is the way that we define 'boards' in the AFE today.
49d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
503fba86fc2184ddee87e7422370671abb18bd0792Chris Masone        @return list of board names, e.g. 'x86-mario'
51d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone        """
52d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone        try:
53511a9e3a1cfc2c6dd1263c8f6cb667ab12cd7a6fAlex Miller            labels = self._afe.get_labels(name__startswith=Labels.BOARD_PREFIX)
54d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone        except Exception as e:
55d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone            raise EnumerateException(e)
56d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
57d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone        if not labels:
583fba86fc2184ddee87e7422370671abb18bd0792Chris Masone            raise NoBoardException()
59d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone
60d525f9c00273dd7f2a69f1d9bbab269d19d9ba2dChris Masone        return map(lambda l: l.name.split(':', 1)[1], labels)
61