diff --git a/app.py b/app.py index 125981d9..b662f30b 100644 --- a/app.py +++ b/app.py @@ -3,11 +3,14 @@ from pylint import lint from astroid import MANAGER from pylint.reporters.text import TextReporter -from subprocess import Popen, PIPE, STDOUT import fileinput +from tempfile import NamedTemporaryFile +from RestrictedPython import compile_restricted +from RestrictedPython import safe_builtins, utility_builtins, limited_builtins +from RestrictedPython.PrintCollector import PrintCollector app = Flask(__name__) -app.debug = True +app.debug = True @app.route('/') def hello_world(): @@ -19,23 +22,6 @@ 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(13): - 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): @@ -45,7 +31,7 @@ def write(self, 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 . + #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}"] @@ -53,13 +39,15 @@ def read(self): pylint_output = WritableObject() #Run Pylint, textreporter will redirect to writable object - lint.Run(["temp.py"]+ARGS, reporter=TextReporter(pylint_output), exit=False) + with NamedTemporaryFile(mode="w+t") as t: + t.write(text) + t.seek(0) + lint.Run([t.name]+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 + #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) ''' @@ -69,15 +57,29 @@ def help_code(): @app.route('/run_code') def run_code(): - 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() - - return jsonify(output) + #Get textarea text from AJAX call + text = request.args.get('text') + execglobals= {} + execglobals["_getiter_"] = lambda x: x.__iter__() + execglobals["_getitem_"] = lambda x, y: x.__getitem__(y) + execglobals["_print_"] = PrintCollector + #if safe_mode: + execglobals["__builtins__"] = {} + execglobals["__builtins__"].update(safe_builtins) + execglobals["__builtins__"].update(limited_builtins) + execglobals["__builtins__"].update(utility_builtins) + try: + byte_code = compile_restricted(text+"ff__result=printed", + filename='', + mode='exec') + + exec(byte_code, execglobals, None) + except SyntaxError as e: + pass + + return jsonify(execglobals.get("ff__result", "")) + #return text if __name__ == "__main__": app.run() - - diff --git a/requirements.txt b/requirements.txt index ba0953ef..3d43a5ac 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ appdirs==1.4.2 -astroid==1.4.9 +astroid==1.5.3 backports.functools-lru-cache==1.3 click==6.7 configparser==3.5.0 @@ -11,9 +11,10 @@ lazy-object-proxy==1.2.2 MarkupSafe==0.23 mccabe==0.6.1 packaging==16.8 -pylint==1.6.5 +pylint==1.7.4 pyparsing==2.1.10 six==1.10.0 Werkzeug==0.11.15 wrapt==1.10.8 -gunicorn==0.14.2 +gunicorn==19.7.1 +RestrictedPython==4.0b2 diff --git a/static/js/javascript.js b/static/js/javascript.js index 41342c38..ab3b1431 100644 --- a/static/js/javascript.js +++ b/static/js/javascript.js @@ -2,7 +2,7 @@ function getHelp(e){for(var n=[["E0401","Used when pylint has been unable to imp return"No information at the moment"}$(document).ready(function(){function e(e,n){function t(e){var t,i,a,o,r s=[{line_no:null,column_no_start:null,column_no_stop:null,fragment:null,message:null,severity:null}],document.getElementById("errorslist").innerHTML="",$("#errorslist").append("LineSeverityErrorMore Info") for(var h=2;h"+t+''+a+""+i+""+c+"")}console.log("error_list"+s),n(s)}var s=[{line_no:null,column_no_start:null,column_no_stop:null,fragment:null,message:null,severity:null}] $.getJSON("/check_code",{text:e},function(e){return console.log(e),current_text=e,t(current_text),!1})}var n=CodeMirror.fromTextArea(document.getElementById("txt"),{mode:{name:"python",version:2,singleLineStringErrors:!1},lineNumbers:!0,indentUnit:4,matchBrackets:!0,lint:!0,styleActiveLine:!0,gutters:["CodeMirror-lint-markers"],lintWith:{getAnnotations:CodeMirror.remoteValidator,async:!0,check_cb:e}}) diff --git a/temp.py b/temp.py deleted file mode 100644 index 2727d526..00000000 --- a/temp.py +++ /dev/null @@ -1,16 +0,0 @@ -import sys -sys.modules['os']=None -sys.modules['os.path']=None -sys.modules['pprint']=None -sys.modules['builtins']=None -sys.modules['shutil']=None -sys.modules['subprocess']=None -sys.modules['jinja2']=None -sys.modules['subprocess']=None -sys.modules['subprocess']=None -sys.modules['yaml']=None -sys.modules['sys']=None -#STOP - -print "hello world" - \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 068d3a4a..cf864bb1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1 +1,72 @@ - PythonBuddy Fork me on GitHub

Python Linter Online

Live Syntax Checking Using Pylint while Running Python

Examples: 1 2 3 4 5 6 7





 

Pylint Output Info

Line Severity Error More Info

+ + + + + + + + PythonBuddy + + + + + + + + + + + + + + + + + + + + + Fork me on GitHub +
+

Python Linter Online

+ Live Syntax Checking Using Pylint while Running Python +

Examples: + 1 + 2 + 3 + 4 + 5 + 6 + 7 +

+
+
+
+ +
+ +
+
+
+

+      

+
+

Pylint Output Info

+ + + + + + + +
LineSeverityErrorMore Info
+
+
+
+ + + +