This repository has been archived by the owner on Apr 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomeization.py
244 lines (192 loc) · 7.92 KB
/
customeization.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import os
import hmac
from hashlib import sha1
import json
from datetime import timedelta
import tempfile
from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify
from flask import redirect
from flask import url_for
from flask import send_from_directory
from flask import make_response
from flask_wtf import CsrfProtect
from werkzeug.utils import secure_filename
import conf
from forms import ProcessForm
from task import make_celery
from task import package_files
from task import get_binary_git_info
from task import perform_git_update
app = Flask(__name__)
app.secret_key = conf.SECRET_KEY
app.config['CELERY_BACKEND'] = 'amqp'
app.config['CELERY_BROKER_URL'] = 'amqp://guest:guest@localhost:5672/'
# Configure the periodic task scheduler.
# 1. 'clean-up-build-directory': Will run once a day to clean up directories in the
# build directory that are older than a specified time (set in the configuration file, BUILD_EXPIRY)
app.config['CELERYBEAT_SCHEDULE'] = {
'clean-up-build-directory': {
'task': 'task.cleanup_build_directory',
'schedule': timedelta(hours=24)
}
}
app.debug = True
celery = make_celery(app)
csrf = CsrfProtect()
csrf.init_app(app)
# Check to make sure the app is reporting the latest versions of everything
get_binary_git_info.apply_async()
@app.route('/', methods=['GET', 'POST'])
def index():
form = ProcessForm(request.form)
# If there are form errors it will not pass form.validate() and
# will fall through to the "GET" handler, passing along the errors
# to the template.
# This means we don't have to check the presence of (most) options
if request.method == 'POST' and form.validate():
schema_language = request.form.get('schema_language', None)
source_option = request.form.get('source_options', None)
canonicalized_source = request.files.get('source_canonical_file', None)
customization_option = request.form.get('customization_options', None)
local_customization = request.files.get('local_customization_file', None)
verbose_output = request.form.get('verbose_output', None)
uploaded_customization = None
# at this point the form will have been checked to make sure the local customization
# file is present if the local customization option is selected,
# but we'll do another sanity check here just to make sure.
if customization_option == 'z-local-customization' and local_customization is not None:
tmpdir = tempfile.mkdtemp()
filename = secure_filename(local_customization.filename)
uploaded_customization = os.path.join(tmpdir, filename)
local_customization.save(uploaded_customization)
uploaded_source = None
if source_option == 'local-source' and canonicalized_source is not None:
tmpdir = tempfile.mkdtemp()
filename = secure_filename(canonicalized_source.filename)
uploaded_source = os.path.join(tmpdir, filename)
canonicalized_source.save(uploaded_source)
verbose = False
if verbose_output == "y":
verbose = True
res = None
res = package_files.apply_async(args=[schema_language, source_option, customization_option],
kwargs={"uploaded_customization": uploaded_customization,
"uploaded_source": uploaded_source,
"verbose": verbose})
return redirect(url_for('process_and_download') + "?cid=" + str(res))
latest_git_revisions = None
with open(os.path.join(app.root_path, 'info.json'), 'r') as info:
js = json.load(info)
latest_git_revisions = js.get('mei_git_revisions', None)
tei_stylesheets_version = js.get('tei_stylesheets_version', None)
d = {
'latest_revisions': latest_git_revisions,
'tei_stylesheets_version': tei_stylesheets_version,
}
return render_template("index.html", form=form, **d)
@app.route('/process/')
def process_and_download():
celery_job_id = request.args.get('cid', None)
# if this is called without a cid, throw the user to the index page.
if celery_job_id is None:
return redirect(url_for('index'))
latest_git_revisions = None
with open(os.path.join(app.root_path, 'info.json'), 'r') as gitinfo:
js = json.load(gitinfo)
latest_git_revisions = js.get('mei_git_revisions', None)
tei_stylesheets_version = js.get('tei_stylesheets_version', None)
d = {
'celery_job_id': celery_job_id,
'latest_revisions': latest_git_revisions,
'tei_stylesheets_version': tei_stylesheets_version,
}
return render_template("process.html", **d)
@app.route('/progress/')
def progress():
celery_job_id = request.args.get('cid', None)
# if this is called without a cid, throw the user to the index page.
if celery_job_id is None:
return redirect(url_for('index'))
task = celery.AsyncResult(celery_job_id)
if task.status == 'PROGRESS':
d = {
'status': task.status,
'percentage': 50,
'download': None,
'message': None
}
return jsonify(d)
elif task.status == 'SUCCESS':
print(task.result)
result = os.path.relpath(task.result['file'], app.root_path)
d = {
'status': task.status,
'percentage': 100,
'download': "/" + result,
'message': task.result['message']
}
return jsonify(d)
elif task.status == 'FAILURE':
d = {
'status': task.status,
'percentage': None,
'download': None,
'message': "The task execution failed."
}
return jsonify(d), 500
else:
d = {
'status': task.status,
'percentage': None,
'download': None,
'message': None
}
return jsonify(d)
@app.route('/build/<path:filename>', methods=['GET'])
def build(filename):
return send_from_directory(conf.BUILT_SCHEMA_DIR, filename, as_attachment=True)
@csrf.exempt
@app.route('/google-code/', methods=['POST', ])
def googlecode():
print('Updating from google code.')
request_body = request.data
google_code_key = conf.GOOGLE_CODE_AUTHKEY
m = hmac.new(google_code_key)
m.update(request_body)
digest = m.hexdigest()
incoming_header = request.headers.get("Google-Code-Project-Hosting-Hook-Hmac")
if digest != incoming_header:
print("Digest did not match Message Secret")
json_resp = jsonify(message="Message Secret was not correct")
return make_response(json_resp, 400)
# fire this off to Celery to pull in the latest results
# perform_svn_update.apply_async()
json_resp = jsonify(message="Success.")
return make_response(json_resp, 200)
@csrf.exempt
@app.route('/github/', methods=['GET', 'POST'])
def github():
if request.method == 'GET':
return make_response(jsonify(message='Github Updating Endpoint.'))
if request.headers.get('X-Github-Event') == 'ping':
print('Received a ping.')
return make_response(jsonify(message='pong'))
gh_key = conf.GITHUB_SECRET_KEY
m = hmac.new(gh_key.encode(), msg=request.data, digestmod=sha1)
digest = m.hexdigest()
signature = request.headers.get('X-Hub-Signature').split('=')[1]
if not hmac.compare_digest(digest, signature):
print('Digest did not match Message Secret!')
print("Digest: " + digest)
print("Signature: " + signature)
json_resp = jsonify(message="Message Secret was not correct")
return make_response(json_resp, 403)
print("Hooray, digest compare succeeded! Hello, GitHub. It's good to see you.")
perform_git_update.apply_async()
json_resp = jsonify(message="Success.")
return make_response(json_resp, 200)
if __name__ == '__main__':
app.run()