<commit_before>""" A set of functions to interact with code execution in an isolated docker
sandbox."""
from docker import Client
import os
import tempfile
import json 
import sys


def query_images():
    """ Return a list of images that are used by this machine to execute
    aribitary code. We return the language names. """
    cli = Client(base_url='unix://var/run/docker.sock', version='auto')
    images = cli.images()

    # only interested in the tag names
    tags = [image['RepoTags'][0] for image in images]
    tags = [tag for tag in tags if tag.startswith('quicktry-')]

    return tags


def execute(workdir, data, stdin):
    # create the client to the docker service
    cli = Client(base_url='unix://var/run/docker.sock', version='auto')

    #creating mapping for languages
    data = {"python27": 
                {"command": "python /mnt/data/input.py", 
                 "image": "python27"},
            "python34": 
                {"command": "python /mnt/data/input.py", 
                 "image": "python34"}
                 }
    json_string = json.dumps(data)
    resp= json.loads(json_string)

    # generate the temporary path for the worker
    with tempfile.TemporaryDirectory(dir=workdir) as dirpath:
        # create the input file
        with open(os.path.join(dirpath, 'input.py'), 'w') as f:
            f.write(data)

        # define the docker container. mount a temporary directory for the
        # input file
        host_config = cli.create_host_config(
                binds=['{}/:/mnt/data'.format(dirpath)])

        # TODO: change the image and command appropriately for the type of
        # input file that we recieve
        # TODO: handle stdin
        container = cli.create_container(
                volumes=['/mnt/data'],
                image= 'quicktry-' + resp[language][image] + ':latest'     #'quicktry-python27:latest',
                command= resp[language][command]         #'python /mnt/data/input.py',
                host_config=host_config )

        # run the script and read stdout
        # TODO: error handling
        c_id = container.get('Id')
        cli.start(container=c_id)
        cli.wait(container=c_id)

        output = cli.logs(container=c_id, stdout=True)

        return output
<commit_msg>added json mapping for language<commit_after>""" A set of functions to interact with code execution in an isolated docker
sandbox."""
from docker import Client
import os
import tempfile
import json 
import sys


def query_images():
    """ Return a list of images that are used by this machine to execute
    aribitary code. We return the language names. """
    cli = Client(base_url='unix://var/run/docker.sock', version='auto')
    images = cli.images()

    # only interested in the tag names
    tags = [image['RepoTags'][0] for image in images]
    tags = [tag for tag in tags if tag.startswith('quicktry-')]

    return tags


def execute(workdir, data, stdin, language):
    # create the client to the docker service
    cli = Client(base_url='unix://var/run/docker.sock', version='auto')

    #creating mapping for languages
    data = {"python27": 
                {"command": "python /mnt/data/input.py", 
                 "image": "python27"},
            "python34": 
                {"command": "python /mnt/data/input.py", 
                 "image": "python34"}
                 }
    json_string = json.dumps(data)
    resp= json.loads(json_string)

    # generate the temporary path for the worker
    with tempfile.TemporaryDirectory(dir=workdir) as dirpath:
        # create the input file
        with open(os.path.join(dirpath, 'input.py'), 'w') as f:
            f.write(data)

        # define the docker container. mount a temporary directory for the
        # input file
        host_config = cli.create_host_config(
                binds=['{}/:/mnt/data'.format(dirpath)])

        # TODO: change the image and command appropriately for the type of
        # input file that we recieve
        # TODO: handle stdin
        container = cli.create_container(
                volumes=['/mnt/data'],
                image= 'quicktry-' + resp[language][image] + ':latest'     #'quicktry-python27:latest',
                command= resp[language][command]         #'python /mnt/data/input.py',
                host_config=host_config )

        # run the script and read stdout
        # TODO: error handling
        c_id = container.get('Id')
        cli.start(container=c_id)
        cli.wait(container=c_id)

        output = cli.logs(container=c_id, stdout=True)

        return output

{'fraction_non_alphanumeric': 0.11089324618736383, 'fraction_numerical': 0.004793028322440087, 'mean_word_length': 2.3364825581395348, 'pattern_counts': {'":': 12, '<': 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': '3669268', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1284, 'n_words': 401}