From a8731c4be0a56fd3a4de2034863d3bc397a282bd Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Mon, 21 Sep 2015 11:47:52 -0700 Subject: [PATCH] Add helper functions for `adb -e/-d` to adb.py. Bug: http://b/22946322 Change-Id: I847f561d617b17f4e6323e0abb46dd270d041782 --- python-packages/adb/device.py | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/python-packages/adb/device.py b/python-packages/adb/device.py index 5a8a6f5e7..e19c4524e 100644 --- a/python-packages/adb/device.py +++ b/python-packages/adb/device.py @@ -110,6 +110,46 @@ def get_device(serial=None, product=None): return _get_unique_device(product) + +def _get_device_by_type(flag): + with open(os.devnull, 'wb') as devnull: + subprocess.check_call(['adb', 'start-server'], stdout=devnull, + stderr=devnull) + try: + serial = subprocess.check_output(['adb', flag, 'get-serialno']).strip() + except subprocess.CalledProcessError: + raise RuntimeError('adb unexpectedly returned nonzero') + if serial == 'unknown': + raise NoUniqueDeviceError() + return _get_device_by_serial(serial) + + +def get_usb_device(): + """Get the unique USB-connected AndroidDevice if it is available. + + Raises: + NoUniqueDeviceError: + 0 or multiple devices are connected via USB. + + Returns: + An AndroidDevice associated with the unique USB-connected device. + """ + return _get_device_by_type('-d') + + +def get_emulator_device(): + """Get the unique emulator AndroidDevice if it is available. + + Raises: + NoUniqueDeviceError: + 0 or multiple emulators are running. + + Returns: + An AndroidDevice associated with the unique running emulator. + """ + return _get_device_by_type('-e') + + # Call this instead of subprocess.check_output() to work-around issue in Python # 2's subprocess class on Windows where it doesn't support Unicode. This # writes the command line to a UTF-8 batch file that is properly interpreted