1#
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the 'License');
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an 'AS IS' BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import logging
18
19from host_controller.command_processor import base_command_processor
20
21
22class CommandList(base_command_processor.BaseCommandProcessor):
23    """Command processor for list command.
24
25    Attributes:
26        arg_parser: ConsoleArgumentParser object, argument parser.
27        console: cmd.Cmd console object.
28        command: string, command name which this processor will handle.
29        command_detail: string, detailed explanation for the command.
30    """
31
32    command = "list"
33    command_detail = "Show information about the hosts."
34
35    def _PrintHosts(self, hosts):
36        """Shows a list of host controllers.
37
38        Args:
39            hosts: A list of HostController objects.
40        """
41        self.console._Print("index  name")
42        for ind, host in enumerate(hosts):
43            self.console._Print("[%3d]  %s" % (ind, host.hostname))
44
45    def _PrintDevices(self, devices):
46        """Shows a list of devices.
47
48        Args:
49            devices: A list of DeviceInfo objects.
50        """
51        attr_names = ("device_serial", "state", "run_target", "build_id",
52                      "sdk_version", "stub")
53        self.console._PrintObjects(devices, attr_names)
54
55    # @Override
56    def SetUp(self):
57        """Initializes the parser for list command."""
58        self.arg_parser.add_argument(
59            "--host", type=int, help="The index of the host.")
60        self.arg_parser.add_argument(
61            "type",
62            choices=("hosts", "devices"),
63            help="The type of the shown objects.")
64
65    # @Override
66    def Run(self, arg_line):
67        """Shows information about the hosts."""
68        args = self.arg_parser.ParseLine(arg_line)
69        if args.host is None:
70            hosts = enumerate(self.console._hosts)
71        else:
72            hosts = [(args.host, self.console._hosts[args.host])]
73        if args.type == "hosts":
74            self._PrintHosts(self.console._hosts)
75        elif args.type == "devices":
76            for ind, host in hosts:
77                devices = host.ListDevices()
78                self.console._Print("[%3d]  %s" % (ind, host.hostname))
79                self._PrintDevices(devices)
80
81    def Help(self):
82        base_command_processor.BaseCommandProcessor.Help(self)
83        logging.info("Sample: build --target=aosp_sailfish-userdebug "
84                     "--branch=<branch name> --artifact-type=device")
85