1 # Copyright (c) 2012 The Chromium OS 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 re 6 7 import common 8 from constants import Labels 9 10 11 class EnumeratorException(Exception): 12 """Base class for exceptions from this module.""" 13 pass 14 15 16 class EnumerateException(EnumeratorException): 17 """Raised when an error is returned from the AFE during enumeration.""" 18 pass 19 20 21 class NoBoardException(EnumeratorException): 22 """Raised when an error is returned from the AFE during enumeration.""" 23 24 25 def __init__(self): 26 super(NoBoardException, self).__init__('No supported boards.') 27 28 29 class BoardEnumerator(object): 30 """Talks to the AFE and enumerates the boards it knows about. 31 32 @var _afe: a frontend.AFE instance used to talk to autotest. 33 """ 34 35 36 def __init__(self, afe=None): 37 """Constructor 38 39 @param afe: an instance of AFE as defined in server/frontend.py. 40 """ 41 self._afe = afe 42 43 44 def Enumerate(self): 45 """Enumerate currently supported boards. 46 47 Lists all labels known to the AFE that start with self._LABEL_PREFIX, 48 as this is the way that we define 'boards' in the AFE today. 49 50 @return list of board names, e.g. 'x86-mario' 51 """ 52 try: 53 labels = self._afe.get_labels(name__startswith=Labels.BOARD_PREFIX) 54 except Exception as e: 55 raise EnumerateException(e) 56 57 if not labels: 58 raise NoBoardException() 59 60 # Filter out all board labels tailing with -number, which is used for 61 # testbed only. 62 label_names = set([re.match(r'(.*?)(?:-\d+)?$', l.name).group(1) 63 for l in labels]) 64 return map(lambda l: l.split(':', 1)[1], label_names) 65