-
Notifications
You must be signed in to change notification settings - Fork 1
/
erase_device.py
37 lines (31 loc) · 968 Bytes
/
erase_device.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
#!/usr/bin/python
import os.path
import sys
from subprocess import check_output, call
import re
device_id = sys.argv[1]
device_name = "Unknown"
def is_device_running(device_id):
result = check_output(['xcrun', 'simctl', 'list'])
name = None
for line in result.split('\n'):
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:
device_id_check = m.group(3)
if device_id == device_id_check:
name = m.group(2)
device_id_check = m.group(3)
device_status = m.group(4)
if "Booted" == device_status:
return True, name
return False, name
isRunning, device_name = is_device_running(device_id)
# uninstall app
if not isRunning:
result = call(['xcrun', 'simctl', 'erase', device_id])
if result != 0:
print "Failed to erase device: %s" % (device_name)
else:
print 'Erased %s' % (device_name)
else:
print "Can't erase %s, already booted" % (device_name)