Home | History | Annotate | Download | only in tradefed_py
      1 #
      2 # Copyright (C) 2017 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 import adb_handler
     18 
     19 class AndroidTestDevice(object):
     20     """Class representing an android device.
     21 
     22     Each instance represents a different device connected to adb.
     23     """
     24 
     25     def __init__(self, serial=None, stream=None):
     26         # TODO: Implement and flesh out the device interface
     27         self.serial = serial
     28         self._logging = stream
     29         self.adb = adb_handler.AdbHandler(serial)
     30 
     31     def executeShellCommand(self, cmd):
     32         """Convenience method to call the adb wrapper to execute a shell command.
     33 
     34         Args:
     35             cmd: The command to be executed in 'adb shell'
     36 
     37         Returns:
     38             The stdout of the command if succeed. Or raise AdbError if failed.
     39         """
     40         return self.adb.exec_shell_command(cmd)
     41 
     42     def getProp(self, name):
     43         if not name:
     44             raise DeviceCommandError('getProp', 'Name of property cannot be None')
     45         out = self.executeShellCommand('getprop %s' % name)
     46         return out.strip()
     47 
     48     def _printHostLog(self, message):
     49         self._logging.write('%s \n' % message)
     50 
     51 class DeviceCommandError(Exception):
     52     """ Exception raised when an error is encountered while running a command.
     53     """
     54 
     55     def __init__(self, cmd, message):
     56         self.cmd = cmd
     57         self.message = message
     58 
     59     def __str__(self):
     60         return ('Error executing device cmd "%s". message: "%s"'
     61                 ) % (self.cmd, self.message)
     62