forked from DiOS-Analysis/Worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
258 lines (216 loc) · 7.22 KB
/
backend.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import requests
import json
import logging
import shutil
import base64
from enum import Enum
#from job import Job
#from device import iDevice
logger = logging.getLogger('worker.'+__name__)
def json_serialize_ignore_type_errors(obj):
try:
print 'encoding: %s' % str(obj)
return base64.b64encode(obj)
except TypeError:
return "##NONE_SERIALIZABLE_OBJECT##"
def jd(obj):
data = json.dumps(obj, default=json_serialize_ignore_type_errors)
return data
class BackendError(Exception):
pass
class Backend(object):
HEADERS = {'content-type': 'application/json'}
RUN_STATE = Enum(['pending', 'running', 'finished', 'failed'])
def __init__(self, baseUrl):
self.baseUrl = baseUrl.strip('/')
self.workerId = None
def register_device_accounts(self, device):
logger.debug("register_device_accounts: %s", device)
backendAccounts = self.get_accounts()
for account in device.accounts():
if not backendAccounts or account['uniqueIdentifier'] not in backendAccounts:
self.post_account(account)
# get device data and check/register with backend
def register_device(self, device):
logger.debug("register_device: %s", device)
r = requests.get("%s/devices/%s" % (self.baseUrl, device.udid))
if (r.status_code == 404):
self.register_device_accounts(device)
r2 = requests.post("%s/devices" % self.baseUrl, headers=self.HEADERS, data=jd({
'udid': device.udid,
'accounts': list(str(acc['uniqueIdentifier']) for acc in device.accounts()),
'deviceInfo': device.device_info_dict()
}))
if (r2.status_code != 200):
logger.error("Unable to register new device: %s" % str(device))
logger.debug("Response: %s" % r2.text)
return False
return True
# gets the worker from backend
# will even create a new worker if none found
# raises a BackendError if the given name is not unique
def worker_for_name(self, name):
logger.debug("worker_for_name: %s", name)
r = requests.get("%s/workers?name=%s" % (self.baseUrl, name))
if r.status_code == 200:
workers = json.loads(r.text)
if len(workers) != 1:
raise BackendError('The workers name is not unique! (%s)'%(name))
worker = workers.values()[0]
if not '_id' in worker:
raise BackendError('No worker-id present!')
return worker
elif r.status_code == 404:
# create a new worker
r2 = requests.post("%s/workers" % self.baseUrl, headers=self.HEADERS, data=jd({
'name': name
}))
if r2.status_code == 200:
return self.worker_for_name(name)
else:
raise BackendError('Unable to create worker: %s' % r2.text)
else:
raise BackendError('Unknown backend error: %s' % r.text)
def get_accounts(self):
logger.debug("get_accounts")
r = requests.get("%s/accounts" % self.baseUrl)
if r.status_code == 200:
return json.loads(r.text)
else:
return {}
def post_account(self, account):
logger.debug("post_account")
r = requests.post("%s/accounts" % self.baseUrl, headers=self.HEADERS, data=jd(account))
if (r.status_code != 200):
logger.error("Unable to add new account: %s" % str(account))
logger.debug("Response: %s" % r.text)
return False
return True
# #returns:
# 200: Job object
# 204: None
# else: False
def get_job_for_device(self, deviceUUID):
logger.debug("get_job_for_device: %s", deviceUUID)
r = requests.get("%s/jobs/getandsetworker/%s/device/%s" % (self.baseUrl, self.workerId, deviceUUID))
if r.status_code == 200:
jobDict = json.loads(r.text)
return jobDict
elif r.status_code == 204:
return None
else:
logger.error("ERROR: get_job failed with: %s" % str(r.status_code))
logger.debug(r.text)
return False
def get_job(self, jobId):
logger.debug("get_job: %s", jobId)
r = requests.get("%s/jobs/%s" % (self.baseUrl, jobId))
if r.status_code == 200:
jobDict = json.loads(r.text)
return jobDict
def post_job(self, jobDict):
logger.debug("post_job: %s", jobDict)
r = requests.post("%s/jobs" % self.baseUrl, data=jd(jobDict), headers=self.HEADERS)
if r.status_code == 200:
return json.loads(r.text)['jobId']
else:
logger.warning("Unable to post job: %s" % str(jobDict))
logger.debug("Response: %s" % r.text)
return None
# returns appId
def post_app(self, appData):
logger.debug("post_app: %s", appData)
r = requests.post("%s/apps" % self.baseUrl, data=jd(appData), headers=self.HEADERS)
if r.status_code == 200:
return json.loads(r.text)['appId']
else:
logger.warning("Unable to post app: %s" % str(appData))
logger.debug("Response: %s" % r.text)
return None
def get_app_bundleId(self, bundleId, version=None):
''' get the appId for given variables
returns: appId or None if the result is not unique
'''
logger.debug("get_app_bundleId: %s", bundleId)
url = "%s/apps/bundleid/%s" % (self.baseUrl, bundleId)
if version:
url += '?version=%s' % version
r = requests.get(url)
if r.status_code == 200:
appsDict = json.loads(r.text)
if len(appsDict) == 1:
return appsDict.values()[0]
logger.debug('%s returned %s results' % (url, len(appsDict)))
else:
logger.error('%s request failed: %s %s' % (url, r.status_code, r.text))
return None
def get_app_archive(self, appId, archivePath):
''' get the ipa file from backend
returns: True or False
'''
logger.debug("get_app_archive: %s", appId)
r = requests.get("%s/apps/%s/ipa" % (self.baseUrl, appId))
if r.status_code == 200:
f = open(archivePath, 'wb')
f.write(r.content)
#shutil.copyfileobj(r.raw, f)
f.close()
return True
else:
logger.warning("Unable to get app archive for app %s" % appId)
logger.debug("Response: %s" % r.text)
return False
def has_app_archive(self, appId):
logger.debug("has_app_archive: %s", appId)
logger.debug("%s/apps/%s/ipa" % (self.baseUrl, appId))
r = requests.head("%s/apps/%s/ipa" % (self.baseUrl, appId))
if r.status_code == 200:
return True
return False
# returns appId
def post_app_archive(self, appId, archivePath):
logger.debug("post_app_archive: %s", appId)
r = requests.post("%s/apps/%s/ipa" % (self.baseUrl, appId), files={
'ipa': open(archivePath, 'rb')
})
if r.status_code == 200:
return json.loads(r.text)['appId']
else:
logger.warning("Unable to post app archive for app %s" % appId)
logger.debug("Response: %s" % r.text)
return None
# returns runId
def post_run(self, appId, runState, runId=None, executionStrategy=None):
logger.debug("post_run: %s", appId)
data={
'app': appId,
'state': runState
}
if runId:
data['_id'] = runId
if executionStrategy:
data['executionStrategy'] = executionStrategy
r = requests.post("%s/runs" % self.baseUrl, data=jd(data), headers=self.HEADERS)
if r.status_code == 200:
return json.loads(r.text)['runId']
else:
logger.warning("Unable to post run: %s" % str(data))
logger.debug("Response: %s" % r.text)
return None
# returns resultId or None
def post_result(self, runId, resultType, resultData):
logger.debug("post_result: %s", runId)
data = {
'run': runId,
'resultInfo': {
'type': resultType,
'data': resultData
}
}
r = requests.post("%s/results" % self.baseUrl, headers=self.HEADERS, data=jd(data))
if r.status_code == 200:
return json.loads(r.text)['resultId']
else:
logger.warning("Unable to post result: %s" % str(data))
logger.debug("Response: %s" % r.text)
return None