Home | History | Annotate | Download | only in mirror
      1 #
      2 # Copyright (C) 2016 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 
     17 from vts.runners.host import const
     18 
     19 
     20 class ShellMirrorObject(object):
     21     '''The class that mirrors a shell on the native side.
     22 
     23     Attributes:
     24         _client: the TCP client instance.
     25         enabled: bool, whether RPC shell feature is enabled for the device.
     26     '''
     27 
     28     def __init__(self, client):
     29         self._client = client
     30         self.enabled = True
     31 
     32     def Execute(self, command, no_except=False):
     33         '''Execute remote shell commands on device.
     34 
     35         Args:
     36             command: string or a list of string, shell commands to execute on
     37                      device.
     38             no_except: bool, if set to True, no exception will be thrown and
     39                        error code will be -1 with error message on stderr.
     40 
     41         Returns:
     42             A dictionary containing shell command execution results
     43         '''
     44         if not self.enabled:
     45             # TODO(yuexima): use adb shell instead when RPC is disabled
     46             return {
     47                 const.STDOUT: [""] * len(command),
     48                 const.STDERR:
     49                 ["VTS remote shell has been disabled."] * len(command),
     50                 const.EXIT_CODE: [-2] * len(command)
     51             }
     52         return self._client.ExecuteShellCommand(command, no_except)
     53 
     54     def CleanUp(self):
     55         self._client.Disconnect()
     56