Created
August 16, 2014 07:14
-
-
Save chrisparnin/5dc44232162892088ed8 to your computer and use it in GitHub Desktop.
Execute Python Tutor Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import urllib.parse | |
import re | |
import tempfile | |
import os | |
from subprocess import STDOUT,CalledProcessError,check_output | |
PATTERN_START = u".py?user_script=" | |
PATTERN_END = u' HTTP/1.0" 200' | |
def createAndExecuteTempFile(content): | |
fileTemp = tempfile.NamedTemporaryFile(mode='w+t',delete=False) | |
name = fileTemp.name | |
try: | |
fileTemp.write(content) | |
fileTemp.close() | |
# stderr=STDOUT | |
output = check_output(["C:/tools/python/python.exe", name],stderr=STDOUT, timeout=5) | |
return output | |
except CalledProcessError as e: | |
print (e.returncode, e.output) | |
return None | |
finally: | |
os.remove(name) | |
count = 0 | |
fail = 0 | |
timeout = 0 | |
total = 0 | |
if len(sys.argv) == 2: | |
fileName = sys.argv[1]; | |
with open(fileName) as f: | |
content = f.readlines() | |
for line in content: | |
scriptStart = line.index(PATTERN_START) | |
scriptEnd = line.find(PATTERN_END) | |
urlFragment = line[scriptStart:scriptEnd] | |
try: | |
parsed = urllib.parse.urlparse("/"+urlFragment) | |
snippet = urllib.parse.parse_qs(parsed.query)['user_script'][0] | |
#val = exec(snippet) | |
output = createAndExecuteTempFile(snippet) | |
if output == None: | |
fail +=1 | |
else: | |
count += 1 | |
#print (output) | |
except: | |
e = sys.exc_info()[0] | |
print (e) | |
timeout +=1 | |
total += 1 | |
if total % 50 == 0: | |
print ("Success",count, "fail", fail, "timeout",timeout) | |
print ("Final: Success",count, "fail", fail, "timeout",timeout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment