-
Notifications
You must be signed in to change notification settings - Fork 0
/
unbuild.py
70 lines (58 loc) · 2.04 KB
/
unbuild.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
#!/usr/bin/env python3
from os import path
import subprocess
import flask
from flask import Flask, Response
from flask import request
from flask_cors import cross_origin
app = Flask(__name__)
ROOT_PATH = path.dirname(path.abspath(__file__))
EMAILS = {
'ethanjli': '[email protected]',
'gersh': '[email protected]'
}
SCRIPT_PATH = path.join(ROOT_PATH, 'commit_project.sh')
COMMIT_MESSAGE_FILE = 'commit_message.txt'
HTML_FILE = 'stella.html'
# HANDLERS
def commit_project(user_name, commit_message, html):
print('Handling commit request from {}:\n{}'.format(user_name, commit_message))
if not commit_message:
raise ValueError('Empty commit message!')
if not html:
raise ValueError('Empty Twine HTML file!')
try:
with open(path.join(ROOT_PATH, COMMIT_MESSAGE_FILE), 'w') as f:
f.write(commit_message)
with open(path.join(ROOT_PATH, HTML_FILE), 'w') as f:
f.write(html)
user_email = EMAILS[user_name]
subprocess.check_call(['/bin/bash', SCRIPT_PATH, user_name, user_email],
stderr=subprocess.STDOUT)
except KeyError:
raise ValueError('Unknown user!')
except subprocess.CalledProcessError:
raise RuntimeError('Unbuild/commit failed!')
# ROUTES
@app.route('/')
def ping():
return "Hello, World!"
@app.route('/save', methods=['POST'])
@cross_origin()
def save():
data = request.get_json(force=True)
try:
commit_project(data['auth']['username'], data['message'], data['html'])
response = flask.make_response('{"status": "success"}')
response.headers['Content-Type'] = 'application/json'
except ValueError as e:
raise
#response = flask.make_response('{"status": "error", "error": "' + str(e) + '"}')
#response.status_code = 400
except RuntimeError as e:
raise
#response = flask.make_response('{"status": "error", "error": "' + str(e) + '"}')
#response.status_code = 500
return response
if __name__ == '__main__':
app.run()