<commit_before>#!/usr/local/bin/python3

import filecmp, json, os, sys
from diff_adt import DiffConfig, DiffResult
from time import localtime, strftime
from subprocess import call

CURRENT_TIMESTAMP = strftime("%Y-%m-%d-%H%M", localtime())
DEBUG_MODE = False


def main():
    print('Getting config and preparing run ...', end=' ')
    config = get_config()
    print('done!')

    print('Initiating grading', *config.labs, '...', end='\n\n')

    # Detect initial run or directory structure corruption and run setup
    if not (os.path.isdir(config.csv_path)
            and os.path.isdir(config.results_dir)
            and os.path.isdir(config.submissions_dir)):
        run_init_setup(config)

    write_lab_list_for_MATLAB(config)
    if not setup_solution_files(config):
        print('\n\nUnable to set up reference solutions. Exiting.')
        exit(1)

    print('Running MATLAB script ...', end='\n\n')
    print('\n\nMATLAB run ' + ('finished!' if generate_MATLAB_output() else '\n\nFAILED!'), end='\n\n')

    print('Comparing results and writing output ...', end=' ')
    result = DiffResult()

    for lab in config.labs:
        diff_lab_outputs(result, lab[:-2], config)

    output_result_to_csv(result, config)
    print('ALL DONE!', end='\n\n')


def run_init_setup(config):
    print('Looks like this is the first time you are running this script.\n'
          'Let me set up some directories ...', end='\n\n')
    for p in [config.csv_path, config.results_dir, config.submissions_dir]:
        if p is config.submissions_dir:
            mkdir(config.submissions_dir)
            for lab in config.labs:
                mkdir(config.submissions_dir + lab[:-2])
        else:
            mkdir(p)
    print('\nAll set up! Now, copy student submission folders into ./submissions/labXX/'
          '\nOnce copying is done, please re-run:', *sys.argv)
    exit(0)


def mkdir(directory):
    print('   mkdir', directory)
    if not os.path.isdir(directory):
        os.mkdir(directory)


def write_lab_list_for_MATLAB(config):
    # Write list of labs to .dat file for MATLAB to read which items to execute
    lab_list_dat = open(config.submissions_dir + 'lab_list.dat', 'w')
    for name in config.labs:
        lab_list_dat.write(name + '\n')
    lab_list_dat.close()


def setup_solution_files(config):
    new_solutions_success = False
    default_solution_success = False

    if check_solution_source(config):
        print('Solution source for all labs detected.\n',
              'Firing up MATLAB to generate new solutions ...', end='\n\n')
        sys.stdout.flush()
        new_solutions_success = generate_new_solutions(config)
        print('Solution generation', 'successful' if new_solutions_success else 'failed :(')

    if not new_solutions_success:
        print('Copying default solutions over instead ... ', end='\n   ')
        sys.stdout.flush()
        default_solution_success = copy_default_solutions(config)
        print('\nCopy complete.' if default_solution_success
              else 'Copy failed! Please check permissions.')

    return new_solutions_success or default_solution_success


def check_solution_source(config):
    result = True
    for lab in config.labs:
        result &= os.path.isfile(config.solutions_dir + 'source/' + lab)
    return result


def generate_new_solutions(config):
    return not call(['matlab', '-nodesktop', '-nosplash', '-nodisplay', '-r',
                     "try, run('./generate_solution'), catch exc, getReport(exc), end, exit"])


def copy_default_solutions(config):
    result = True
    default_dir = config.solutions_dir + 'default/'
    for file_name in os.listdir(default_dir):
        if '.txt' in file_name:
            result &= not call(['cp', default_dir + file_name, config.solutions_dir])<commit_msg>Some output format cleanup ...<commit_after>#!/usr/local/bin/python3

import filecmp, json, os, sys
from diff_adt import DiffConfig, DiffResult
from time import localtime, strftime
from subprocess import call

CURRENT_TIMESTAMP = strftime("%Y-%m-%d-%H%M", localtime())
DEBUG_MODE = False


def main():
    print('Getting config and preparing run ...', end=' ')
    config = get_config()
    print('done!')

    print('Initiating grading', *config.labs, '...', end='\n\n')

    # Detect initial run or directory structure corruption and run setup
    if not (os.path.isdir(config.csv_path)
            and os.path.isdir(config.results_dir)
            and os.path.isdir(config.submissions_dir)):
        run_init_setup(config)

    write_lab_list_for_MATLAB(config)
    if not setup_solution_files(config):
        print('\n\nUnable to set up reference solutions. Exiting.')
        exit(1)

    print('   Running MATLAB script to generate student outputs ...', end='\n\n')
    print('\n\nMATLAB run ' + ('finished!' if generate_MATLAB_output() else '\n\nFAILED!'), end='\n\n')

    print('Comparing results and writing output ...', end=' ')
    result = DiffResult()

    for lab in config.labs:
        diff_lab_outputs(result, lab[:-2], config)

    output_result_to_csv(result, config)
    print('ALL DONE!', end='\n\n')


def run_init_setup(config):
    print('Looks like this is the first time you are running this script.\n'
          'Let me set up some directories ...', end='\n\n')
    for p in [config.csv_path, config.results_dir, config.submissions_dir]:
        if p is config.submissions_dir:
            mkdir(config.submissions_dir)
            for lab in config.labs:
                mkdir(config.submissions_dir + lab[:-2])
        else:
            mkdir(p)
    print('\nAll set up! Now, copy student submission folders into ./submissions/labXX/'
          '\nOnce copying is done, please re-run:', *sys.argv)
    exit(0)


def mkdir(directory):
    print('   mkdir', directory)
    if not os.path.isdir(directory):
        os.mkdir(directory)


def write_lab_list_for_MATLAB(config):
    # Write list of labs to .dat file for MATLAB to read which items to execute
    lab_list_dat = open(config.submissions_dir + 'lab_list.dat', 'w')
    for name in config.labs:
        lab_list_dat.write(name + '\n')
    lab_list_dat.close()


def setup_solution_files(config):
    new_solutions_success = False
    default_solution_success = False

    if check_solution_source(config):
        print('Solution source for all labs detected.\n',
              '  Firing up MATLAB to generate new solutions ...', end='\n\n')
        sys.stdout.flush()
        new_solutions_success = generate_new_solutions(config)
        print('Solution generation', 'successful!' if new_solutions_success else 'failed :(', end='\n\n')

    if not new_solutions_success:
        print('Copying default solutions over instead ... ', end='\n   ')
        sys.stdout.flush()
        default_solution_success = copy_default_solutions(config)
        print('\nCopy complete!' if default_solution_success
              else 'Copy failed! Please check permissions.')

    return new_solutions_success or default_solution_success


def check_solution_source(config):
    result = True
    for lab in config.labs:
        result &= os.path.isfile(config.solutions_dir + 'source/' + lab)
    return result


def generate_new_solutions(config):
    return not call(['matlab', '-nodesktop', '-nosplash', '-nodisplay', '-r',
                     "try, run('./generate_solution'), catch exc, getReport(exc), end, exit"])


def copy_default_solutions(config):
    result = True
    default_dir = config.solutions_dir + 'default/'
    for file_name in os.listdir(default_dir):
        if '.txt' in file_name:
            result &= not call(['cp', default_dir + file_name, config.solutions_dir])
{'fraction_non_alphanumeric': 0.120943568356575, 'fraction_numerical': 0.0013032712107389549, 'mean_word_length': 3.0538827258320125, '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': '1096986', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2304, 'n_words': 621}