-
Notifications
You must be signed in to change notification settings - Fork 4
/
pilot.py
135 lines (104 loc) · 3.62 KB
/
pilot.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import json
import requests
import logging
import time
logger = logging.getLogger('pilot')
class PilotException(Exception):
pass
class Pilot(object):
_WAIT_SLEEP_TIME = 5
def __init__(self, baseUrl):
self.baseUrl = baseUrl.strip('/')
def _wait_for_task_finished(self, taskInfo=None):
finished = False
while not finished:
r = requests.get('%s/status' % self.baseUrl)
if (r.status_code != 200):
raise PilotException('unable to get pilot status from device: %s' % r.text)
status = json.loads(r.text)
if 'taskRunning' not in status:
raise PilotException('Invalid Pilot status: %s' % r.text)
# check if finished
if status['taskRunning'] == False:
finished = True
# check if the next job is already running
elif taskInfo and status['taskInfo'] != taskInfo:
finished = True
else:
logger.debug('waiting for task to finish...')
time.sleep(Pilot._WAIT_SLEEP_TIME)
logger.info('task finished.')
def installed_applications(self):
r = requests.get('%s/applications' % self.baseUrl)
if (r.status_code != 200):
raise PilotException('unable to get list of installed apps from device: %s' % r.text)
apps = json.loads(r.text)
if not apps:
return {}
return apps
def install_appstore(self, appInfo, accountIdentifier, taskInfo={}):
''' this method will return if the app download was successfully initiated (probably the app will not yet be fully installed)
'''
## add some debugging data
taskInfo.update({'worker_action':'install_appstore'})
## appInfo contains store data and thus the bundleID key is 'bundle-id'!!
if 'bundle-id' in appInfo:
bundleId = appInfo['bundle-id']
if bundleId in self.installed_applications():
raise PilotException('App already installed! <%s>' % bundleId)
data = {
'appInfo': appInfo,
'accountIdentifier': accountIdentifier,
'taskInfo': taskInfo
}
r = requests.post("%s/install/appstore" % self.baseUrl, data=json.dumps(data))
if (r.status_code != 200):
logger.error("Initiating install failed! Response: %s" % r.text)
return False
self._wait_for_task_finished()
result = True
if 'bundle-id' in appInfo:
result = appInfo['bundle-id'] in self.installed_applications()
return result
def install_cydia(self, bundleId, taskInfo={}):
data = {
'bundleId': bundleId,
'taskInfo': taskInfo
}
r = requests.post("%s/install/cydia" % self.baseUrl, data=json.dumps(data))
if (r.status_code != 200):
logger.error("Install failed! Response: %s" % r.text)
return False
return True
def open(self, bundleId, taskInfo={}):
data = {
'taskInfo': taskInfo
}
r = requests.post("%s/open/%s" % (self.baseUrl, bundleId), data=json.dumps(data))
if (r.status_code != 200):
logger.error("Open failed! <%s> Response: %s" % (bundleId, r.text))
def run_auto_execution(self, bundleId, taskInfo={}):
logger.info('starting execution of %s', bundleId)
taskInfo['bundleId'] = bundleId
data = {
'taskInfo': taskInfo
}
r = requests.post("%s/execute/%s" % (self.baseUrl, bundleId), data=json.dumps(data))
if (r.status_code != 200):
logger.error("Open failed! <%s> Response: %s" % (bundleId, r.text))
return False
## wait until the execution has finished
self._wait_for_task_finished(taskInfo=taskInfo)
logger.info('execution of %s finished', bundleId)
return True
def inject(self, process, command, taskInfo={}):
data = {
'process': process,
'command': command,
'taskInfo': taskInfo
}
r = requests.post("%s/inject" % self.baseUrl, data=json.dumps(data))
if (r.status_code != 200):
logger.error("Inject failed! Response: %s" % r.text)
return None
return json.loads(r.text)