forked from MediaCrush/MediaCrush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
105 lines (85 loc) · 3.32 KB
/
app.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
from mediacrush.app import app
from mediacrush.config import _cfg, _cfgi
from mediacrush.files import extension
import os
import scss
import coffeescript
from slimit import minify
from shutil import rmtree, copyfile
app.static_folder = os.path.join(os.getcwd(), "static")
scss.config.LOAD_PATHS = [
os.path.join(os.getcwd(), 'styles')
]
def prepare():
if os.path.exists(app.static_folder):
rmtree(app.static_folder)
os.makedirs(app.static_folder)
compiler = scss.Scss(scss_opts = {
'style': 'compressed' if not app.debug else None
})
# Compile styles (scss)
d = os.walk('styles')
for f in list(d)[0][2]:
if extension(f) == "scss":
with open(os.path.join('styles', f)) as r:
output = compiler.compile(r.read())
parts = f.rsplit('.')
css = '.'.join(parts[:-1]) + ".css"
with open(os.path.join(app.static_folder, css), "w") as w:
w.write(output)
w.flush()
# Compile scripts (coffeescript)
d = os.walk('scripts')
preprocess = ['scripts/mediacrush.js']
for f in list(d)[0][2]:
outputpath = os.path.join(app.static_folder, os.path.basename(f))
inputpath = os.path.join('scripts', f)
if extension(f) == "js":
if inputpath in preprocess:
with open(inputpath) as r:
output = r.read().decode("utf-8")
output = output.replace("{{ protocol }}", _cfg("protocol"))
output = output.replace("{{ domain }}", _cfg("domain"))
with open(outputpath, "w") as w:
w.write(output.encode("utf-8"))
w.flush()
else:
copyfile(inputpath, outputpath)
elif extension(f) == "manifest":
with open(inputpath) as r:
manifest = r.read().decode("utf-8").split('\n')
javascript = ''
for script in manifest:
script = script.strip(' ')
if script == '' or script.startswith('#'):
continue
bare = False
if script.startswith('bare: '):
bare = True
script = script[6:]
with open(os.path.join('scripts', script)) as r:
coffee = r.read()
if script.endswith('.js'):
javascript += coffee # straight up copy
else:
javascript += coffeescript.compile(coffee, bare=bare)
output = '.'.join(f.rsplit('.')[:-1]) + '.js'
if not app.debug:
javascript = minify(javascript)
with open(os.path.join(app.static_folder, output), "w") as w:
w.write(javascript.encode("utf-8"))
w.flush()
d = os.walk('images')
for f in list(d)[0][2]:
outputpath = os.path.join(app.static_folder, os.path.basename(f))
inputpath = os.path.join('images', f)
copyfile(inputpath, outputpath)
@app.before_first_request
def compile_first():
prepare()
@app.before_request
def compile_if_debug():
if app.debug and _cfg("debug-static-recompile") == 'true':
prepare()
if __name__ == '__main__':
app.run(host=_cfg("debug-host"), port=_cfgi('debug-port'), debug=True)