This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_static.py
executable file
·107 lines (82 loc) · 3.27 KB
/
compile_static.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
#!/usr/bin/env python
import os
import tempfile
from shutil import copyfile, rmtree
import coffeescript
import scss
from mediacrush.app import app
from mediacrush.config import _cfg, _cfgi
app.static_folder = os.path.join(os.getcwd(), "static")
scss.config.LOAD_PATHS = [os.path.join(os.getcwd(), "styles")]
def prepare():
path = tempfile.mkdtemp()
compiler = scss.Scss(scss_opts={"style": "compressed" if not app.debug else None})
# Unsafe extnsion function used only here
extension = lambda f: f.rsplit(".", 1)[1].lower()
# Compile styles (scss)
d = os.walk("styles")
for f in list(d)[0][2]:
if extension(f) == "scss":
print(("[scss] %s" % f))
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(path, 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(path, 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()
output = output.replace("{{ protocol }}", _cfg("protocol"))
output = output.replace("{{ domain }}", _cfg("domain"))
with open(outputpath, "w") as w:
w.write(output)
w.flush()
else:
copyfile(inputpath, outputpath)
elif extension(f) == "manifest":
with open(inputpath) as r:
manifest = r.read().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:]
print(("[coffee] %s" % script))
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"
with open(os.path.join(path, output), "w") as w:
w.write(javascript)
w.flush()
if os.path.exists(app.static_folder):
rmtree(app.static_folder)
os.makedirs(app.static_folder)
d = os.walk(path)
for f in list(d)[0][2]:
inputpath = os.path.join(path, os.path.basename(f))
outputpath = os.path.join(app.static_folder, f)
copyfile(inputpath, outputpath)
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)
if __name__ == "__main__":
prepare()