forked from KanoComputing/make-art
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
426 lines (318 loc) · 12.1 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# server.py
#
# Copyright (C) 2014-2015 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2
#
import json
import os
import time
import logging
from flask import Flask, Response, request, send_from_directory
from kano_profile.apps import load_app_state_variable
from kano_profile.badges import increment_app_state_variable_with_dialog, \
save_app_state_variable_with_dialog, calculate_xp
from kano_world.share_helpers import login_and_share
from kano.network import is_internet
from kano.utils.audio import play_sound
from kano.utils.file_operations import empty_directory, ensure_dir, \
recursively_copy
from kano.logging import logger
APP_NAME = 'kano-draw'
PARENT_PID = None
CHALLENGE_DIR = os.path.expanduser('~/Draw-content')
WALLPAPER_DIR = os.path.join(CHALLENGE_DIR, 'wallpapers')
STATIC_ASSET_DIR = os.path.join(os.path.expanduser('~'),
'.make-art-assets')
ensure_dir(CHALLENGE_DIR)
ensure_dir(WALLPAPER_DIR)
ensure_dir(STATIC_ASSET_DIR)
def _copy_package_assets():
# Use abs paths for both src and dest for subsequent replacements to work
src_dir = os.path.abspath(_get_package_static_dir())
dest_dir = os.path.abspath(STATIC_ASSET_DIR)
# First Clear this cache
empty_directory(dest_dir)
# Now copy the static assets
recursively_copy(src_dir, dest_dir)
logger.info('Successfully copied over static assets')
def _get_package_static_dir():
bin_path = os.path.abspath(os.path.dirname(__file__))
if bin_path.startswith('/usr'):
return '/usr/share/kano-draw'
else:
return os.path.abspath(os.path.join(bin_path, '../www'))
def _get_co_assets():
from kano_content.api import ContentManager
cm = ContentManager.from_local()
co_index = {}
for co in cm.list_local_objects(spec='make-art-assets'):
co_files = co.get_data('').get_content()
if len(co_files) != 2:
logger.warning(
'Count of files other than 2 in co[{}], skipping'.format(
co.get_data('').get_dir()
)
)
continue
# Check whether the first file is the index
index_no = _get_co_index_apply_order(co_files[0])
if index_no is not None:
co_index[index_no] = co_files[1]
else:
# It wasn't the first one, go for the second one
index_no = _get_co_index_apply_order(co_files[1])
if index_no is not None:
co_index[index_no] = co_files[0]
else:
err_msg = 'None of the files contained in co have apply index'
logger.error(err_msg)
continue
return co_index
def _apply_co_packages(dest_dir):
import tarfile
co_index = _get_co_assets()
for order in sorted(co_index.iterkeys()):
tar_file = co_index[order]
# First try to open the file
try:
tarball = tarfile.open(tar_file)
except (IOError, OSError) as exc:
err_msg = "Couldn't open file '{}', [{}]".format(tar_file, exc)
logger.error(err_msg)
continue
except tarfile.ReadError as exc:
err_msg = 'Error parsing tarfile "{}", [{}]'.format(tar_file, exc)
logger.error(err_msg)
continue
else:
# Now try to extract the files one by one
with tarball:
for tarred_file in tarball:
try:
tarball.extract(tarred_file, path=dest_dir)
except IOError as exc:
# This is to guard against weird tar behaviour when
# trying to ovewrite symlinks
bad_filename = os.path.join(dest_dir, tarred_file.name)
if os.path.islink(bad_filename):
logger.debug(
'Remove link and ovewrite "{}"'.format(
bad_filename)
)
os.remove(os.path.join(dest_dir, tarred_file.name))
tarball.extract(tarred_file, path=dest_dir)
def _get_co_index_apply_order(fname):
index_no = None
# Files names have absolute path
if os.path.basename(fname) == 'index.json':
try:
index_fh = open(fname)
except (IOError, OSError) as exc:
err_msg = 'Error opening file "{}". [{}]'.format(
fname,
exc
)
logger.error(err_msg)
else:
with index_fh:
try:
ind_data = json.load(index_fh)
index_no = ind_data['apply_order']
except KeyError as exc:
err_msg = ("JSON in '{}' doesn't contain right key: "
"[{}]").format(fname, exc)
logger.error(err_msg)
except ValueError as exc:
err_msg = 'File "{}" is not a valid JSON: [{}]'.format(
fname,
exc
)
logger.error(err_msg)
return index_no
def _get_static_dir():
return STATIC_ASSET_DIR
def _get_image_from_str(img_str):
import base64
image_b64 = img_str.split(',')[-1]
image_data = base64.b64decode(image_b64)
return image_data
def _save(data):
filename = data['filename']
try:
desc = data['description']
except KeyError:
desc = ''
code = data['code']
image = _get_image_from_str(data['image'])
filepath = os.path.join(CHALLENGE_DIR, '{}.draw'.format(filename))
json_path = os.path.join(CHALLENGE_DIR, '{}.json'.format(filename))
img_path = os.path.join(CHALLENGE_DIR, '{}.png'.format(filename))
with open(filepath, 'w') as f:
f.write(code)
with open(json_path, 'w') as f:
f.write(
json.dumps({
'filename': filename,
'description': desc
})
)
with open(img_path, 'wb') as f:
f.write(image)
return (filename, filepath)
_copy_package_assets()
_apply_co_packages(STATIC_ASSET_DIR)
server = Flask(__name__, static_folder=_get_static_dir(), static_url_path='/')
server_logger = logging.getLogger('werkzeug')
server_logger.setLevel(logging.ERROR)
@server.route('/')
# Redirect a localLoad back to index for routing in Angular
@server.route('/localLoad/<path:path>')
def root(path=None):
return server.send_static_file('index.html')
@server.route('/<path:path>')
def static_proxy(path):
# send_static_file will guess the correct MIME type
return server.send_static_file(path)
@server.route("/challenge/local/<path:filename>", methods=['POST'])
def save_challenge(filename):
data = json.loads(request.data)
_save(data)
return ''
@server.route("/challenge/local/<path:path>", methods=['GET'])
def load_challenge(path):
directory, filename = os.path.split(path)
return send_from_directory('/{}'.format(directory),
filename,
as_attachment=True)
@server.route("/challenge/local/wallpaper/<path:filename>", methods=['POST'])
def save_wallpaper(filename):
data = json.loads(request.data)
imgs = {
'1024': _get_image_from_str(data['image_1024']),
'4-3': _get_image_from_str(data['image_4_3']),
'16-9': _get_image_from_str(data['image_16_9'])
}
img_path = os.path.join(
WALLPAPER_DIR,
'{filename}-{{ratio}}.png'.format(filename=filename)
)
for ratio, img_data in imgs.iteritems():
with open(img_path.format(ratio=ratio), 'wb') as f:
f.write(img_data)
return ''
@server.route("/challenge/web/<path:filename>", methods=['POST'])
def share(filename):
# TODO: Move this connection handling into a function in Kano Utils
import subprocess
if not is_internet():
subprocess.call(['sudo', 'kano-settings', '4'])
if not is_internet():
return 'You have no internet'
data = json.loads(request.data)
filename, filepath = _save(data)
success, msg = login_and_share(filepath, filename, APP_NAME)
if not success:
return msg
increment_app_state_variable_with_dialog(APP_NAME, 'shared', 1)
return ''
@server.route('/challenge/web', methods=['GET'])
def load_share():
# TODO: Import kano-share python module and use return code instead
import subprocess
p = subprocess.Popen(["kano-share", APP_NAME],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
for line in p.stdout:
path = line.split('File Path: ')[-1]
if len(line) == len(path):
continue
path = path.replace('$HOME', os.path.expanduser('~')).rstrip('\n')
directory, _, filename = path.rpartition('/')
return send_from_directory(directory, filename, as_attachment=True)
@server.route('/progress/<world>/<int:challengeNo>', methods=['POST'])
def _save_level(world, challengeNo):
old_xp = calculate_xp()
needsToSave = False
groups = load_app_state_variable(APP_NAME, 'groups')
# We might need to load the worlds file here so that we're sure that
# no one is abusing the API from the OS
if groups is None:
groups = {}
if world in groups:
if groups[world]['challengeNo'] < challengeNo:
groups[world]['challengeNo'] = challengeNo
needsToSave = True
else:
groups[world] = {'challengeNo': challengeNo}
needsToSave = True
if needsToSave:
save_app_state_variable_with_dialog(APP_NAME, 'groups', groups)
new_xp = calculate_xp()
return str(new_xp - old_xp)
@server.route('/progress', methods=['GET'])
def _load_level():
value = {
'groups': load_app_state_variable(APP_NAME, 'groups'),
'challenge': load_app_state_variable(APP_NAME, 'challenge')
}
# Previously we used to save the progress as "level"
level = load_app_state_variable(APP_NAME, 'level')
if value['groups'] is None:
value['groups'] = {}
# Replace the Challege var here.
if level > value['challenge']:
value['challenge'] = level
value = json.dumps(value)
return Response(value, content_type='application/json')
@server.route('/lines-of-code', methods=['POST'])
def _increment_lines_of_code():
data = json.loads(request.data)
new_lines = data['newLines']
increment_app_state_variable_with_dialog(
APP_NAME, 'lines_of_code', new_lines
)
return ''
@server.route('/shutdown', methods=['POST'])
def _shutdown():
import signal
try:
server_shutdown = request.environ.get('werkzeug.server.shutdown')
if server_shutdown is not None:
logger.info('Will attempt to shutdown the server')
server_shutdown()
except Exception as exc:
logger.error(
'Error while trying to shut down the server: [{}]'.format(exc)
)
# Send signal to parent to initiate shutdown
os.kill(PARENT_PID, signal.SIGINT)
@server.route('/browsemore', methods=['POST'])
def _browsemore():
from kano import run_bg
run_bg("chromium http://world.kano.me/shares/kano-draw")
@server.errorhandler(404)
def page_not_found(err):
err_msg = 'Cannot find file {}'.format(request.path)
return err_msg, 404
@server.route('/play_sound/<path:filename>', methods=['POST'])
def play_sounds(filename):
print os.path.realpath(os.path.join(_get_static_dir(), filename))
sound_file = os.path.realpath(os.path.join(_get_static_dir(), filename))
play_sound(sound_file)
return ''
def start(parent_pid=None):
"""
The server process will receive any requests to shutdown but
the app that runs this as a daemon will be unaware of this
request so store the PID of the parent.
"""
global PARENT_PID
PARENT_PID = parent_pid
# Run the server
try:
server.run(port=8000)
except EnvironmentError as exc:
if exc.errno == 98:
logger.error('Another server is running bound at our port')
_shutdown()
time.sleep(2)