Skip to content

Instantly share code, notes, and snippets.

@yaniv-aknin
Created February 26, 2013 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaniv-aknin/5037735 to your computer and use it in GitHub Desktop.
Save yaniv-aknin/5037735 to your computer and use it in GitHub Desktop.
compileror: a file compilation service
#!/usr/bin/env python
import os
from subprocess import Popen
from tempfile import mkdtemp
from httplib import BAD_REQUEST
from flask import Flask, request, send_from_directory
from werkzeug.utils import secure_filename
app = Flask(__name__)
@app.route('/', methods=['POST'])
def upload_file():
temp_dir = mkdtemp()
for field, uploaded in request.files.iteritems():
filename = secure_filename(uploaded.filename)
uploaded.save(os.path.join(temp_dir, filename))
assert os.path.exists(os.path.join(temp_dir, 'main.c')), 'no main.c'
process = Popen(['gcc', 'main.c', '-o', 'executable'], cwd=temp_dir)
if process.wait() != 0:
return 'ERROR', BAD_REQUEST
return send_from_directory(temp_dir, 'executable', as_attachment=True, attachment_filename='executable')
if __name__ == '__main__':
app.run(debug='DEBUG' in os.environ, port=int(os.environ.get('PORT', 5000)), host='0.0.0.0')
web: ./compileror $PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment