# NOTE: pass -d to this to print debugging info when the server crashes.
from flask import Flask, render_template, url_for, request
from subprocess import Popen, PIPE, check_call
import sys, os, string

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    url_for('static', filename='logo.ico')
    if request.method == 'POST':
        code = request.form['code']
        input = request.form['input'].replace('\r\n', '\n')
        print('Got code:', code, 'input:', input)
        print('Running O code...')
        p = Popen(['o-ide', '-e', code], stdout=PIPE, stderr=PIPE, stdin=PIPE, universal_newlines=True)
        output, error = p.communicate(input)
        print('Output:', output, 'error:', error)
        if p.returncode:
            return render_template('error.html', code=code, input=input, error=error)
        else:
            return render_template('code.html', code=code, input=input, output=output, stack=error or '[]')
    else:
        return render_template('primary.html')

@app.route('/link/')
@app.route('/link/<link>')
def link(link='code="Error in linking code"o&input='):
    url_for('static', filename='logo.ico')
    print('Link:', link)
    return render_template('link.html', link=link)

print("__name__:" + __name__)
if __name__ == 'ide':
    print('Compiling O...')
    check_call(['gcc', 'o.c', '-DIDE', '-o', 'o-ide', '-lm'])
    print('Starting server...')
    app.run(debug='-d' in sys.argv[1:])
@@ -194,16 +194,22 @@
 , string
+, glob
 %0A%0Aapp = 
@@ -225,16 +225,95 @@
 ame__)%0A%0A
+def compileO():%0A    check_call(%5B'gcc', 'o.c', '-DIDE', '-o', 'o-ide', '-lm'%5D)%0A%0A
 @app.rou
@@ -434,16 +434,166 @@
 'POST':%0A
+        #Check if C was compiled%0A        if len(glob.glob(%22o-ide*%22)) %3C 1%0A            print(%22Compiling O...%22)%0A            compileO()%0A        #Run code%0A
         
@@ -910,24 +910,47 @@
 cate(input)%0A
+        #Output to IDE%0A
         prin
@@ -1505,23 +1505,19 @@
 k)%0A%0A
-print(%22
+if 
 __name__
 :%22 +
@@ -1516,42 +1516,21 @@
 me__
-:%22 + __name__)%0Aif __name__ == 'ide
+ == '__main__
 ':%0A 
@@ -1565,63 +1565,16 @@
    c
-heck_call(%5B'gcc', 'o.c', '-DIDE', '-o', 'o-ide', '-lm'%5D
+ompileO(
 )%0A  

Work around for Gunicorn