-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimctl.py
104 lines (93 loc) · 2.77 KB
/
simctl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
from glob import glob
import re
from subprocess import check_output, CalledProcessError, STDOUT, call
XCODES = glob("/Applications/Xcode*.app")
# Contents/Developer/Applications/iOS\ Simulator.app
class SimDevice(object):
def __init__(self, info):
self.updateWithInfo(info)
self.info = info
def updateWithInfo(self, info):
self.deviceId = info['id']
self.name = info['name']
self.state = info['state']
self.version = info['version']
def isBooted(self):
if self.state == 'Booted':
return True
return False
def addXcrun(self, xcrun):
self.info['xcruns'].append(xcrun)
def addXcode(self, xcode):
self.info['xcodes'].append(xcode)
def getXcrun(self, xcrunIndex=0):
return self.info['xcruns'][xcrunIndex]
class SimControl(object):
def __init__(self):
self.devices = {}
pass
def bootDevice(self, device):
result = call([device.getXcrun(), 'boot', device.deviceId])
if result != 0:
return False
return True
def shutdownDevice(self, device):
result = call([device.getXcrun(), 'shutdown', device.deviceId])
if result != 0:
return False
return True
def uninstallApp(self, device, app_id):
result = call([device.getXcrun(), 'uninstall', device.deviceId, app_id])
if result != 0:
print "Failed to uninstall app: %s" % (app_id)
return False
return True
def openURL(self, device, url):
result = call([device.getXcrun(), 'openurl', device.deviceId, url])
if result != 0:
print "Failed to openurl app: %s" % (url)
return False
return True
def deviceLookupHash(self):
self.loadDevices()
return self.devices
def loadDevices(self):
for xcode in XCODES:
xcrun = os.path.join(xcode, 'Contents/Developer/usr/bin/simctl')
result = check_output([xcrun, 'list', 'devices'])
lines = re.split("\n+", result)
currentOS = ''
for line in lines:
osMatch = re.search("--\s+(iOS\s+\d+\.\d+)\s+--", line)
if osMatch:
currentOS = osMatch.group(1)
continue
m = re.search("(\s+)(.*)\s\(([\d\w]{8}-[\w\d]{4}-[\w\d]{4}-[\w\d]{4}-[\w\d]{12})\)\s+\((\w+)\)(\s+\((unavailable).*)?", line)
if m:
name = m.group(2)
deviceID = m.group(3)
state = m.group(4)
unavailable = True if m.group(6) else False
if unavailable:
continue
info = {'name': name,
'id': deviceID,
'state': state,
'unavailable': unavailable,
'xcruns': [xcrun],
'xcodes': [os.path.basename(xcode)],
'version': currentOS}
if self.devices.has_key(deviceID):
device = self.devices[deviceID]
device.addXcrun(xcrun)
device.addXcode(xcode)
else:
device = SimDevice(info)
self.devices[deviceID] = device
def activeDevices(self):
self.loadDevices()
return self.devices.values()
if __name__ == '__main__':
sim = SimControl()
print sim.activeDevices()