Пример #1
0
def delcases():
    print 'Deleting all test cases for problem ' + globals.getprob()
    for file in os.listdir(globals.probdir):
        if not file.endswith('.dat'):
            os.remove(os.path.join(globals.probdir, file))
            print 'Deleted ' + file
    print 'Problem {}: all test cases deleted'.format(globals.getprob())
Пример #2
0
def delcases ():
    print 'Deleting all test cases for problem ' + globals.getprob()
    for file in os.listdir(globals.probdir):
        if not file.endswith('.dat'):
            os.remove(os.path.join(globals.probdir, file))
            print 'Deleted ' + file
    print 'Problem {}: all test cases deleted'.format(globals.getprob())
Пример #3
0
def testgen ():
    numcases = globals.getnumcases()
    execorig = globals.getsetting('executable')
    execloc = os.path.join(globals.sandboxdir, execorig.name)
    execfile = open(execloc, 'wb')
    execfile.write(execorig.read())
    execfile.close()
    os.chmod(execloc, 0755)
    print 'Automatically generating {} test cases for problem {} with {}'.format(numcases, globals.getprob(), execorig.name)
    curtest = 0
    for i in range(numcases):
        while True:
            inloc = os.path.join(globals.probdir, 'pcugen{:03d}.in'.format(curtest))
            ansloc = os.path.join(globals.probdir, 'pcugen{:03d}.ans'.format(curtest))
            if not os.path.exists(inloc):
                break
            curtest += 1
        print 'Generating case {:03d}'.format(curtest)
        execresult = helper.runproc('./{} {} {}'.format(execorig.name, curtest, random.randint(0, 2147483647)), globals.sandboxdir, timelim=10.0)
        if execresult[0] != 0:
            print 'Generator executable error'
            break
        infile = open(inloc, 'wb')
        ansfile = open(ansloc, 'wb')
        infile.write(execresult[2])
        ansfile.write(execresult[3])
        infile.close()
        ansfile.close()
        curtest += 1
    log.addmsg('Generated {} tests for problem {} with {}'.format(numcases, globals.getprob(), execorig.name))
Пример #4
0
def make():
    print 'Creating {} source file for problem {}'.format(
        settings.langexts[globals.getext()], globals.getprob())
    template = helper.read(
        os.path.join(globals.templatedir,
                     globals.getmode() + '.' + globals.getext()))
    if template is None:
        print '{}.{} template file not found (add it to templates folder)'.format(
            globals.getmode(), globals.getext())
        return False
    helper.write(os.path.join(globals.working, globals.getsrc()),
                 pattern.convert(template))
    print 'Source file created: ' + globals.getsrc()
    log.addmsg('Creating {} source file for problem {}'.format(
        settings.langexts[globals.getext()], globals.getprob()))
    return True
Пример #5
0
def main():
    # Read command line options

    globals.probsettings = parse.parseopts()

    # Initialize problem

    helper.makedir(globals.sandboxdir)
    if globals.getprob() is not None:
        globals.probdir = os.path.join(globals.tmpdir, 'probs',
                                       globals.getprob())
        globals.probsettingsfile = os.path.join(globals.probdir,
                                                'probsettings.dat')
        helper.makedir(globals.probdir)

    # Merge dictionary with command line options

    if globals.getprob() is not None:
        opts = helper.readdict(globals.probsettingsfile)  # Previous settings
        for key in opts:
            if key not in globals.probsettings or globals.probsettings[
                    key] is None:
                globals.probsettings[key] = opts[key]

    # Assert that basic values are present in settings

    if globals.getext() is None:
        globals.probsettings['langext'] = settings.defaultlangext

    if globals.getmode() is None:
        globals.probsettings['mode'] = settings.defaultmode

    # Execute command

    print settings.bigdivider
    globals.commands[globals.probsettings['command']]()

    # Write new settings dict to file

    if globals.getprob() is not None:
        helper.writedict(globals.probsettingsfile, globals.probsettings)

    # Clear sandbox directory

    helper.deldir(globals.sandboxdir)

    print settings.bigdivider
Пример #6
0
def setfile (ext):
    file = globals.probsettings['filename']
    type = exttotype[ext]
    testid = globals.gettestid()
    print 'Setting test case {} {} for problem {}'.format(testid, type, globals.getprob())
    input = ''
    if file is None:
        print 'Enter {} below (Ctrl-{} when finished)'.format(type, 'Z' if helper.iswindows() else 'D')
        print settings.smalldivider
        input = sys.stdin.read()
        print settings.smalldivider
    else:
        print 'Retrieving from ' + file.name
        input = file.read()
    helper.write(os.path.join(globals.probdir, testid + ext), input)
    print 'Set {} successfully'.format(exttotype[ext])
    log.addmsg('Set test case {} {} for problem {}'.format(testid, type, globals.getprob()))
    return True
Пример #7
0
def main ():
    # Read command line options

    globals.probsettings = parse.parseopts()

    # Initialize problem

    helper.makedir(globals.sandboxdir)
    if globals.getprob() is not None:
        globals.probdir = os.path.join(globals.tmpdir, 'probs', globals.getprob())
        globals.probsettingsfile = os.path.join(globals.probdir, 'probsettings.dat')
        helper.makedir(globals.probdir)

    # Merge dictionary with command line options

    if globals.getprob() is not None:
        opts = helper.readdict(globals.probsettingsfile) # Previous settings
        for key in opts:
            if key not in globals.probsettings or globals.probsettings[key] is None:
                globals.probsettings[key] = opts[key]

    # Assert that basic values are present in settings

    if globals.getext() is None:
        globals.probsettings['langext'] = settings.defaultlangext

    if globals.getmode() is None:
        globals.probsettings['mode'] = settings.defaultmode

    # Execute command

    print settings.bigdivider
    globals.commands[globals.probsettings['command']]()

    # Write new settings dict to file

    if globals.getprob() is not None:
        helper.writedict(globals.probsettingsfile, globals.probsettings)

    # Clear sandbox directory

    helper.deldir(globals.sandboxdir)

    print settings.bigdivider
Пример #8
0
def setfile(ext):
    file = globals.probsettings['filename']
    type = exttotype[ext]
    testid = globals.gettestid()
    print 'Setting test case {} {} for problem {}'.format(
        testid, type, globals.getprob())
    input = ''
    if file is None:
        print 'Enter {} below (Ctrl-{} when finished)'.format(
            type, 'Z' if helper.iswindows() else 'D')
        print settings.smalldivider
        input = sys.stdin.read()
        print settings.smalldivider
    else:
        print 'Retrieving from ' + file.name
        input = file.read()
    helper.write(os.path.join(globals.probdir, testid + ext), input)
    print 'Set {} successfully'.format(exttotype[ext])
    log.addmsg('Set test case {} {} for problem {}'.format(
        testid, type, globals.getprob()))
    return True
Пример #9
0
def getfile (ext):
    file = parse.parseopts()['filename']
    type = exttotype[ext]
    testid = globals.gettestid()
    print 'Getting test case {} {} for problem {}'.format(testid, type, globals.getprob())
    output = helper.read(os.path.join(globals.probdir, testid + ext))
    if output is None:
        print 'Did not find {} file'.format(type)
        return False
    if file is None:
        print 'File found: {} below'.format(type)
        print settings.smalldivider
        print output + ('' if settings.eof is None else settings.eof)
    else:
        try:
            file.write(output)
            file.close()
            print 'Wrote {} to {}'.format(type, file.name)
        except IOError:
            print 'Unable to write to ' + file.name
            return False
    return True
Пример #10
0
def getfile(ext):
    file = parse.parseopts()['filename']
    type = exttotype[ext]
    testid = globals.gettestid()
    print 'Getting test case {} {} for problem {}'.format(
        testid, type, globals.getprob())
    output = helper.read(os.path.join(globals.probdir, testid + ext))
    if output is None:
        print 'Did not find {} file'.format(type)
        return False
    if file is None:
        print 'File found: {} below'.format(type)
        print settings.smalldivider
        print output + ('' if settings.eof is None else settings.eof)
    else:
        try:
            file.write(output)
            file.close()
            print 'Wrote {} to {}'.format(type, file.name)
        except IOError:
            print 'Unable to write to ' + file.name
            return False
    return True
Пример #11
0
def run():
    if not globals.commands['comp']():
        return False
    print settings.meddivider
    stdio = globals.getmode() == 'stdio'
    print 'Executing problem {} in {} I/O mode'.format(globals.getprob(),
                                                       globals.getmode())
    files = os.listdir(globals.probdir)
    cases = []
    for file in files:
        if file.endswith('.in'):
            cases.append(file[:-3])
    print '{} test inputs found'.format(len(cases))
    sandboxin = os.path.join(globals.sandboxdir,
                             pattern.convert(settings.inputfile))
    sandboxout = os.path.join(globals.sandboxdir,
                              pattern.convert(settings.outputfile))
    execcommand = pattern.convert(
        settings.execcommands[settings.langexts[globals.getext()]])
    langlim = settings.timeconstraints[settings.langexts[globals.getext()]]
    outcomes = collections.OrderedDict([('Correct', 0), ('Runtime Error', 0),
                                        ('Time Limit Exceeded', 0),
                                        ('Presentation Error', 0),
                                        ('Wrong Answer', 0),
                                        ('No Output Produced', 0),
                                        ('No Answer File', 0)])
    print 'Time limit per input: {:.2f} seconds'.format(langlim)
    for case in cases:
        print settings.meddivider
        print 'Test input ' + case
        helper.copy(os.path.join(globals.probdir, case + '.in'), sandboxin)
        input = helper.read(sandboxin)
        execresult = helper.runproc(execcommand,
                                    globals.sandboxdir,
                                    stdin=(input if stdio else None),
                                    timelim=langlim)
        output = execresult[2] if stdio else helper.read(sandboxout)
        ans = helper.read(os.path.join(globals.probdir, case + '.ans'))
        wronganswer = False
        if execresult[0] is None:  # Time limit exceeded
            print 'Execution time limit exceeded'
            outcomes['Time Limit Exceeded'] += 1
        else:
            print 'Execution time: {:.2f} seconds'.format(execresult[1])
            if execresult[0] != 0:
                print 'Runtime error: exit code ' + repr(execresult[0])
                outcomes['Runtime Error'] += 1
            elif output is None or len(output) == 0:
                print 'Program did not produce output'
                outcomes['No Output Produced'] += 1
            elif ans is None:
                print 'No answer file provided'
                outcomes['No Answer File'] += 1
            elif ans == output or \
                 (''.join(ans.split()) == ''.join(output.split())
                  and settings.acceptformatting):
                print 'Correct'
                outcomes['Correct'] += 1
            elif ''.join(ans.split()) == ''.join(output.split()):
                print 'Presentation Error (extra/missing whitespace)'
                outcomes['Presentation Error'] += 1
                wronganswer = True
            else:
                print 'Wrong Answer'
                outcomes['Wrong Answer'] += 1
                wronganswer = True
        if settings.printinput:
            helper.printdesc(input, 'Input', settings.smalldivider)
        if settings.printoutput:
            helper.printdesc(output, 'Output', settings.smalldivider)
        if wronganswer and settings.printexpected:
            helper.printdesc(ans, 'Expected Output', settings.smalldivider)
        if settings.printstdout and not stdio:
            helper.printdesc(execresult[2], 'Stdout', settings.smalldivider)
        if settings.printstderr:
            helper.printdesc(execresult[3], 'Stderr', settings.smalldivider)
        helper.write(os.path.join(globals.probdir, case + '.out'), output)
        if not stdio:
            helper.write(os.path.join(globals.probdir, case + '.stdout'),
                         execresult[2])
        helper.write(os.path.join(globals.probdir, case + '.stderr'),
                     execresult[3])
    if settings.printsummary:
        print settings.meddivider
        print 'Results Summary'
        for outcome in outcomes:
            print '{:2d} | {:<20}'.format(outcomes[outcome], outcome)
        print 'Total Score: {:.1f}'.format(
            (100.0 * outcomes['Correct']) / max(1, len(cases)))
    log.addmsg('Executed problem {} in {} I/O mode with score {:.1f}'.format(
        globals.getprob(), globals.getmode(),
        (100.0 * outcomes['Correct']) / max(1, len(cases))))
    return True
Пример #12
0
import re

import globals
import settings

# Formatting Rules

rules = [
    lambda str: re.sub("\$INFILE", settings.inputfile, str),
    lambda str: re.sub("\$OUTFILE", settings.outputfile, str),
    lambda str: re.sub("\$SRCFILE", globals.getsrc(), str),
    lambda str: re.sub("\$PROB", globals.getprob(), str),
    lambda str: re.sub("\$EXT", globals.getext(), str),
    lambda str: re.sub("\$USER", settings.username, str),
]

# String Formatter


def convert(str):
    for rule in rules:
        str = rule(str)
    return str
Пример #13
0
def run ():
    if not globals.commands['comp']():
        return False
    print settings.meddivider
    stdio = globals.getmode() == 'stdio'
    print 'Executing problem {} in {} I/O mode'.format(globals.getprob(), globals.getmode())
    files = os.listdir(globals.probdir)
    cases = []
    for file in files:
        if file.endswith('.in'):
            cases.append(file[:-3])
    print '{} test inputs found'.format(len(cases))
    sandboxin = os.path.join(globals.sandboxdir, pattern.convert(settings.inputfile))
    sandboxout = os.path.join(globals.sandboxdir, pattern.convert(settings.outputfile))
    execcommand = pattern.convert(settings.execcommands[settings.langexts[globals.getext()]])
    langlim = settings.timeconstraints[settings.langexts[globals.getext()]]
    outcomes = collections.OrderedDict([
        ('Correct', 0),
        ('Runtime Error', 0),
        ('Time Limit Exceeded', 0),
        ('Presentation Error', 0),
        ('Wrong Answer', 0),
        ('No Output Produced', 0),
        ('No Answer File', 0)
    ])
    print 'Time limit per input: {:.2f} seconds'.format(langlim)
    for case in cases:
        print settings.meddivider
        print 'Test input ' + case
        helper.copy(os.path.join(globals.probdir, case + '.in'), sandboxin)
        input = helper.read(sandboxin)
        execresult = helper.runproc(execcommand, globals.sandboxdir, stdin=(input if stdio else None), timelim=langlim)
        output = execresult[2] if stdio else helper.read(sandboxout)
        ans = helper.read(os.path.join(globals.probdir, case + '.ans'))
        wronganswer = False
        if execresult[0] is None: # Time limit exceeded
            print 'Execution time limit exceeded'
            outcomes['Time Limit Exceeded'] += 1
        else:
            print 'Execution time: {:.2f} seconds'.format(execresult[1])
            if execresult[0] != 0:
                print 'Runtime error: exit code ' + repr(execresult[0])
                outcomes['Runtime Error'] += 1
            elif output is None or len(output) == 0:
                print 'Program did not produce output'
                outcomes['No Output Produced'] += 1
            elif ans is None:
                print 'No answer file provided'
                outcomes['No Answer File'] += 1
            elif ans == output:
                print 'Correct'
                outcomes['Correct'] += 1
            elif ''.join(ans.split()) == ''.join(output.split()):
                print 'Presentation Error (extra/missing whitespace)'
                outcomes['Presentation Error'] += 1
                wronganswer = True
            else:
                print 'Wrong Answer'
                outcomes['Wrong Answer'] += 1
                wronganswer = True
        if settings.printinput:
            helper.printdesc(input, 'Input', settings.smalldivider)
        if settings.printoutput:
            helper.printdesc(output, 'Output', settings.smalldivider)
        if wronganswer and settings.printexpected:
            helper.printdesc(ans, 'Expected Output', settings.smalldivider)
        if settings.printstdout and not stdio:
            helper.printdesc(execresult[2], 'Stdout', settings.smalldivider)
        if settings.printstderr:
            helper.printdesc(execresult[3], 'Stderr', settings.smalldivider)
        helper.write(os.path.join(globals.probdir, case + '.out'), output)
        if not stdio:
            helper.write(os.path.join(globals.probdir, case + '.stdout'), execresult[2])
        helper.write(os.path.join(globals.probdir, case + '.stderr'), execresult[3])
    if settings.printsummary:
        print settings.meddivider
        print 'Results Summary'
        for outcome in outcomes:
            print '{:2d} | {:<20}'.format(outcomes[outcome], outcome)
        print 'Total Score: {:.1f}'.format((100.0 * outcomes['Correct']) / len(cases)); 
    return True
Пример #14
0
def make ():
    print 'Creating {} source file for problem {}'.format(settings.langexts[globals.getext()], globals.getprob())
    template = helper.read(os.path.join(globals.templatedir, globals.getmode() + '.' + globals.getext()))
    if template is None:
        print '{}.{} template file not found (add it to templates folder)'.format(globals.getmode(), globals.getext())
        return False
    helper.write(os.path.join(globals.working, globals.getsrc()), pattern.convert(template))
    print 'Source file created: ' + globals.getsrc()
    log.addmsg('Creating {} source file for problem {}'.format(settings.langexts[globals.getext()], globals.getprob()))
    return True
Пример #15
0
import re

import globals
import settings

# Formatting Rules

rules = [
    lambda str: re.sub('\$INFILE', settings.inputfile, str),
    lambda str: re.sub('\$OUTFILE', settings.outputfile, str),
    lambda str: re.sub('\$SRCFILE', globals.getsrc(), str),
    lambda str: re.sub('\$PROB', globals.getprob(), str),
    lambda str: re.sub('\$EXT', globals.getext(), str),
    lambda str: re.sub('\$USER', settings.username, str)
]

# String Formatter


def convert(str):
    for rule in rules:
        str = rule(str)
    return str