summaryrefslogtreecommitdiff
path: root/website/gsv_flask.py
blob: ece083283ce2dae167214ccf16737857c72a881c (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
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)