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 21from vts.utils.python.common import cmd_utils 22 23 24class CommandShell(base_command_processor.BaseCommandProcessor): 25 """Command processor for shell command. 26 27 Attributes: 28 arg_parser: ConsoleArgumentParser object, argument parser. 29 command: string, command name which this processor will handle. 30 command_detail: string, detailed explanation for the command. 31 """ 32 33 command = "shell" 34 command_detail = "Runs a shell command on the host OS." 35 36 # @Override 37 def SetUp(self): 38 """Initializes the parser for device command.""" 39 self.arg_parser.add_argument( 40 "command", 41 metavar="COMMAND", 42 nargs="+", 43 help="The command to be executed. If the command contains " 44 "arguments starting with \"-\", place the command at end of line " 45 "after \"--\".") 46 47 # @Override 48 def Run(self, arg_line): 49 """Runs a shell command.""" 50 args = self.arg_parser.ParseLine(arg_line) 51 cmd_list = self.ReplaceVars(args.command) 52 stdout, stderr, retcode = cmd_utils.ExecuteOneShellCommand( 53 " ".join(cmd_list)) 54 if stdout: 55 logging.info(stdout) 56 if stderr: 57 logging.error(stderr) 58 if retcode != 0: 59 return False 60