<commit_before>"""The main xonsh script."""
import sys
import os
import argparse
import builtins
import shell
import tempfile
import shlex
from functools import wraps

BASE_DIR = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(BASE_DIR, "xonsh"))
from xonsh.shell import Shell
from xonsh.parser import Parser
from xonsh.tools import TERM_COLORS

ROOT = None


def chdir(d):
    os.chdir(d)
    if hasattr(builtins, "__xonsh_env__"):
        builtins.__xonsh_env__['PWD'] = os.getcwd()


# Decorator functions
# for making sure the current directory is a submission directory
def current(*arg, **kwarg):
    error = True
    if arg and not callable(arg[0]):
        error = False
    elif kwarg:
        error = kwarg["error"]

    def decorator(func):
        @wraps(func)
        def validate(*args, **kwargs):
            if not os.path.isfile(os.path.join(os.getcwd(), "submission.yaml")):
                if error:
                    print("Not in submission directory, exiting...")
                return False
            return func(*args, **kwargs)
        return validate
    if arg and callable(arg[0]):
        return decorator(arg[0])
    return decorator


# A cool helper function to make a tuple of arguments
def ar(*args, **kwargs):
    return args, kwargs


# Runs argparse on the arguments passed before running the function
def arguments(*gargs, **gkwargs):
    def decorator(func):
        if "prog" not in gargs[0][1]:
            gargs[0][1]["prog"] = func.__name__
        parser = argparse.ArgumentParser(*gargs[0][0], **gargs[0][1])
        for i in range(1, len(gargs)):
            parser.add_argument(*gargs[i][0], **gargs[i][1])

        @wraps(func)
        def args(*args, **kwargs):
            try:
                opts = parser.parse_args(args[0])
                args = args + (opts, parser)
                return func(*args, **kwargs)
            except SystemExit:
                return None
        args.__doc__ = parser.format_help()
        return args
    return decorator


@arguments(
    ar(description='List submissions'),
    ar('type', nargs="?", default="queue", help='which submissions to list'),
    ar('-t', '--team', help='filter by team'),
    ar('-p', '--problem', help='filter by problem')
)
def submissions(arg, opts, parser, stdin=None):
    return shell.submissions(opts.type, opts.team, opts.problem)


@arguments(
    ar(description='Checkout submission'),
    ar('id', nargs="?", default="next", help='which submissions to checkout')
)
def checkout(arg, opts, parser, stdin=None):
    return chdir(shell.checkout(opts.id, cwd=ROOT))


@current
@arguments(
    ar(description='Compile current submission')
)
def compile_sub(arg, opts, parser, stdin=None):
    shell.compile()


@current
@arguments(
    ar(description='Execute current submission'),
    ar('-d', '--detail', action="store_true", help="Print detailed output of the submission")
)
def execute(arg, opts, parser, stdin=None):
    val = shell.execute(os.getcwd())
    if opts.detail:
        print("Return code %d." % val[0])
        if val[1].strip():
            print("stdout:\n%s" % val[1].strip())
        if val[2].strip():
            print("stderr:\n%s" % val[2].strip())
    else:
        if val[1].strip():
            sys.stdout.write(val[1])
        if val[2].strip():
            sys.stderr.write(val[2])


def run_test(test, detail=False, diff=True, diff_cmd="sdiff"):
    path = os.path.join(os.getcwd(), "tests", test)
    if not os.path.isfile(path):
        sys.stderr.write("Test %s does not exist, exiting.\n" % test)
        return

    data = None
    with open(path, 'r', encoding='utf-8') as f:
        data = f.read()

    val = shell.execute(os.getcwd(), test=True, data=data)
    if diff and diff_cmd is not None:
        print("Exit status: %d" % val[0])
        if val[2].strip():
            print("stderr:\n%s" % val[2].strip())
        out = path[:-3] + ".out"
        valdiff = shell.run(shlex.split(diff_cmd) + [out, "-"], stdin=val[1])

        if valdiff[0] == 1:
            print("diff: Output mismatch")
            print(valdiff[1].strip())
            return False
        elif valdiff[0] == 0:
            print("diff: Output matches")
            return True
        else:
            print("diff returned non-zero exit status! %d" % valdiff[0])
            if valdiff[1].strip():
                print("stdout:\n%s" % valdiff[1].strip())
            if valdiff[2].strip():
                print("stderr:\n%s" % valdiff[2].strip())<commit_msg>Updated sdiff options<commit_after>"""The main xonsh script."""
import sys
import os
import argparse
import builtins
import shell
import tempfile
import shlex
from functools import wraps

BASE_DIR = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(BASE_DIR, "xonsh"))
from xonsh.shell import Shell
from xonsh.parser import Parser
from xonsh.tools import TERM_COLORS

ROOT = None


def chdir(d):
    os.chdir(d)
    if hasattr(builtins, "__xonsh_env__"):
        builtins.__xonsh_env__['PWD'] = os.getcwd()


# Decorator functions
# for making sure the current directory is a submission directory
def current(*arg, **kwarg):
    error = True
    if arg and not callable(arg[0]):
        error = False
    elif kwarg:
        error = kwarg["error"]

    def decorator(func):
        @wraps(func)
        def validate(*args, **kwargs):
            if not os.path.isfile(os.path.join(os.getcwd(), "submission.yaml")):
                if error:
                    print("Not in submission directory, exiting...")
                return False
            return func(*args, **kwargs)
        return validate
    if arg and callable(arg[0]):
        return decorator(arg[0])
    return decorator


# A cool helper function to make a tuple of arguments
def ar(*args, **kwargs):
    return args, kwargs


# Runs argparse on the arguments passed before running the function
def arguments(*gargs, **gkwargs):
    def decorator(func):
        if "prog" not in gargs[0][1]:
            gargs[0][1]["prog"] = func.__name__
        parser = argparse.ArgumentParser(*gargs[0][0], **gargs[0][1])
        for i in range(1, len(gargs)):
            parser.add_argument(*gargs[i][0], **gargs[i][1])

        @wraps(func)
        def args(*args, **kwargs):
            try:
                opts = parser.parse_args(args[0])
                args = args + (opts, parser)
                return func(*args, **kwargs)
            except SystemExit:
                return None
        args.__doc__ = parser.format_help()
        return args
    return decorator


@arguments(
    ar(description='List submissions'),
    ar('type', nargs="?", default="queue", help='which submissions to list'),
    ar('-t', '--team', help='filter by team'),
    ar('-p', '--problem', help='filter by problem')
)
def submissions(arg, opts, parser, stdin=None):
    return shell.submissions(opts.type, opts.team, opts.problem)


@arguments(
    ar(description='Checkout submission'),
    ar('id', nargs="?", default="next", help='which submissions to checkout')
)
def checkout(arg, opts, parser, stdin=None):
    return chdir(shell.checkout(opts.id, cwd=ROOT))


@current
@arguments(
    ar(description='Compile current submission')
)
def compile_sub(arg, opts, parser, stdin=None):
    shell.compile()


@current
@arguments(
    ar(description='Execute current submission'),
    ar('-d', '--detail', action="store_true", help="Print detailed output of the submission")
)
def execute(arg, opts, parser, stdin=None):
    val = shell.execute(os.getcwd())
    if opts.detail:
        print("Return code %d." % val[0])
        if val[1].strip():
            print("stdout:\n%s" % val[1].strip())
        if val[2].strip():
            print("stderr:\n%s" % val[2].strip())
    else:
        if val[1].strip():
            sys.stdout.write(val[1])
        if val[2].strip():
            sys.stderr.write(val[2])


def run_test(test, detail=False, diff=True, diff_cmd="sdiff --ignore-all-space"):
    path = os.path.join(os.getcwd(), "tests", test)
    if not os.path.isfile(path):
        sys.stderr.write("Test %s does not exist, exiting.\n" % test)
        return

    data = None
    with open(path, 'r', encoding='utf-8') as f:
        data = f.read()

    val = shell.execute(os.getcwd(), test=True, data=data)
    if diff and diff_cmd is not None:
        print("Exit status: %d" % val[0])
        if val[2].strip():
            print("stderr:\n%s" % val[2].strip())
        out = path[:-3] + ".out"
        valdiff = shell.run(shlex.split(diff_cmd) + [out, "-"], stdin=val[1])

        if valdiff[0] == 1:
            print("diff: Output mismatch")
            print(valdiff[1].strip())
            return False
        elif valdiff[0] == 0:
            print("diff: Output matches")
            return True
        else:
            print("diff returned non-zero exit status! %d" % valdiff[0])
            if valdiff[1].strip():
                print("stdout:\n%s" % valdiff[1].strip())
            if valdiff[2].strip():
                print("stderr:\n%s" % valdiff[2].strip())
{'fraction_non_alphanumeric': 0.15040246995258574, 'fraction_numerical': 0.009041790715624656, 'mean_word_length': 2.5878164556962027, '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': '4892722', 'n_tokens_mistral': 2900, 'n_tokens_neox': 2819, 'n_words': 742}