Garry Wang | 111a26f | 2021-07-23 15:25:14 -0700 | [diff] [blame] | 1 | # Copyright 2021 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 | """Module for multi-DUTs support related helpers.""" |
| 6 | |
| 7 | from collections import namedtuple |
| 8 | |
| 9 | # BuildTarget contains information for a hwtest target. |
| 10 | BuildTarget = namedtuple('BuildTarget', ['board', 'model', 'cros_build']) |
| 11 | |
| 12 | |
| 13 | def convert_secondary_targets_to_string(secondary_targets): |
| 14 | """Convert A list of BuildTarget namedtuple to a string. |
| 15 | |
| 16 | Args: |
| 17 | secondary_targets: a list of BuildTarget namedtuple. Example: |
| 18 | [ |
| 19 | BuildTarget( |
| 20 | board="nami", |
| 21 | model=None, |
| 22 | cros_build="nami-release/R88-12000.00" |
| 23 | ), |
| 24 | BuildTarget( |
| 25 | board="coral", |
| 26 | model="babytiger", |
| 27 | cros_build="coral-release/R85-11000.00" |
| 28 | ) |
| 29 | ] |
| 30 | |
| 31 | Returns: |
| 32 | A string which BuildTargets are separated by ";" while attribute |
| 33 | of a BuildTarget are separated by ",". Example: |
| 34 | "nami,,nami-release/R88-12000.00;coral,babytiger,coral-release/R85-11000.00" |
| 35 | """ |
| 36 | l = [] |
| 37 | for target in secondary_targets: |
| 38 | board = target.board or '' |
| 39 | model = target.model or '' |
| 40 | cros_build = target.cros_build or '' |
| 41 | l.append('%s,%s,%s' % (board, model, cros_build)) |
| 42 | return ';'.join(l) |
| 43 | |
| 44 | |
| 45 | def restruct_secondary_targets_from_string(input_string): |
| 46 | """Restruct a stringified secondary targets list. |
| 47 | |
| 48 | Args: |
| 49 | input_string: a output from convert_secondary_targets_to_string function. |
| 50 | |
| 51 | Returns: |
| 52 | a list of BuildTarget namedtuple. |
| 53 | """ |
| 54 | secondary_targets = [] |
| 55 | if not input_string: |
| 56 | return secondary_targets |
| 57 | for section in input_string.split(';'): |
| 58 | board, model, cros_build = section.split(',') |
| 59 | board = board or None |
| 60 | model = model or None |
| 61 | cros_build = cros_build or None |
| 62 | secondary_targets.append( |
| 63 | BuildTarget(board=board, model=model, cros_build=cros_build)) |
| 64 | return secondary_targets |