Last active
August 29, 2015 14:23
-
-
Save archector/33e8344b752279b11985 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import re | |
import os | |
import sys | |
################################################################################################ | |
# ADB path | |
ADB = "/Users/darren/Library/Android/sdk/platform-tools/adb" | |
# image destination path | |
IMAGE_PATH = "/Users/darren/Desktop/images/" | |
# matched prefix | |
PREFIX = "192.168" | |
# Path to Robotium-Screenshots | |
ROBOTIUM_PATH = "sdcard/Robotium-Screenshots/" | |
# id | |
ID = "1" | |
################################################################################################ | |
# check if directory exists, and create it if necessary | |
def createDir(str): | |
if not os.path.exists(str): | |
os.makedirs(str) | |
return | |
def usage(): | |
print 'usage: python ' + sys.argv[0] + ' {PATH_TO_YOUR_ADB} {PATH_TO_YOUR_IMAGE_DIRECTORY} {id}' | |
def cmd_get_devices_ip(): | |
return ADB + " devices | tail -n +2 | grep " + PREFIX + " | cut -sf 1 " | |
def cmd_get_devices_spec(): | |
return ADB + " devices -l | tail -n +2 | grep " + PREFIX + " | cut -d ' ' -f 7 | cut -d ':' -f 2" | |
def cmd_find_jpgs(): | |
return "find " + IMAGE_PATH + " -name '*.jpg'" | |
def get_image_name(str): | |
tokenize_list = re.split('/', str) | |
return [tokenize_list[len(tokenize_list) - 2], tokenize_list[len(tokenize_list) - 1]] | |
################################################################################################ | |
if len(sys.argv) != 4: | |
usage() | |
exit() | |
else: | |
ADB = sys.argv[1] | |
IMAGE_PATH = sys.argv[2] | |
if (IMAGE_PATH[-1:] != "/"): | |
IMAGE_PATH += "/" | |
ID = str(sys.argv[3]) | |
print "Fetching devices information...." | |
# get a list of devices' ip | |
devices_ip = subprocess.Popen(cmd_get_devices_ip(), shell=True, stdout=subprocess.PIPE) | |
ip_list = re.split('\n', devices_ip.communicate()[0]) | |
ip_list = filter(None, ip_list) | |
print ip_list | |
# get a list of devices' model name | |
devices_spec = subprocess.Popen(cmd_get_devices_spec(), shell=True, stdout=subprocess.PIPE) | |
model_list = re.split('\n', devices_spec.communicate()[0]) | |
model_list = filter(None, model_list) | |
print model_list | |
print "\nCreating necessary directories..." | |
# create directories | |
createDir(IMAGE_PATH) | |
createDir(IMAGE_PATH + ID) | |
for model in model_list: | |
createDir(IMAGE_PATH + ID + "/" + model) | |
print "\nPulling images from devices..." | |
# fetch image and put into directory | |
for index in range(len(model_list)): | |
ip = ip_list[index] | |
model = model_list[index] | |
subprocess.call([ADB, '-s', ip, 'pull', ROBOTIUM_PATH, IMAGE_PATH + ID + "/" + model]) | |
print "\nOrganizing images..." | |
image_paths = subprocess.Popen(cmd_find_jpgs(), shell=True, stdout=subprocess.PIPE) | |
image_path_list = re.split('\n', image_paths.communicate()[0]) | |
image_path_list = filter(None, image_path_list) | |
for image_path in image_path_list: | |
folder_name, picture_name = get_image_name(image_path) | |
new_folder_name = picture_name.replace(".jpg", "") | |
subprocess.call(["mv", IMAGE_PATH + ID + "/" + folder_name + "/" + picture_name, IMAGE_PATH + ID + "/" + new_folder_name + "-" + folder_name + ".jpg"]) | |
# delete images if required | |
print "\nRemoving images from devices..." | |
for index in range(len(model_list)): | |
ip = ip_list[index] | |
subprocess.call([ADB, '-s', ip, 'shell', 'rm', '-r', ROBOTIUM_PATH]) | |
# done | |
print "\nDONE!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import re | |
import sys | |
import time | |
from threading import Thread | |
################################################################################################ | |
# GENYMOTION | |
GENYMOTION = "/Applications/Genymotion.app/Contents/MacOS/player" | |
# ADB | |
ADB = "/Users/darren/Library/Android/sdk/platform-tools/adb" | |
# SLEEP TIME | |
SLEEP_TIME = 60 | |
# Commands | |
cmd_get_vms = "vboxmanage list vms | cut -d '\"' -f 2" | |
cmd_shutdown_vms = "vboxmanage list vms | cut -d '\"' -f 2 | xargs -I {} vboxmanage controlvm {} poweroff" | |
################################################################################################ | |
def usage(): | |
print 'usage: python ' + sys.argv[0] + ' {PATH_TO_YOUR_GENYMOTION_PLAYER} {PATH_TO_YOUR_ADB} {SLEEP_TIME}' | |
def cmd_unlock_devices(): | |
return ADB + " devices | tail -n +2 | cut -sf 1 | xargs -I {} " + ADB + " -s {} shell input keyevent KEYCODE_MENU" | |
def cmd_kill_processes(): | |
return "ps | grep " + GENYMOTION + " | grep -v " + sys.argv[0] + " | awk '{print $1}' | xargs kill" | |
def craft_vbox_command(vm_list): | |
cmd = "" | |
for index in range(len(vm_list)): | |
vm = vm_list[index] | |
if index == len(vm_list) - 1: | |
cmd += GENYMOTION + ' --vm-name \"' + vm + "\"" | |
else: | |
cmd += GENYMOTION + ' --vm-name \"' + vm + "\" & " | |
cmd += " & " + "sleep " + str(SLEEP_TIME) | |
return cmd | |
def sleep_and_unlock_devices(): | |
time.sleep(float(SLEEP_TIME)) | |
print "\nUnlocking devices..." | |
subprocess.call(cmd_unlock_devices(), shell=True) | |
return | |
def start_and_sleep_devices(): | |
print "\nStarting available vms..." | |
print craft_vbox_command(available_vm_list) | |
subprocess.call(craft_vbox_command(available_vm_list), shell=True) | |
################################################################################################ | |
if len(sys.argv) != 4: | |
usage() | |
exit() | |
else: | |
GENYMOTION = sys.argv[1] | |
ADB = sys.argv[2] | |
SLEEP_TIME = sys.argv[3] | |
print "Killing idle processes and vms..." | |
subprocess.call(cmd_kill_processes(), shell=True) | |
subprocess.call(cmd_shutdown_vms, shell=True) | |
print "\nFetching vms information..." | |
vb_vms = subprocess.Popen(cmd_get_vms, shell=True, stdout=subprocess.PIPE) | |
vm_list = re.split('\n', vb_vms.communicate()[0]) | |
vm_list = filter(None, vm_list) | |
print vm_list | |
print "\nRestoring vms..." | |
available_vm_list = [] | |
for vm in vm_list: | |
code = subprocess.call(['vboxmanage', 'snapshot', vm, 'restore', 'factory']) | |
if code == 0: | |
available_vm_list.append(vm) | |
# stop if no devices | |
if (len(available_vm_list) == 0): | |
print "NO AVAILAVBLE VIRTUAL DEVICES!!!!!!" | |
sys.exit() | |
# sleep & unlock device in background thread | |
thread1 = Thread(target = sleep_and_unlock_devices) | |
thread1.start() | |
# start vms and wait for the first thread to call unlock, and pray nothing bad happends | |
thread2 = Thread(target = start_and_sleep_devices) | |
thread2.start() | |
thread2.join() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import re | |
import sys | |
import time | |
# GENYMOTION | |
GENYMOTION = "/Applications/Genymotion.app/Contents/MacOS/player" | |
# ADB | |
ADB = "/Users/darren/Library/Android/sdk/platform-tools/adb" | |
# Device Name | |
DEVICE = "Google Nexus 6 - 5.0.0 - API 21 - 1440x2560" | |
################################################################################################ | |
def kill_device(genymotion, device): | |
print "\nKilling device \"" + device + "\" process..." | |
print "ps | grep \"" + genymotion + "\" | grep \"" + device + "\" | awk '{print $1}' | xargs kill" | |
subprocess.call("ps | grep \"" + genymotion + "\" | grep \"" + device + "\" | grep -v " + sys.argv[0] + " | awk '{print $1}' | xargs kill", shell=True) | |
def poweroff_device(device): | |
print "\nPowering off virtual device..." | |
code = subprocess.call("vboxmanage controlvm \"" + device + "\" poweroff", shell=True) | |
def fetch_vm(device): | |
print "\nGetting device information..." | |
vms = subprocess.Popen("vboxmanage list vms | cut -d '\"' -f 2", shell=True, stdout=subprocess.PIPE) | |
vm_list = re.split('\n', vms.communicate()[0]) | |
if any(device in s for s in vm_list): | |
print "\nDevice \"" + device + "\" exists." | |
else: | |
print "\nDevice \"" + device + "\" does not exist. Shutting down..." | |
sys.exit(1) | |
def restore_snapshot(device): | |
print "\nRestoring snapshot..." | |
code = subprocess.call("vboxmanage snapshot \"" + device + "\" restore \"factory\"", shell=True) | |
if (code == 0): | |
print "\nSuccessfully restore snapshot" | |
else: | |
print "\nFailed to restore snapshot. Shutting down..." | |
sys.exit(1) | |
def get_device_ip(device): | |
print "\nGetting device \"" + device + "\" ip..." | |
device_name = device.replace(" ", "_") | |
device_name = device_name.replace("-", "_") | |
device_name = device_name.replace(".", "_") | |
print "\nFinding \"" + device_name + "\" from adb devices..." | |
ip = subprocess.Popen(ADB + " devices -l | grep " + device_name + " | cut -d ' ' -f 1 ", shell=True, stdout=subprocess.PIPE).communicate()[0] | |
ip = ip.replace("\n", "") | |
print "\nFound \"" + ip + "\"" | |
return ip | |
def get_all_devices_ip(): | |
print "\nGetting all devices ip..." | |
ips = subprocess.Popen(ADB + " devices | tail -n +2 | awk '{print $1}'", shell=True, stdout=subprocess.PIPE).communicate()[0] | |
ip_list = ips.split("\n") | |
ip_list = filter(None, ip_list) | |
return ip_list | |
def get_ip_from_ip_list(device): | |
ip_list = get_all_devices_ip(); | |
print "\nLooping through devices to find " + device + "..." | |
for ip in ip_list: | |
model = subprocess.Popen(ADB + " -s " + ip + " shell getprop ro.product.model", shell=True, stdout=subprocess.PIPE).communicate()[0] | |
model = model.replace("\n", "") | |
model = model.replace("\r", "") | |
print model | |
if model == device: | |
print "found: " + model | |
return ip | |
return "" | |
def play_snapshot(device): | |
subprocess.Popen([GENYMOTION, "--vm-name", device], stdout=subprocess.PIPE) | |
# subprocess.call(GENYMOTION + " --vm-name \"" + device + "\"", shell=True); | |
def check_status(device_ip): | |
print "\nChecking device \"" + device_ip + "\" connection..." | |
print ADB + " -s " + device_ip + " shell getprop init.svc.bootanim" | |
result = subprocess.Popen(ADB + " -s " + device_ip + " shell getprop init.svc.bootanim", shell=True, stdout=subprocess.PIPE).communicate()[0] | |
print result | |
status = result.replace("\n", "") | |
status = status.replace("\r", "") | |
return status | |
def unlock_device(device_ip): | |
print "\nUnlocking device \"" + device_ip + "\"..." | |
subprocess.call(ADB + " -s " + device_ip + " shell input keyevent KEYCODE_MENU", shell=True) | |
def usage(): | |
print 'usage: python ' + sys.argv[0] + ' {PATH_TO_YOUR_GENYMOTION_PLAYER} {PATH_TO_YOUR_ADB} {DEVICE}' | |
################################################################################################ | |
if len(sys.argv) != 4: | |
usage() | |
sys.exit(1) | |
else: | |
GENYMOTION = str(sys.argv[1]) | |
ADB = str(sys.argv[2]) | |
DEVICE = str(sys.argv[3]) | |
kill_device(GENYMOTION, DEVICE) | |
sys.stdout.flush() | |
poweroff_device(DEVICE) | |
sys.stdout.flush() | |
fetch_vm(DEVICE) | |
sys.stdout.flush() | |
restore_snapshot(DEVICE) | |
sys.stdout.flush() | |
play_snapshot(DEVICE) | |
time.sleep(5) | |
sys.stdout.flush() | |
ip = "" | |
retry = 10 | |
while (True): | |
retry -= 1 | |
if (retry <= 0): | |
print "\nUnable to find device ip \"" + DEVICE + "\"!" | |
print "\nChecking adb shell getprop to see if device exists..." | |
sys.stdout.flush() | |
ip = get_ip_from_ip_list(DEVICE) | |
if (ip == ""): | |
print "\nFail to find device... Exiting..." | |
sys.stdout.flush() | |
sys.exit(-1) | |
else: | |
break | |
else: | |
print "\nRetry getting IP count: " + str(retry) + "..." | |
sys.stdout.flush() | |
ip = get_device_ip(DEVICE) | |
if (ip != ""): | |
print "\nIP found: " + ip | |
break | |
time.sleep(5) | |
retry = 100; | |
while (True): | |
retry -= 1 | |
if (retry <= 0): | |
print "\nUnable to start device \"" + DEVICE + "\"!" | |
sys.exit(1) | |
print "\nRetry connection count: " + str(retry) + "..." | |
sys.stdout.flush() | |
result = check_status(ip) | |
if (result == "stopped"): | |
print "\nDevice " + DEVICE + " is fully started!" | |
break; | |
time.sleep(5) | |
unlock_device(ip) | |
time.sleep(5) | |
print "DONE!!!" | |
sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment