<commit_before>#!/usr/bin/env python

import sys
import subprocess
import os

SUB_NAMES = ["hello.py", "hello"]

def grade(argv):

    # Extract Paths from Args
    sub_dir = os.path.abspath(argv[0])
    tst_dir = os.path.abspath(argv[1])

    # Check Paths
    if not os.path.exists(sub_dir):
        sys.stderr.write("Could not find submission directory: '{:s}'\n".format(sub_dir))
        return -1
    if not os.path.exists(tst_dir):
        sys.stderr.write("Could not find test directory: '{:s}'\n".format(tst_dir))
        return -1

    # Find submission
    found = False
    for sub_name in SUB_NAMES:
        sub_path = os.path.abspath("{:s}/{:s}".format(sub_dir, sub_name))
        if os.path.exists(sub_path):
            found = True
            break
    if not found:
        sys.stderr.write("Could not find submission: '{:s}'\n".format(SUB_NAMES))
        sys.stderr.write("Does yoru program have a valid name?\n")
        sys.stdout.write("{:2d}\n".format(0))
        return 0

    # Prep Submission
    os.chmod(sub_path, 0775)

    # Grade
    sys.stderr.write("Grading {:s}\n".format(sub_name))
    sys.stderr.write("__________________________\n")

    score = 0
    try:
        p = subprocess.Popen([sub_path],
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        ret = p.returncode
    except Exception as e:
        sys.stderr.write("Error occured running program: {:s}\n".format(str(e)))
        ret = -1
    else:
        sys.stderr.write("{:s} output:\n {:s}\n".format(sub_name, stdout))

    if (ret == 0):

        # Sanitize Output
        output = stdout.rstrip().lstrip()
        output = output.lower()

        # Grade
        if ("hello" in output):
            sys.stderr.write("Found 'hello' in output -> 5 of 5\n".format(output))
            score += 5
        else:
            sys.stderr.write("Could not find 'hello' in output -> 0 of 5\n".format(output))

        if ("world" in output):
            sys.stderr.write("Found 'world' in output -> 5 of 5\n".format(output))
            score += 5
        else:
            sys.stderr.write("Could not find 'world' in output -> 0 of 5\n".format(output))

    else:

        # Submission Returend Error
        sys.stderr.write("{:s} returned error: {:d} +0\n".format(sub_name, ret))
        sys.stderr.write(str(stderr))

    sys.stderr.write("__________________________\n".format(score))
    sys.stderr.write("Score ---------------> {:2d}\n".format(score))

    sys.stdout.write("{:2d}\n".format(score))<commit_msg>Updated grading script to accomodate tmp directory<commit_after>#!/usr/bin/env python

import sys
import subprocess
import os
import shutil

SUB_NAMES = ["hello.py", "hello"]

def grade(argv):

    # Extract Paths from Args
    sub_dir = os.path.abspath(argv[0])
    tst_dir = os.path.abspath(argv[1])
    tmp_dir = os.path.abspath(argv[2])

    # Check Paths
    if not os.path.exists(sub_dir):
        sys.stderr.write("Could not find submission directory: '{:s}'\n".format(sub_dir))
        return -1
    if not os.path.exists(tst_dir):
        sys.stderr.write("Could not find test directory: '{:s}'\n".format(tst_dir))
        return -1

    # Find submission
    found = False
    for sub_name in SUB_NAMES:
        orig_sub_path = os.path.abspath("{:s}/{:s}".format(sub_dir, sub_name))
        if os.path.exists(orig_sub_path):
            found = True
            break
    if not found:
        sys.stderr.write("Could not find submission: '{:s}'\n".format(SUB_NAMES))
        sys.stderr.write("Does your program have a valid name?\n")
        sys.stdout.write("{:2d}\n".format(0))
        return 0

    # Prep Submission
    tmp_sub_path = os.path.abspath("{:s}/{:s}".format(tmp_dir, sub_name))
    shutil.copy(orig_sub_path, tmp_sub_path)
    os.chmod(tmp_sub_path, 0775)

    # Grade
    sys.stderr.write("Grading {:s}\n".format(sub_name))
    sys.stderr.write("__________________________\n")

    score = 0
    try:
        p = subprocess.Popen([tmp_sub_path],
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        ret = p.returncode
    except Exception as e:
        sys.stderr.write("Error occured running program: {:s}\n".format(str(e)))
        ret = -1
    else:
        sys.stderr.write("{:s} output:\n {:s}\n".format(sub_name, stdout))

    if (ret == 0):

        # Sanitize Output
        output = stdout.rstrip().lstrip()
        output = output.lower()

        # Grade
        if ("hello" in output):
            sys.stderr.write("Found 'hello' in output -> 5 of 5\n".format(output))
            score += 5
        else:
            sys.stderr.write("Could not find 'hello' in output -> 0 of 5\n".format(output))

        if ("world" in output):
            sys.stderr.write("Found 'world' in output -> 5 of 5\n".format(output))
            score += 5
        else:
            sys.stderr.write("Could not find 'world' in output -> 0 of 5\n".format(output))

    else:

        # Submission Returned Error
        sys.stderr.write("{:s} returned error: {:d} +0\n".format(sub_name, ret))
        sys.stderr.write(str(stderr))

    sys.stderr.write("__________________________\n".format(score))
    sys.stderr.write("Score ---------------> {:2d}\n".format(score))

    sys.stdout.write("{:2d}\n".format(score))
{'fraction_non_alphanumeric': 0.15394614670380688, 'fraction_numerical': 0.01021355617455896, 'mean_word_length': 2.3474207582349287, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '632161', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1745, 'n_words': 386}