forked from KEHANG/rmg_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
158 lines (133 loc) · 4.55 KB
/
server.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from flask import Flask, render_template, request, g, make_response, redirect, url_for
import os
import subprocess
import psycopg2
from werkzeug import secure_filename
ALLOWED_EXTENSIONS = set(['py'])
app = Flask(__name__)
app.config["UPLOAD_FOLDER"] = 'temp'
okCmds = frozenset(["python"])
@app.route('/<cmd>/<script>/')
def runCmd(cmd, script):
if cmd in okCmds:
print cmd, script
o = subprocess.check_output([cmd,script])
return o
else:
return ( cmd + ' is not an ok command.' )
@app.route('/<cmd>/<script>/<input_file>/<id>')
def runCmdWithInput(cmd, script, input_file, id):
if cmd in okCmds and script == "rmg":
script = "scripts/rmg.py"
input_file = "temp/input.py"
o = subprocess.call([cmd, script, input_file])
if o == 0:
# crete file binary
f = open('temp/chemkin/chem.inp', 'rb')
data = psycopg2.Binary(f.read())
# connect to db and update the row with
# right id
conn = get_db()
cur = conn.cursor()
cur.execute(
"""UPDATE job_result SET result=%s WHERE id=%s
RETURNING id;""",
(data, id))
return_value = cur.fetchone()
conn.commit()
cur.close()
return id
else:
return "Need your effort to make it right!!"
else:
return ( cmd + ' is not an ok command.' )
@app.route('/job_result/<id>')
def showResultWithJobId(id):
conn = get_db()
cur = conn.cursor()
cur.execute("""SELECT result FROM job_result WHERE id=%s;""", (id,))
chem_file = cur.fetchone()[0]
cur.close()
response = make_response(str(chem_file))
response.headers["Content-Disposition"] = "attachment; filename=chem.inp"
return response
# chem_args = parse(inputBytes) # Bytes
# chem_ret_val = fullrun(chem_args) # Data
# # chem_out = toString(chem_ret_val) # Bytes
# @app.route('processData','GET')
# def processData():
# renderForm
# @app.route('processData','POST')
# def processData(dataBytes):
# bytes = app.getBytes
# resp = toString( fullrun ( parse (bytes)))
# resp.setHeader('Content-Style','download');
@app.route('/')
def index():
return render_template('index.html')
@app.route('/find_molecule')
def find_molecule():
return render_template('find_molecule.html')
@app.route('/run_rmg_job', methods=['GET', 'POST'])
def run_rmg_job():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
raise Exception("no file received!")
db = get_db()
# insert blank
print "get the db!"
cur = db.cursor()
cur.execute(
"""INSERT INTO job_result
VALUES (%s, %s, %s, null, TRUE)
RETURNING id;""",
("5", request.form["job_name"], request.form["cmd"]))
return_value = cur.fetchone()
db.commit()
cur.close()
return str(return_value[0])
else:
return render_template('run_rmg_job.html')
@app.route('/run_rmg_job_upload', methods=['GET', 'POST'])
def run_rmg_job_upload():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('run_rmg_job', filename=filename))
else:
return "get no file!"
else:
return render_template('run_rmg_job.html')
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = connect_to_database()
return db
def connect_to_database():
with open("settings", "r") as f:
line = f.readline()
if not line:
print "Warning: Database connection info incomplete!"
else:
pwd = line
conn = psycopg2.connect(database='postgres', user='postgres', host='localhost', password=pwd)
return conn
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
if __name__ == "__main__":
app.debug = True
try:
with app.app_context():
conn = connect_to_database()
g._database = conn
print "connected!"
except:
print "I am unable to connect to the database"
app.run(host="0.0.0.0", port=80)