/
build_templates.py
132 lines (110 loc) · 3.21 KB
/
build_templates.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
#!/usr/bin/env python2
# encoding: utf-8
"""Build html template for pages with resources inlined.
**EXPERIMENTAL**
"""
from __future__ import print_function, division, unicode_literals
from io import open
from subprocess import Popen, PIPE
import platform
import os
from time import sleep
# TODO: move all of these address things into a common config place
DEBUG = os.environ.get('FRONTEND_DEBUG') == 'on'
HOST = platform.node()
if DEBUG:
PORT = 8100
else:
PORT = 8000
TORNADO_SERVER = 'http://{}:{}/'.format(HOST, PORT)
BUILD_DIR = os.path.join(os.path.dirname(__file__), 'build')
PAGE_LIST = [
('/', 'index'),
]
TEMPLATE = """
{{% extends 'base.html' %}}
{{% block styles %}}<style>{style}</style>{{% end %}}
{{% block loadCSS %}}<script id="loadcss">{load_css_func}
loadCSS('/static/vendor.css');
loadCSS('/static/app.css');
</script>{{% end %}}
""".strip()
LOAD_CSS_FUNC = """
function loadCSS(href) {
'use strict';
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = href;
link.media = 'only foo';
var place = document.getElementById('loadcss');
place.parentNode.insertBefore(link, place);
var sheets = document.styleSheets;
link.testReady = function(cb) {
var ready = false;
for (var i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
var rules = sheet.cssRules || sheet.rules;
if (sheet.href &&
rules.length > 0 &&
sheet.href.length >= href.length &&
sheet.href.indexOf(href) == sheet.href.length - href.length) {
ready = true;
console.log(href + " ready.");
}
}
if (ready) {
cb();
} else {
setTimeout(function() {
link.testReady(cb);
}, 5);
}
};
link.testReady(function() {
link.media = 'all';
});
return link;
}
"""
def _process_js_func(func):
lines = func.split('\n')
lines = [l.strip() for l in lines]
out_str = ' '.join(lines)
return out_str
def _parse_args():
from argparse import ArgumentParser
parser = ArgumentParser(description="")
args = parser.parse_args()
return args
def run():
args = _parse_args()
try:
tornado = Popen(['python', '../static-server.py'])
node = Popen(['node', 'build/server/server.js'])
sleep(1)
for url, page_name in PAGE_LIST:
out_file = os.path.join(BUILD_DIR, page_name + '.html')
# useless, since template is cached by tornado
os.unlink(out_file)
url = TORNADO_SERVER[:-1] + url
phantom = Popen(['phantomjs', 'ph.js', url], stdout=PIPE)
styles = phantom.stdout.readline().rstrip()
load_css_func = _process_js_func(LOAD_CSS_FUNC)
template = TEMPLATE.format(style=styles, load_css_func=load_css_func)
with open(out_file, 'w') as f:
f.write(template)
print("out file:", out_file)
phantom.communicate()
finally:
try:
if tornado.poll() is None:
tornado.terminate()
except Exception:
pass
try:
if node.poll() is None:
node.terminate()
except Exception:
pass
if __name__ == "__main__":
run()