MA0301/exam_template/python/Python.py

27 lines
620 B
Python

import tempfile
import subprocess
import os
import sys
from common import replaceContent
def makeTmpFile(content):
fd, path = tempfile.mkstemp()
while ('_' in path):
fd, path = tempfile.mkstemp()
with os.fdopen(fd, 'w') as tmp:
tmp.write(content)
return path
def grabOutput(path):
return subprocess.check_output(['python', path]).decode(sys.stdout.encoding)
def processFileContent(content):
path = makeTmpFile(content)
output = grabOutput(path)
return replaceContent(
(path, output),
'Python',
lambda temp, cont: temp.replace('%CODEFILE', cont[0]).replace('%OUTPUT', cont[1])
)