<commit_before>"""File Adapter Template File

IMPORTANT: NOT A FUNCTIONAL ADAPTER. FUNCTIONS MUST BE IMPLEMENTED

Notes:
    - Each of the functions defined below must return a json serializable
      object, json_response, or valid HttpResponse object
    - A json_response creates an HttpResponse object given parameters:
        - content: string with the contents of the response 
        - status: string with the status of the response 
        - status_code: HTTP status code 
        - error: string with the error message if there is one 
"""
from django.http import StreamingHttpResponse
from common.response import json_response
import logging
logger = logging.getLogger("newt." + __name__)
from common.shell import run_command
from common import gridutil
import tempfile
import mimetypes

def download_path(request, machine_name, path):
    """Returns a StreamingHttpResponse with the file

    Keyword arguments:
    machine_name -- name of the machine
    path -- path to file
    """
    src = gridutil.get_grid_path(machine_name, path)
    env = gridutil.get_cred_env(request.user)
    dest = "/tmp/newt_"+request.user.username+"/"
    logger.debug("File download requested: %s (%s)" % (path, src))
    (output, error, retcode) = run_command(gridutil.GLOBUS_CONF['LOCATION'] + "bin/globus-url-copy %s %s" % (src, dest), env=env)
    if retcode != 0:
        logger.warning("Unable to download file: %s" % src)
        return json_response(content=output, status="ERROR", status_code=500, error=error)
    filename = path.rsplit("/")[-1]
    f = open(dest+"filename", "r")
    mimetype = mimetypes.guess_type(f.name)
    if mimetype is None:
        mimetype = "application/octet-stream"
    return StreamingHttpResponse(f, content_type=mimetype)

def put_file(request, machine, path):
    """Writes the uploaded file to path and returns the path

    Keyword arguments:
    request -- HttpRequest containing the data
    machine_name -- name of the machine
    path -- path to file
    """
    # Get data from request body
    data = request.read()
    # Write data to temporary location
    # TODO: Get temporary path from settings.py 
    tmp_file = tempfile.NamedTemporaryFile(prefix="newt_")
    tmp_file.write(data)
    tmp_file.file.flush()

    src = "file:///%s" % tmp_file.name
    env = gridutil.get_cred_env(request.user)
    dest = gridutil.get_grid_path(machine, path)

    logger.debug("Putting file to location: %s" % dest)

    (output, error, retcode) = run_command(gridutil.GLOBUS_CONF['LOCATION'] + "bin/globus-url-copy %s %s" % (src, dest), env=env)
    if retcode != 0:
        return json_response(content=output, status="ERROR", status_code=500, error=error)
    tmp_file.close()
    return {'location': path}


def get_dir(request, machine_name, path):
    """Returns a directory listing of path (as an array)

    Keyword arguments:
    machine_name -- name of the machine
    path -- path to file
    """
    try:
        env = gridutil.get_cred_env(request.user)
        path = gridutil.get_grid_path(machine_name, path)
        output, error, ret_code = run_command(gridutil.GLOBUS_CONF['LOCATION'] + "bin/uberftp -ls %s" % path, 
                                              env=env)<commit_msg>Fixed globus file bugs<commit_after>"""File Adapter Template File

IMPORTANT: NOT A FUNCTIONAL ADAPTER. FUNCTIONS MUST BE IMPLEMENTED

Notes:
    - Each of the functions defined below must return a json serializable
      object, json_response, or valid HttpResponse object
    - A json_response creates an HttpResponse object given parameters:
        - content: string with the contents of the response 
        - status: string with the status of the response 
        - status_code: HTTP status code 
        - error: string with the error message if there is one 
"""
from django.http import StreamingHttpResponse
from common.response import json_response
import logging
logger = logging.getLogger("newt." + __name__)
from common.shell import run_command
from common import gridutil
import tempfile
import mimetypes

def download_path(request, machine_name, path):
    """Returns a StreamingHttpResponse with the file

    Keyword arguments:
    machine_name -- name of the machine
    path -- path to file
    """
    src = gridutil.get_grid_path(machine_name, path)
    env = gridutil.get_cred_env(request.user)
    dest = "/tmp/newt_"+request.user.username+"/"
    logger.debug("File download requested: %s (%s)" % (path, src))
    (output, error, retcode) = run_command(gridutil.GLOBUS_CONF['LOCATION'] + "bin/globus-url-copy %s %s" % (src, dest), env=env)
    if retcode != 0:
        logger.warning("Unable to download file: %s" % src)
        return json_response(content=output, status="ERROR", status_code=500, error=error)
    filename = path.rsplit("/")[-1]
    f = open(dest+"filename", "r")
    mimetype = mimetypes.guess_type(f.name)
    if mimetype is None:
        mimetype = "application/octet-stream"
    return StreamingHttpResponse(f, content_type=mimetype)

def put_file(request, machine, path):
    """Writes the uploaded file to path and returns the path

    Keyword arguments:
    request -- HttpRequest containing the data
    machine_name -- name of the machine
    path -- path to file
    """
    # Get data from request body
    data = request.read()
    # Write data to temporary location
    # TODO: Get temporary path from settings.py 
    tmp_file = tempfile.NamedTemporaryFile(prefix="newt_")
    tmp_file.write(data)
    tmp_file.file.flush()

    src = "file:///%s" % tmp_file.name
    env = gridutil.get_cred_env(request.user)
    dest = gridutil.get_grid_path(machine, path)

    logger.debug("Putting file to location: %s" % dest)

    (output, error, retcode) = run_command(gridutil.GLOBUS_CONF['LOCATION'] + "bin/globus-url-copy %s %s" % (src, dest), env=env)
    if retcode != 0:
        return json_response(content=output, status="ERROR", status_code=500, error=error)
    tmp_file.close()
    return {'location': path}


def get_dir(request, machine_name, path):
    """Returns a directory listing of path (as an array)

    Keyword arguments:
    machine_name -- name of the machine
    path -- path to file
    """
    try:
        env = gridutil.get_cred_env(request.user)
        path = gridutil.get_grid_path(machine_name, path)
        output, error, retcode = run_command(gridutil.GLOBUS_CONF['LOCATION'] + "bin/uberftp -ls %s" % path, 
                                              env=env)
{'fraction_non_alphanumeric': 0.10323574730354391, 'fraction_numerical': 0.002773497688751926, 'mean_word_length': 3.2816622691292876, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5307724', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1910, 'n_words': 594}