1# Copyright 2018 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import vts.utils.python.common.cmd_utils as cmd_utils
16
17
18class CmdResult(object):
19    """Shell command result object.
20
21    Attributes:
22        stdout: string, command stdout output.
23                If multiple command results are included in the object,
24                only the last one is returned.
25        stdouts: list of string, a list of command stdout outputs.
26        stderr: string, command stderr output.
27                If multiple command results are included in the object,
28                only the last one is returned.
29        stderrs: list of string, a list of command stderr outputs
30        returncode: int, command returncode output.
31                    If multiple command results are included in the object,
32                    only the last one is returned.
33        returncodes: list of int, a list of command returncode outputs.
34        cmd: string, original command that generates the result.
35             If multiple commands are included in the object,
36             only the last one is returned.
37             original command may not always be included (is None).
38        cmds: a list of string, original commands that generate the results.
39        exception: Exception or string, the exception that prevented the command
40                   from generating result.
41                   If multiple commands are included in the object,
42                   only the last one is returned.
43        execptions: a list of Exception or string, the exceptions that
44                    prevented the command from generating result.
45    """
46
47    def __init__(self, stdout, stderr, returncode, cmd=None, exception=None):
48        self.stdouts = []
49        self.stderrs = []
50        self.returncodes = []
51        self.cmds = []
52        self.exceptions = []
53        self.AddResult(stdout, stderr, returncode, cmd=cmd, exception=exception)
54
55    @property
56    def stdout(self):
57        """Returns command stdout output.
58
59        If multiple command results are included in the object, only the last one is returned.
60        """
61        return self.stdouts[-1]
62
63    @property
64    def stderr(self):
65        """Returns command stderr output.
66
67        If multiple command results are included in the object, only the last one is returned.
68        """
69        return self.stderrs[-1]
70
71    @property
72    def returncode(self):
73        """Returns command returncode output.
74
75        If multiple command results are included in the object, only the last one is returned.
76        """
77        return self.returncodes[-1]
78
79    @property
80    def cmd(self):
81        """Returns original command that generates the result.
82
83        If multiple commands are included in the object, only the last one is returned.
84        """
85        return self.cmds[-1]
86
87    @property
88    def exception(self):
89        """Returns the exception that prevented the command from generating result.
90
91        If multiple commands are included in the object, only the last one is returned.
92        """
93        return self.exceptions[-1]
94
95    def AddResult(self, stdout, stderr, returncode, cmd=None, exception=None):
96        """Adds additional command result data to the object.
97
98        Args:
99            stdout: string, command stdout output.
100            stderr: string, command stderr output.
101            returncode: int, command returncode output.
102            cmd: string, original command that generates the result.
103                 Defaults to None.
104            exception: Exception or string, the exception that prevented the command
105                       from generating result.
106        """
107        self.stdouts.append(stdout)
108        self.stderrs.append(stderr)
109        self.returncodes.append(returncode)
110        self.cmds.append(cmd)
111        self.exceptions.append(exception)
112
113    def __getitem__(self, key):
114        """Legacy code support for getting results as a dictionary.
115
116        Args:
117            key: string, commend result type.
118
119        Returns:
120            list of string or int, command results corresponding to provided key.
121
122        Raises:
123            KeyError if key is not specified in const.
124        """
125        if key == cmd_utils.STDOUT:
126            return self.stdouts
127        elif key == cmd_utils.STDERR:
128            return self.stderrs
129        elif key == cmd_utils.EXIT_CODE:
130            return self.returncodes
131        else:
132            raise KeyError(key)