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
17from host_controller.command_processor import base_command_processor
18from host_controller.console_argument_parser import ConsoleArgumentError
19
20
21class CommandLease(base_command_processor.BaseCommandProcessor):
22    """Command processor for lease command.
23
24    Attributes:
25        arg_parser: ConsoleArgumentParser object, argument parser.
26        console: cmd.Cmd console object.
27        command: string, command name which this processor will handle.
28        command_detail: string, detailed explanation for the command.
29    """
30
31    command = "lease"
32    command_detail = "Make a host lease command tasks from TFC."
33
34    def _PrintTasks(self, tasks):
35        """Shows a list of command tasks.
36
37        Args:
38            devices: A list of DeviceInfo objects.
39        """
40        attr_names = ("request_id", "command_id", "task_id", "device_serials",
41                      "command_line")
42        self.console._PrintObjects(tasks, attr_names)
43
44    # @Override
45    def SetUp(self):
46        """Initializes the parser for lease command."""
47        self.arg_parser.add_argument(
48            "--host", type=int, help="The index of the host.")
49
50    # @Override
51    def Run(self, arg_line):
52        """Makes a host lease command tasks from TFC."""
53        args = self.arg_parser.ParseLine(arg_line)
54        if args.host is None:
55            if len(self.console._hosts) > 1:
56                raise ConsoleArgumentError("More than one hosts.")
57            args.host = 0
58        tasks = self.console._hosts[args.host].LeaseCommandTasks()
59        self._PrintTasks(tasks)