Example #1
0
def run(volumn,
        compilerName='g++',
        option='-std=c++0x',
        runName='a.out',
        imageName='cpp',
        memoryLimit=128,
        memorySwapLimit=256,
        stdinName='stdin.txt',
        timeLimit=5,
        logger=None):
    (hostVolumn, containerVolumn) = getVolumnPath(volumn)

    #Run
    command = "-v %s --net none --memory %sm --memory-swap %sm %s sh -c '%s < %s'" % (
        volumn, str(memoryLimit), str(memorySwapLimit), imageName,
        containerVolumn + runName, containerVolumn + stdinName)

    D = dockerContainer.execute(command, timeLimit, logger)

    exitCode = D['exitcode']
    if logger is not None:
        logger.info('Run done. (exit code: %s)' % str(exitCode))

    res = {}
    if exitCode == 0:
        res['state'] = 'success'
        res['stdout'] = D['stdout']
        res['stderr'] = D['stderr']
        res['runningTime'] = D['runningTime']
    elif D['state'] == 'tle':
        res['state'] = 'tle'
        res['stdout'] = ''
        res['stderr'] = 'Running time limit exceeded.'
        res['runningTime'] = ''
        res['containerId'] = D['ContainerId']
    elif exitCode == 137:
        res['state'] = 'error'
        res['stdout'] = ''
        res['stderr'] = 'Memory limit exceeded.'
        res['runningTime'] = ''
    elif 'docker' not in D['stderr']:
        res['state'] = 'error'
        res['stdout'] = D['stdout']
        res['stderr'] = D['stderr']
        res['runningTime'] = ''
        if logger is not None:
            logger.info('Exception while running(may due to user): ' +
                        res['stderr'])
    else:
        res['state'] = 'error'
        res['stdout'] = ''
        res['stderr'] = 'Server error.'
        res['runningTime'] = D['runningTime']
        if logger is not None:
            logger.critical('Error while running: ' + res['stderr'])
        else:
            print 'Error while running: ' + res['stderr']

    return res
Example #2
0
def compile(sourceFile,
            volumn,
            compilerName='g++',
            option='-std=c++0x',
            binaryName='a.out',
            imageName='npclown/gcc:2.0',
            timeLimit=10,
            logger=None):
    (hostVolumn, containerVolumn) = getVolumnPath(volumn)

    if len(sourceFile) < 1:
        return None
    fileNames = containerVolumn + sourceFile[0]
    for source in sourceFile[1:]:
        fileNames = fileNames + ' ' + containerVolumn + source
    if binaryName is not None:
        binaryName = '-o ' + containerVolumn + binaryName
    else:
        binaryName = ''

    command = "-v %s %s sh -c '%s %s %s %s'" % (
        volumn, imageName, compilerName, binaryName, fileNames, option)
    logger.debug(command)
    D = dockerContainer.execute(command, timeLimit, logger)

    exitCode = D['exitcode']
    if logger is not None:
        logger.info('Compile done. (exit code: %s)' % str(exitCode))

    res = {}
    if exitCode == 0:
        res['state'] = 'success'
        res['stdout'] = D['stdout']
        res['stderr'] = D['stderr']
        res['runningTime'] = D['runningTime']
    elif exitCode == 1:
        res['state'] = 'compile error'
        res['stdout'] = D['stdout']
        res['stderr'] = D['stderr']
        res['runningTime'] = D['runningTime']
    elif D['state'] == 'tle':
        res['state'] = 'tle'
        res['stdout'] = ''
        res['stderr'] = 'Compile time limit exceeded.'
        res['runningTime'] = D['runningTime']
    else:
        res['state'] = 'error'
        res['stdout'] = ''
        res['stderr'] = 'Server error.'
        res['runningTime'] = D['runningTime']
        if logger is not None:
            logger.critical('Error while compile: ' + D['stderr'])
        else:
            print 'Error while compile: ' + D['stderr']

    return res
def compile(sourceFile, volumn, compilerName = 'g++', option='-std=c++0x', binaryName='a.out', imageName='baekjoon/onlinejudge-gcc:4.8', timeLimit=5, logger=None):
    (hostVolumn, containerVolumn) = getVolumnPath(volumn)

    if len(sourceFile) < 1:
        return None
    fileNames = containerVolumn + sourceFile[0]
    for source in sourceFile[1:]:
        fileNames = fileNames + ' ' + containerVolumn + source
    if binaryName is not None:
        binaryName = '-o ' + containerVolumn + binaryName
    else:
        binaryName = ''

    command = "-v %s %s sh -c '%s %s %s %s'" % (volumn, imageName, compilerName, option, binaryName, fileNames)
    logger.debug(command)

    D = dockerContainer.execute(command, timeLimit, logger)

    exitCode = D['exitcode']
    if logger is not None:
        logger.info('Compile done. (exit code: %s)' % str(exitCode))

    res = {}
    if exitCode == 0:
        res['state'] = 'success'
        res['stdout'] = D['stdout']
        res['stderr'] = D['stderr']
    elif exitCode == 1:
        res['state'] = 'compile error'
        res['stdout'] = D['stdout']
        res['stderr'] = D['stderr']
    elif D['state'] == 'tle':
        res['state'] = 'tle'
        res['stdout'] = ''
        res['stderr'] = 'Compile time limit exceeded.'
    else:
        res['state'] = 'error'
        res['stdout'] = ''
        res['stderr'] = 'Server error.'
        if logger is not None:
            logger.critical('Error while compile: ' + D['stderr'])
        else:
            print 'Error while compile: ' + D['stderr']

    return res
def run(runName, volumn, option='', intpName='python', imageName='baekjoon/onlinejudge-gcc:4.8', memoryLimit=128, memorySwapLimit=256, stdinName='stdin.txt', timeLimit=5, logger=None):
    (hostVolumn, containerVolumn) = getVolumnPath(volumn)

    #Run
    command = "-v %s --net none --memory %sm --memory-swap %sm %s sh -c '%s %s %s < %s'" % (volumn, str(memoryLimit), str(memorySwapLimit), imageName, intpName, option, runName, containerVolumn+stdinName)
    logger.debug(command)
    D = dockerContainer.execute(command, timeLimit, logger)

    exitCode = D['exitcode']
    if logger is not None:
        logger.info('Run done. (exit code: %s)' % str(exitCode))

    res = {}
    if exitCode == 0:
        res['state'] = 'success'
        res['stdout'] = D['stdout']
        res['stderr'] = D['stderr']
    elif D['state'] == 'tle':
        res['state'] = 'tle'
        res['stdout'] = ''
        res['stderr'] = 'Running time limit exceeded.'
    elif exitCode == 137:
        res['state'] = 'error'
        res['stdout'] = ''
        res['stderr'] = 'Memory limit exceeded.'
    elif 'docker' not in D['stderr']:
        res['state'] = 'error'
        res['stdout'] = D['stdout']
        res['stderr'] = D['stderr']
        if logger is not None:
            logger.info('Exception while running(may due to user): ' + res['stderr'])
    else:
        res['state'] = 'error'
        res['stdout'] = ''
        res['stderr'] = 'Server error.'
        if logger is not None:
            logger.critical('Error while running: ' + res['stderr'])
        else:
            print 'Error while running: ' + res['stderr']

    return res
    dirpath = getFromDict(key='dir', json_data=json_data, default='')
    volumn = dirpath + ":/data"

    #Get stdin string
    try:
        stdin = getFromDict(key='inputs', json_data=json_data, default='')
        with open(dirpath + '/stdin.txt', 'w') as fp:
            fp.write(stdin.encode('utf8'))
    except Exception as e:
        logger.critical('Cannot write stdin file.' + str(e))

    #compile
    command = "-v %s npclown/java:1.0 sh -c 'cd /data; find ./src -name *.java > sources_list.txt; javac -cp . -d class @sources_list.txt'" % (
        volumn)
    logger.debug(command)
    D = dockerContainer.execute(command, timeLimit, logger)
    #print D
    exitCode = D['exitcode']
    if logger is not None:
        logger.info('Compile done. (exit code: %s)' % str(exitCode))

    res = {}
    if exitCode == 0:
        res['state'] = 'success'
        res['stdout'] = D['stdout']
        res['stderr'] = D['stderr']
        res['runningTime'] = D['runningTime']
    elif exitCode == 1:
        res['state'] = 'compile error'
        res['stdout'] = D['stdout']
        res['stderr'] = D['stderr']