summaryrefslogtreecommitdiff
path: root/kpov_judge/add_task.py
blob: 7839ccef7eb98e45a92de1cf6afec67ae63447e7 (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
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
#!/usr/bin/env python

import pymongo
from bson.son import SON
from bson import Binary
import sys
import inspect
import kpov_util
import settings
import glob
import os

def task_check(results, params):
    data = urllib.parse.urlencode({
        'results': json.dumps(results),
        'params': json.dumps(params)
    })
    req = urllib.request.Request('{task_url}/{task_name}/results.json'.format(task_url=task_url, task_name=task_name), data)
    response = urllib.request.urlopen(req)
    response_dict = json.loads(response.read())
    hints = response_dict.get('hints', [])
    hints = ['status: ' + response_dict.get('status', '')] + hints
    return response_dict.get('result', 'No result'), "\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("Usage: {0} <task_dir> [task_name]".format(sys.argv[0]))
        exit(1)
    dirname = sys.argv[1]
    fname = os.path.join(dirname, 'task.py')
    try:
        class_id, task_id = sys.argv[2].split('/')
    except:
        normpath = os.path.normpath(dirname)
        class_id = os.path.split(os.path.dirname(normpath))[-1]
        task_id = os.path.basename(normpath)
    print((class_id, task_id))

    db = pymongo.MongoClient(settings.DB_URI).get_default_database()

    source = open(fname).read()
    d = {}
    # defines task, task_check, gen_params, prepare_disks, computers, params_meta
    exec(compile(source, fname, 'exec'), globals(), d)

    public_meta = {}
    for k, v in d['params_meta'].items():
        if v.get('public', False):
            public_meta[k] = v
    task_source = "\n\n".join([
        inspect.getsource(d['task']),
        uploading_task_check_source,
        "params_meta = {}".format(public_meta),
        dummy_gen_params_source])
    task_check_source = inspect.getsource(d['task_check'])
    gen_params_source = inspect.getsource(d['gen_params'])
    prepare_disks_source = inspect.getsource(d['prepare_disks'])
    x = list(d['params_meta'].keys()) # check for existence
    db.computers_meta.remove({'task_id': task_id, 'class_id': class_id})
    auto_networks = set([None])
    for k, v in d['computers'].items():
        for n in v.get('networks_interfaces', []):
            auto_networks.add(n.get('network', None))
        db.computers_meta.update({
                'task_id': task_id,
                'class_id': class_id, 
                'name': k
            }, {'$set': v}, upsert=True)
    auto_networks.remove(None)
    db.networks.remove({'task_id': task_id, 'class_id': class_id})
    try:
        net_list = iter(networks.items())
    except:
        net_list = [(k, {'public': False}) for k in auto_networks]
    for k, v in net_list:
        db.networks.update({'task_id': task_id, 'class_id': class_id, 'name': k}, {'$set': v}, upsert=True)
    db.task_checkers.update({
            'task_id': task_id, 'class_id': class_id
        }, {'$set': {'source': task_check_source}}, upsert=True)
    db.tasks.update({
            'task_id': task_id, 'class_id': class_id
        },{'$set': {'source': task_source}}, upsert=True)
    db.prepare_disks.update({
            'task_id': task_id, 'class_id': class_id
        }, {'$set': {'source': prepare_disks_source}}, upsert=True)
    db.gen_params.update({'task_id': task_id, 'class_id': class_id},
        {'$set': {'source': gen_params_source}}, upsert=True)
    db.task_params_meta.update({'task_id': task_id, 'class_id': class_id},
        {'$set': {'params': d['params_meta']}}, upsert=True)
    db.task_instructions.update({'task_id': task_id, 'class_id': class_id}, 
        {'$set': d['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,
                        'class_id': class_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,
                            'class_id': class_id,
                            'fname': fname,
                        },
                        {'$set': {'data': Binary(f.read())}}, upsert=True)