summaryrefslogtreecommitdiff
path: root/add_task.py
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@fri.uni-lj.si>2019-02-24 21:05:27 +0100
committerTimotej Lazar <timotej.lazar@fri.uni-lj.si>2019-02-24 21:05:27 +0100
commit8081a5520a441b43a8a7a73f3a90c7aacfaa8e10 (patch)
treec7f49bd33ed19d53afc0ee9df8b2c82c200c5910 /add_task.py
parent9963b74f777edf985540eac71b1ca095f88b8bca (diff)
Move everything one level up
Diffstat (limited to 'add_task.py')
-rwxr-xr-xadd_task.py124
1 files changed, 124 insertions, 0 deletions
diff --git a/add_task.py b/add_task.py
new file mode 100755
index 0000000..7bb2f3e
--- /dev/null
+++ b/add_task.py
@@ -0,0 +1,124 @@
+#!/usr/bin/env python3
+
+import glob
+import inspect
+import os
+import settings
+import sys
+import urllib
+
+import kpov_util
+import pymongo
+from bson import Binary
+
+def task_check(results, params):
+ data = {
+ 'results': json.dumps(results),
+ 'params': json.dumps({k: v for k, v in params.items() if k != 'token'}),
+ }
+ # should be an argument to task_check, but probably better not modify the signature…
+ if 'token' in params:
+ data['token'] = params['token']
+
+ response = urllib.request.urlopen(
+ '{task_url}/{task_name}/results.json'.format(task_url=task_url, task_name=task_name),
+ data=urllib.parse.urlencode(data).encode())
+ response_dict = json.loads(response.read().decode())
+ hints = response_dict.get('hints', [])
+ hints = ['status: ' + response_dict.get('status', '')] + hints
+ return response_dict.get('result', 'No result'), 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('network_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 = d['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, 'rb') as f:
+ db.howto_images.update({
+ 'task_id': task_id,
+ 'class_id': class_id,
+ 'fname': fname,
+ },
+ {'$set': {'data': Binary(f.read())}}, upsert=True)