#!/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.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_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_HOST).kpov try: db.authenticate(settings.USERNAME, settings.PASSWORD) except Exception as 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.items(): 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 = list(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 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': 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, 'class_id': class_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, '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)