-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdocuments.py
388 lines (321 loc) · 8.73 KB
/
documents.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from flask.ext.mongokit import Document
from mongokit import ObjectId, IS, OR, Collection
from bson.errors import InvalidId
import time
import logging
class Enum(dict):
__getattr__ = dict.get
def __init__(self, *l):
for e in l:
self[e.upper()] = e
def values_both_types(self):
''' return the values as str and unicode to
be able to check aganinst both variants
'''
results = []
for val in self.values():
results.append(str(val))
results.append(unicode(val))
return results
logger = logging.getLogger('Backend.'+__name__)
DATABASE="AABackend"
class BackendDocument(Document):
__database__ = DATABASE
# returns a copy of self!
def clean_doc(self):
cp = self.copy()
if '_id' in self:
cp = self.copy()
cp['_id'] = str(self['_id'])
return cp
#TODO handle everything here by inspectiong the structure dict!
@classmethod
def rebuild_doc_dict(cls, db, docDict):
# print "rebuild_doc_dict!"
if not docDict:
return {}
if '_id' in docDict:
idVal = docDict['_id']
# print "cls: " + str(cls) + " id: " + str(idVal)
if isinstance(idVal, basestring):
idVal = ObjectId(idVal)
if idVal:
docDict['_id'] = idVal
return docDict
# add str as authorized type
Document.authorized_types.append(str)
use_dot_notation = True
use_autorefs = True
###
### add find_and_modify support for documents
# this will be available via mongokit in feature releases
# this method hast to be added to the collection
def collection_find_and_modify(self, *args, **kwargs):
obj_class = kwargs.pop('wrap', None)
doc = super(Collection, self).find_and_modify(*args, **kwargs)
if obj_class:
return self.collection[obj_class.__name__](doc)
# the document method
def document_find_and_modify(self, *args, **kwargs):
return self.collection.find_and_modify(wrap=self._obj_class, *args, **kwargs)
if not hasattr(Document, 'find_and_modify'):
Collection.find_and_modify = collection_find_and_modify
Document.find_and_modify = document_find_and_modify
###
class Worker(BackendDocument):
__collection__ = 'workers'
structure = {
'name': basestring
}
# An Apple store-account
class Account(BackendDocument):
__collection__ = 'accounts'
structure = {
'uniqueIdentifier': basestring,
'appleId': basestring,
'password': basestring,
'storeCountry': basestring
}
indexes = [{
'fields':['uniqueIdentifier'],
'unique':True,
},{
'fields':['appleId'],
'unique':True,
}]
# A device (iPhone, ...)
class Device(BackendDocument):
__collection__ = 'devices'
structure = {
'udid': basestring,
'deviceInfo': dict,
'accounts': [Account]
}
required_fields = ['udid', 'accounts', 'deviceInfo']
indexes = [{
'fields':['udid'],
'unique':True,
}]
def clean_doc(self):
cp = super(Device, self).clean_doc()
accList = []
if 'accounts' in self and self.accounts:
for acc in self.accounts:
accList.append(acc.uniqueIdentifier)
cp['accounts'] = accList
return cp
@classmethod
def rebuild_doc_dict(cls, db, docDict):
docDict = super(Device, cls).rebuild_doc_dict(db, docDict)
if 'accounts' in docDict:
accList = []
accIdList = docDict['accounts']
for accId in accIdList:
acc = db.Account.find_one({'uniqueIdentifier':accId})
accList.append(acc)
docDict['accounts'] = accList
return docDict
# A job will be executed by a worker
class Job(BackendDocument):
STATE = Enum(u'undefined', u'pending', u'running', u'finished', u'failed')
TYPE = Enum(u'run_app', u'install_app')
use_autorefs = True
__collection__ = 'jobs'
structure = {
'type': IS(*TYPE.values_both_types()),
'state': IS(*STATE.values_both_types()),
'jobInfo': dict,
#TODO accountId, bundleId, storeCountry, appType{AppStoreApp,CydiaApp}, executionStrategy{DefaultExecution,OpenCloseExecution,RandomExecution,SmartExecution}
'worker': Worker,
'device': Device,
'date_added': float
}
required_fields = ['type', 'state', 'jobInfo']
default_values = {
'date_added': time.time,
'state': STATE.UNDEFINED
}
indexes = [{
'fields':['type', 'state'],
}]
def clean_doc(self):
cp = super(Job, self).clean_doc()
if 'worker' in self and self.worker:
cp['worker'] = str(self.worker['_id'])
if 'device' in self and self.device:
cp['device'] = str(self.device['udid'])
return cp
@classmethod
def rebuild_doc_dict(cls, db, docDict):
docDict = super(Job, cls).rebuild_doc_dict(db, docDict)
if 'worker' in docDict:
workerId = docDict['worker']
try:
workerId = ObjectId(workerId)
except InvalidId:
return docDict
worker = db.Worker.find_one({'_id':workerId})
docDict['worker'] = worker
if 'device' in docDict:
deviceId = docDict['device']
device = db.Device.find_one({'udid':deviceId})
if device:
docDict['device'] = device
else:
logger.debug('given string %s is not a device udid' % deviceId)
try:
deviceId = ObjectId(deviceId)
except InvalidId:
logger.debug('given string %s is not a deviceId' % deviceId)
device = db.Device.find_one({'_id':deviceId})
if device:
docDict['device'] = device
return docDict
def can_run_on_device(self, device):
if self.type == Job.TYPE.RUN_APP:
jobInfo = self.jobInfo
if 'accountId' in jobInfo:
for acc in device.accounts:
if jobInfo['accountId'] == acc.uniqueIdentifier:
return True
return False
if 'storeCountry' in jobInfo:
country = jobInfo['storeCountry']
for acc in device.accounts:
if country == acc.storeCountry:
return True
return False
return True
# A app is a concrete app (user account, version, bundleID) under test
class App(BackendDocument):
__collection__ = 'apps'
type_field = 'type'
structure = {
'type': basestring,
'name': basestring,
'bundleId': basestring,
'version': basestring,
'date_added': float
}
gridfs = {
'files': ['ipa']
}
required_fields = ['bundleId', 'version', 'type', 'name']
default_values = {
'date_added': time.time
}
indexes = [{
'fields':['bundleId', 'version'],
'unique':True,
},{
'fields':['name']
}]
# this will fix validation errors for superclass fields
use_schemaless = True
class AppStoreApp(App):
use_schemaless = True
structure = {
'trackId': OR(int, basestring),
'account': Account,
'price': OR(float, basestring),
}
required_fields = ['trackId', 'account']
indexes = [{
'fields':['bundleId', 'version'],
'unique':True,
},{
'fields':['name']
}]
def clean_doc(self):
cp = super(AppStoreApp, self).clean_doc()
if 'account' in self and self.account:
cp['account'] = str(self.account['uniqueIdentifier'])
return cp
@classmethod
def rebuild_doc_dict(cls, db, docDict):
docDict = super(AppStoreApp, cls).rebuild_doc_dict(db, docDict)
if 'account' in docDict:
acc = db.Account.find_one({
'uniqueIdentifier': str(docDict['account'])
})
docDict['account'] = acc
return docDict
class CydiaApp(App):
structure = {
}
# A run is a concrete app execution
class Run(BackendDocument):
STATE = Enum(u'undefined', u'pending', u'running', u'finished')
use_autorefs = True
__collection__ = 'runs'
structure = {
'app': App,
'state': IS(*STATE.values_both_types()),
'executionStrategy': basestring,
'date_added': float
}
required_fields = ['app']
default_values = {
'date_added': time.time
}
indexes = [{
'fields':['state'],
}]
def clean_doc(self):
cp = super(Run, self).clean_doc()
if 'app' in self and self.app:
cp['app'] = str(self.app['_id'])
return cp
@classmethod
def rebuild_doc_dict(cls, db, docDict):
docDict = super(Run, cls).rebuild_doc_dict(db, docDict)
if 'app' in docDict:
appId = docDict['app']
try:
appId = ObjectId(appId)
except InvalidId:
return docDict
app = db.AppStoreApp.find_one({'_id':appId})
if not app:
app = db.CydiaApp.find_one({'_id':appId})
docDict['app'] = app
return docDict
# A result needs a corresponding run, multiple results per run are possible
class Result(BackendDocument):
TYPE = Enum(u'app_archive', u'string', u'criteria', u'stacktrace', u'tcpdump', u'screenshot', u'coverage', u'tracking_libs', u'http_requests')
__collection__ = 'results'
structure = {
'run': Run,
'resultInfo': {
'type': basestring,
'data': None
},
'date_added': float
}
gridfs = {
'files': ['apparchive']
}
required_fields = ['run']
default_values = {
'date_added': time.time
}
indexes = [{
'fields':['resultInfo.type'],
}]
def clean_doc(self):
cp = super(Result, self).clean_doc()
if 'run' in self and self.run:
cp['run'] = str(self.run['_id'])
return cp
@classmethod
def rebuild_doc_dict(cls, db, docDict):
docDict = super(Result, cls).rebuild_doc_dict(db, docDict)
if 'run' in docDict:
runId = docDict['run']
try:
runId = ObjectId(runId)
except InvalidId:
return docDict
run = db.Run.find_one({'_id':runId})
docDict['run'] = run
return docDict