<commit_before>from django.http import JsonResponse, HttpResponse
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
import json
import os
from constants import RUN_SCRIPT_PATH, TEMPLATE_PATH, RUN_CONFIG_DEFAULT_PATH, POLLER_URL
from util.utilities import print_debug, print_message
from models import ModelRun, RunScript
import shutil
import requests


#
# Creates a new model run configuration
# input: user, the user requesting a new run config folder
#        run_name, the name of the new run config
#        run_type, the type of the run being created (model, diagnostic)
#        optional, template, the name of their predefined template
@login_required
def create_run(request):
    print request.body
    data = json.loads(request.body)
    user = str(request.user)
    path = os.path.abspath(os.path.dirname(__file__))
    user_directory = path + RUN_SCRIPT_PATH + user
    template_directory = path + TEMPLATE_PATH
    config_path = path + RUN_CONFIG_DEFAULT_PATH

    if not os.path.exists(user_directory):
        print_message("Creating directory {}".format(user_directory), 'ok')
        os.makedirs(user_directory)

    new_run = data.get('run_name')
    if not new_run:
        print_message('No new run_name specied', 'error')
        return HttpResponse(status=400)

    new_run_dir = os.path.join(user_directory, new_run)
    if os.path.exists(new_run_dir):
        print_message('Run directory already exists', 'error')
        return HttpResponse(status=409)

    try:
        os.makedirs(new_run_dir)
    except Exception as e:
        print_debug(e)
        return HttpResponse(status=500)

    try:
        run = ModelRun(user=request.user)
        run.save()
    except Exception as e:
        print_message("Error saving run {} in database for user {}".format(new_run_dir, request.user), 'error')
        shutil.rmtree(new_run_dir, ignore_errors=True)
        print_debug(e)
        return JsonResponse({'error': 'error saving run in database'})

    run_type = data.get('run_type')
    if not run_type:
        print_message('No run_type specied', 'error')
        return HttpResponse(status=400)

    config_path += run_type + '.json'
    try:
        shutil.copyfile(config_path, new_run_dir + '/config.json')
    except Exception as e:
        print_debug(e)
        print_message("Error saving config file {} for user {}".format(config_path, user), 'error')
        return JsonResponse({'error': 'config not saved'})


    template = data.get('template')
    if not template:
        return JsonResponse({'new_run_dir': new_run_dir})

    if user in template:
        template_search_dirs = [user]
    else:
        template_search_dirs = [user, 'global']

    template = template.split('/')[-1]
    found_template = False
    template_path = False
    template_search_dirs = [ str(template_directory + x) for x in template_search_dirs]
    for directory in template_search_dirs:
        if os.path.exists(directory):
            if template in os.listdir(directory):
                found_template = True
                template_path = directory + '/' + template
        else:
            os.mkdir(directory)

    if found_template:
        try:
            shutil.copyfile(template_path, new_run_dir + '/' + template)
        except Exception as e:<commit_msg>Adds the user defined run_name to the config.json<commit_after>from django.http import JsonResponse, HttpResponse
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
import json
import os
from constants import RUN_SCRIPT_PATH, TEMPLATE_PATH, RUN_CONFIG_DEFAULT_PATH, POLLER_URL
from util.utilities import print_debug, print_message
from models import ModelRun, RunScript
import shutil
import requests


#
# Creates a new model run configuration
# input: user, the user requesting a new run config folder
#        run_name, the name of the new run config
#        run_type, the type of the run being created (model, diagnostic)
#        optional, template, the name of their predefined template
@login_required
def create_run(request):
    print request.body
    data = json.loads(request.body)
    user = str(request.user)
    path = os.path.abspath(os.path.dirname(__file__))
    user_directory = path + RUN_SCRIPT_PATH + user
    template_directory = path + TEMPLATE_PATH
    config_path = path + RUN_CONFIG_DEFAULT_PATH

    if not os.path.exists(user_directory):
        print_message("Creating directory {}".format(user_directory), 'ok')
        os.makedirs(user_directory)

    new_run = data.get('run_name')
    if not new_run:
        print_message('No new run_name specied', 'error')
        return HttpResponse(status=400)

    new_run_dir = os.path.join(user_directory, new_run)
    if os.path.exists(new_run_dir):
        print_message('Run directory already exists', 'error')
        return HttpResponse(status=409)

    try:
        os.makedirs(new_run_dir)
    except Exception as e:
        print_debug(e)
        return HttpResponse(status=500)

    try:
        run = ModelRun(user=request.user)
        run.save()
    except Exception as e:
        print_message("Error saving run {} in database for user {}".format(new_run_dir, request.user), 'error')
        shutil.rmtree(new_run_dir, ignore_errors=True)
        print_debug(e)
        return JsonResponse({'error': 'error saving run in database'})

    run_type = data.get('run_type')
    if not run_type:
        print_message('No run_type specied', 'error')
        return HttpResponse(status=400)

    config_path += run_type + '.json'
    new_config_path = new_run_dir + '/config.json'
    conf = {}
    try:
        with open(config_path, 'r') as config_file:
            conf = json.loads(config_file.read())
            conf['run_name'] = new_run
            config_file.close()
        with open(new_config_path, 'w') as new_config:
            conf = json.dumps(conf)
            new_config.write(conf)
            new_config.close()
    except Exception as e:
        print_debug(e)
        print_message("Error saving config file {} for user {}".format(config_path, user), 'error')
        return JsonResponse({'error': 'config not saved'})


    template = data.get('template')
    if not template:
        return JsonResponse({'new_run_dir': new_run_dir})

    if user in template:
        template_search_dirs = [user]
    else:
        template_search_dirs = [user, 'global']

    template = template.split('/')[-1]
    found_template = False
    template_path = False
    template_search_dirs = [ str(template_directory + x) for x in template_search_dirs]
    for directory in template_search_dirs:
        if os.path.exists(directory):
            if template in os.listdir(directory):
                found_template = True
                template_path = directory + '/' + template
        else:
            os.mkdir(directory)

    if found_template:
        try:
            shutil.copyfile(template_path, new_run_dir + '/' + template)
        except Exception as e:
{'fraction_non_alphanumeric': 0.09216850438224485, 'fraction_numerical': 0.0036754311563471868, 'mean_word_length': 3.0754608294930876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2940583', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2057, 'n_words': 556}