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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pymongo
from bson.son import SON
from bson import Binary
import sys
import inspect
import kpov_random_helpers
import settings
import glob
import os
def task_check(results, params):
data = urllib.urlencode({
'results': json.dumps(results),
'params': json.dumps(params)
})
req = urllib2.Request('{task_url}/{task_name}/results.json'.format(task_url=task_url, task_name=task_name), data)
response = urllib2.urlopen(req)
response_dict = json.loads(response.read())
hints = response_dict.get('hints', [])
hints = [u'status: ' + response_dict.get('status', '')] + hints
return response_dict.get('result', 'No result'), u"\n".join(hints)
uploading_task_check_source = inspect.getsource(task_check)
def gen_params(user_id, meta):
return dict()
dummy_gen_params_source = inspect.getsource(gen_params)
if __name__ == '__main__':
if len(sys.argv) < 2:
print u"Usage: {0} <task_dir> [task_name]".format(sys.argv[0])
exit(1)
dirname = sys.argv[1]
fname = os.path.join(dirname, 'task.py')
try:
task_id = sys.argv[2]
except:
task_id = os.path.basename(os.path.normpath(dirname))
db = pymongo.MongoClient(settings.DB_HOST).kpov
try:
db.authenticate(settings.USERNAME, settings.PASSWORD)
except Exception, e:
print "Not authenticated:"+str(e)
#no auth or auth config?
source = open(fname).read()
code = compile(source, fname, 'exec')
# the following line creates:
# task, task_check, gen_params, prepare_disks, computers, params_meta.
exec(code)
public_meta = dict()
for k, v in params_meta.iteritems():
if v.get('public', False):
public_meta[k] = v
task_source = "\n\n".join([
inspect.getsource(task),
uploading_task_check_source,
"params_meta = {}".format(public_meta),
dummy_gen_params_source])
task_check_source = inspect.getsource(task_check)
gen_params_source = inspect.getsource(gen_params)
prepare_disks_source = inspect.getsource(prepare_disks)
x = params_meta.keys() # check for existence
db.computers_meta.remove({'task_id': task_id})
auto_networks = set([None])
for k, v in computers.iteritems():
for n in v.get('networks_interfaces', []):
auto_networks.add(n.get('network', None))
db.computers_meta.update({'task_id': task_id, 'name': k}, {'$set': v}, upsert=True)
auto_networks.remove(None)
db.networks.remove({'task_id': task_id})
try:
net_list = networks.iteritems()
except:
net_list = [(k, {'public': False}) for k in auto_networks]
for k, v in net_list:
db.networks.update({'task_id': task_id, 'name': k}, {'$set': v}, upsert=True)
db.task_checkers.update({'task_id': task_id}, {'$set': {'source': task_check_source}}, upsert=True)
db.tasks.update({'task_id': task_id},{'$set': {'source': task_source}}, upsert=True)
db.prepare_disks.update({'task_id': task_id}, {'$set': {'source': prepare_disks_source}}, upsert=True)
db.gen_params.update({'task_id': task_id}, {'$set': {'source': gen_params_source}}, upsert=True)
db.task_params_meta.update({'task_id': task_id}, {'$set': {'params': params_meta}}, upsert=True)
#instructions = dict([(k, v.encode('utf8')) for k, v in instructions.iteritems()])
#print instructions
db.task_instructions.update({'task_id': task_id}, {'$set': instructions
}, upsert=True)
for howto_dir in glob.glob(os.path.join(dirname, 'howtos/*')):
howto_lang = os.path.basename(os.path.normpath(howto_dir))
if howto_lang not in {'images'}:
with open(os.path.join(howto_dir, 'index.html')) as f:
db.howtos.update({'task_id': task_id, 'lang': howto_lang},
{'$set': {'text': f.read()}}, upsert=True)
else:
for img in glob.glob(os.path.join(howto_dir, '*')):
fname = os.path.basename(img)
with open(img) as f:
db.howto_images.update({'task_id': task_id, 'fname': fname},
{'$set': {'data': Binary(f.read())}}, upsert=True)
|