• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 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"""Shared libs by run_suite.py & run_suite_skylab.py."""
6
7from __future__ import absolute_import
8from __future__ import division
9from __future__ import print_function
10
11import collections
12import json
13import sys
14
15from autotest_lib.client.common_lib import enum
16
17
18# Return code that will be sent back to callers.
19RETURN_CODES = enum.Enum(
20        'OK',
21        'ERROR',
22        'WARNING',
23        'INFRA_FAILURE',
24        'SUITE_TIMEOUT',
25        'BOARD_NOT_AVAILABLE',
26        'INVALID_OPTIONS',
27)
28
29
30class SuiteResult(collections.namedtuple('SuiteResult',
31                                         ['return_code', 'output_dict'])):
32    """Result of running a suite to return."""
33
34    def __new__(cls, return_code, output_dict=None):
35        if output_dict is None:
36            output_dict = dict()
37        else:
38            output_dict = output_dict.copy()
39        output_dict['return_code'] = return_code
40        return super(SuiteResult, cls).__new__(cls, return_code, output_dict)
41
42    @property
43    def string_code(self):
44        """Return the enum string name of the numerical return_code."""
45        return RETURN_CODES.get_string(self.return_code)
46
47
48def dump_json(obj):
49    """Write obj JSON to stdout."""
50    output_json = json.dumps(obj, sort_keys=True)
51    sys.stdout.write('#JSON_START#%s#JSON_END#' % output_json.strip())
52