forked from nawalgupta/PythonBuddy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
103 lines (81 loc) · 2.99 KB
/
app.py
File metadata and controls
103 lines (81 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#Created by Ethan Chiu 10/25/16
from flask import Flask, render_template, request, jsonify
from pylint import lint
from astroid import MANAGER
from pylint.reporters.text import TextReporter
from subprocess import Popen, PIPE, STDOUT
from RestrictedPython import compile_restricted_exec
from RestrictedPython.PrintCollector import PrintCollector
from RestrictedPython.Guards import safe_builtins
app = Flask(__name__)
app.debug = True
@app.route('/')
def hello_world():
return render_template("index.html")
@app.route('/check_code')
def check_code():
#Get textarea text from AJAX call
text = request.args.get('text')
#Open temp file which will be parsed
'''
f = open("temp.py","r+")
f.seek()
f.write(text)
f.truncate()
f.close()
'''
with open("temp.py", "r") as in_file:
buf = in_file.readlines()
with open("temp.py", "w") as out_file:
for line in range(7):
out_file.write(buf[line])
out_file.write("\n")
for line in text:
out_file.write(line)
#Writable Object that will be used as a TextReporter
class WritableObject(object):
def __init__(self):
self.content = []
def write(self, st):
self.content.append(st)
def read(self):
return self.content
#Remember that you can configure with a seperate file for more specific limitations => --rcfile=/path/to/config.file .
#See http://stackoverflow.com/a/10138997/4698963
#Add "--disable=R,C" to ARGs to print only errors & warnings
ARGS = ["-r","n", "--disable=R,C","--msg-template={path}:{line}: [{msg_id}({symbol}), {obj}] {msg}"]
pylint_output = WritableObject()
#Run Pylint, textreporter will redirect to writable object
lint.Run(["temp.py"]+ARGS, reporter=TextReporter(pylint_output), exit=False)
pylint_list = pylint_output.content
#Clear Cache. VERY IMPORTANT! This will make sure that there's no funky issues. See: http://stackoverflow.com/questions/2028268/invoking-pylint-programmatically#comment5393474_4803466
MANAGER.astroid_cache.clear()
#Return json object, which is the pylint_output seperated by each newline
return jsonify(pylint_list)
'''
@app.route('/get_help')
def help_code():
'''
@app.route('/run_code')
def run_code():
'''
OLD INSECURE CODE => MORE FREEDOM ON THE USER SIDE THOUGH
print "run_test"
cmd = 'python temp.py'
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
'''
with open('temp.py', 'r') as myfile:
text=myfile.read()
code = compile_restricted_exec(text)
#exec(code)
#Get code object as stated: https://late.am/post/2012/03/26/exploring-python-code-objects.html
if code[0] is None:
print(code)
return jsonify(code[1])
else:
print(code[0].co_consts)
return jsonify(code[0].co_consts[6])
return jsonify("Code Cannot be Run")
if __name__ == "__main__":
app.run()