Skip to content

Instantly share code, notes, and snippets.

@TheHippo
Created March 3, 2014 16:50
Show Gist options
  • Save TheHippo/9329179 to your computer and use it in GitHub Desktop.
Save TheHippo/9329179 to your computer and use it in GitHub Desktop.
Wrapper script for working with adb and multiple Android devices

Usage

$set_android_device.py adb shell
(1) - R32D3006XXX [device usb:3-1.1 product:mantaray model:Nexus_10 device:manta]
(2) - 01499EAD0701XXXX [device usb:3-1.4 product:yakju model:Galaxy_Nexus device:maguro]
2
shell@maguro:/ $ 

How it works

  • The script reads all available devices through adb and let you chose one.
  • It set the environment variable ANDROID_SERIAL to the selected device
  • Executes the command that is attached throught the arguments
#!/usr/bin/env python
import subprocess
import sys
import os
def parse_device_list(str):
raw_list = filter(None, str.splitlines())[1:]
devices = []
for raw_device in raw_list:
parts = raw_device.split()
devices.append((parts[0], " ".join(parts[1:])))
return devices
def get_device_list():
p = subprocess.Popen(['adb', 'devices', '-l'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if len(err) == 0:
return parse_device_list(out)
else:
return
devices = get_device_list()
if devices == None:
print("Could not find any device")
sys.exit(1)
for i in range(len(devices)):
id, name = devices[i]
print("(%d) - %s [%s]" % (i+1, id, name))
def num(s):
try:
return int(s)
except exceptions.ValueError:
return
while True:
choosen = num(sys.stdin.readline())
if (choosen != None and choosen > 0 and choosen <= len(devices)):
break
os.environ['ANDROID_SERIAL'] = devices[choosen - 1][0]
p = subprocess.Popen(sys.argv[1:])
try:
p.communicate()
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment