summaryrefslogtreecommitdiff
path: root/kpov_judge/add_task.py
blob: ec65fd0f4777342dc6c61997a1681dfe5d3cd46c (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pymongo
from bson.son import SON
import sys
import inspect
import kpov_random_helpers
import settings
import guestfs
import glob

def uploading_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)
    return response.read()

uploading_task_check_source = inspect.get_source(uploading_task_check)

def dummy_gen_params(user_id, meta):
    return dict()

dummy_gen_params_source = inspect.get_source(dummy_gen_params)


if __name__ == '__main__':
    if len(sys.argv) != 3:
        print "Usage: {0} task_name task_source.py"
        print "The task_source should contain two functions - task(param1, param2, ...) and task_check(res, params)"
    task_id = sys.argv[1]
    fname = sys.argv[2]
    db = pymongo.Connection(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:
        if v.get('public', False):
            public_meta[k] = v
    task_source = inspect.getsource(task) + "\n\n" +
        uploading_task_check_source + "\n\n" +
        "params_meta = " + str(public_meta) + "\n\n" + 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)