summaryrefslogtreecommitdiff
path: root/website/gsv_flask.py
diff options
context:
space:
mode:
Diffstat (limited to 'website/gsv_flask.py')
-rwxr-xr-xwebsite/gsv_flask.py116
1 files changed, 116 insertions, 0 deletions
diff --git a/website/gsv_flask.py b/website/gsv_flask.py
new file mode 100755
index 0000000..ece0832
--- /dev/null
+++ b/website/gsv_flask.py
@@ -0,0 +1,116 @@
+#!/usr/bin/env python
+
+# Copyright (C) 2014 Assaf Gordon <assafgordon@gmail.com>
+#
+# This file is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this file. If not, see <http://www.gnu.org/licenses/>.
+#
+# Written by Assaf Gordon
+
+from flask import Flask, url_for, request, redirect, jsonify, render_template, make_response, send_file
+from werkzeug import secure_filename
+import string
+import datetime
+import random
+import json
+import requests
+import re
+import sys
+import os
+import csv
+import datetime
+import httplib
+import pprint
+from redis import Redis
+from subprocess import Popen,PIPE
+from rq import Queue
+
+from gsv_worker import do_gnu_eval, sanitize_project_name, \
+ acceptable_source_url, \
+ get_current_active_job_num, \
+ get_current_submitted_job_num, \
+ allocate_new_submitted_job_num, \
+ get_completed_jobs
+
+app = Flask(__name__)
+app.debug = True
+
+redis_connection=Redis()
+q = Queue(connection=redis_connection)
+
+pp = pprint.PrettyPrinter()
+
+results_dir = "/var/lib/gsv-eval/"
+
+@app.route("/")
+def index():
+ submitted_jobs_count = get_current_submitted_job_num()
+ completed_jobs_count = get_current_active_job_num()
+ pending_jobs_count = int(submitted_jobs_count - completed_jobs_count)
+ completed_jobs = get_completed_jobs()
+ print "pending = ", pending_jobs_count
+ print "submitted = ", submitted_jobs_count
+ print "completed = ", completed_jobs_count
+ return render_template("home.html",
+ completed_jobs = completed_jobs,
+ submitted_jobs_count = submitted_jobs_count,
+ pending_jobs_count = pending_jobs_count)
+
+@app.route("/eval", methods=['POST'])
+def eval():
+ if not 'project_name' in request.form:
+ return "Error: missing project name"
+ project_name = request.form['project_name'].strip()
+ project_name = sanitize_project_name(str(project_name))
+
+ if not 'source_url' in request.form:
+ return "Error: missing source URL"
+ source_url = request.form['source_url'].strip()
+ if acceptable_source_url(source_url):
+ return "Error: Source URL (%s) does not match allowed patternas. Only simple URLs are allowed (no CGI parameters)" % (source_url)
+
+ job_id = allocate_new_submitted_job_num()
+
+ job = q.enqueue(do_gnu_eval, job_id, results_dir, project_name, source_url)
+
+ link = url_for('show',jobid=job_id)
+ return redirect(link)
+
+@app.route('/show/<jobid>')
+def show(jobid):
+ try:
+ jobid = int(jobid)
+ except:
+ return "error: invalid ID"
+
+ max_job = get_current_submitted_job_num()
+ if jobid > int(max_job):
+ return "error: no such job"
+
+ processed_jobs = get_current_active_job_num()
+
+ result_file = results_dir + "/" + str(jobid) + ".html"
+ if not os.path.isfile(result_file) or os.path.getsize(result_file)==0:
+ if jobid < processed_jobs:
+ return "job failed, please try to re-run it, or file a bug report"
+
+ return render_template('not_ready.html')
+
+ return send_file(result_file)
+
+@app.route("/detect")
+def detect():
+ return render_template("detect_license.html");
+
+if __name__ == "__main__":
+ app.run(host='0.0.0.0',port=5106)