-
Notifications
You must be signed in to change notification settings - Fork 17
/
webpage.py
179 lines (155 loc) · 7.12 KB
/
webpage.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
"""fips verb to build the oryol samples webpage"""
import os
import yaml
import shutil
import subprocess
import glob
from string import Template
from mod import log, util, project, emscripten, android
GitHubSamplesURL = 'https://github.com/floooh/oryol-samples/tree/master/src/'
ExportAssets = True
BuildConfig = 'webgl2-wasm-ninja-release'
#-------------------------------------------------------------------------------
def deploy_webpage(fips_dir, proj_dir, webpage_dir) :
"""builds the final webpage under under fips-deploy/oryol-samples-webpage"""
ws_dir = util.get_workspace_dir(fips_dir)
# load the websamples.yml file, should have been created during the last build
with open(webpage_dir + '/websamples.yml', 'r') as f :
samples = yaml.load(f.read())
# create directories
for platform in ['wasm'] :
platform_dir = '{}/{}'.format(webpage_dir, platform)
if not os.path.isdir(platform_dir) :
os.makedirs(platform_dir)
# link to the Core Samples
content = '<div class="thumb">\n'
content += ' <div class="thumb-title">To Core Samples...</div>\n'
content += ' <div class="img-frame"><a href="http://floooh.github.com/oryol/index.html"><img class="image" src="core_samples.jpg"></img></a></div>\n'
content += '</div>\n'
# build the thumbnail gallery
for sample in samples :
if sample['name'] != '__end__' :
name = sample['name']
imgPath = sample['image']
types = sample['type']
desc = sample['desc']
head, tail = os.path.split(imgPath)
if tail == 'none' :
imgFileName = 'dummy.jpg'
else :
imgFileName = tail
content += '<div class="thumb">\n'
content += ' <div class="thumb-title">{}</div>\n'.format(name)
content += ' <div class="img-frame"><a href="wasm/{}.html"><img class="image" src="{}" title="{}"></img></a></div>\n'.format(name,imgFileName,desc)
content += '</div>\n'
# populate the html template, and write to the build directory
with open(proj_dir + '/web/index.html', 'r') as f :
templ = Template(f.read())
html = templ.safe_substitute(samples=content)
with open(webpage_dir + '/index.html', 'w') as f :
f.write(html)
# copy other required files
for name in ['style.css', 'dummy.jpg', 'emsc.js', 'favicon.png', 'core_samples.jpg'] :
log.info('> copy file: {}'.format(name))
shutil.copy(proj_dir + '/web/' + name, webpage_dir + '/' + name)
# generate WebAssembly HTML pages
if emscripten.check_exists(fips_dir) :
wasm_deploy_dir = '{}/fips-deploy/oryol-samples/{}'.format(ws_dir, BuildConfig)
for sample in samples :
name = sample['name']
if name != '__end__' and 'emscripten' in sample['type'] :
log.info('> generate wasm HTML page: {}'.format(name))
for ext in ['js', 'wasm'] :
src_path = '{}/{}.{}'.format(wasm_deploy_dir, name, ext)
if os.path.isfile(src_path) :
shutil.copy(src_path, '{}/wasm/'.format(webpage_dir))
with open(proj_dir + '/web/wasm.html', 'r') as f :
templ = Template(f.read())
src_url = GitHubSamplesURL + sample['src'];
html = templ.safe_substitute(name=name, source=src_url)
with open('{}/wasm/{}.html'.format(webpage_dir, name), 'w') as f :
f.write(html)
# copy the screenshots
for sample in samples :
if sample['name'] != '__end__' :
img_path = sample['image']
head, tail = os.path.split(img_path)
if tail != 'none' :
log.info('> copy screenshot: {}'.format(tail))
shutil.copy(img_path, webpage_dir + '/' + tail)
#-------------------------------------------------------------------------------
def export_assets(fips_dir, proj_dir, webpage_dir) :
data_src_dir = '{}/data/'.format(proj_dir)
data_dst_dir = '{}/data/'.format(webpage_dir)
if not os.path.exists(data_dst_dir) :
os.makedirs(data_dst_dir)
for ext in ['txt', 'dump', 'kcc', 'tap', 'orb'] :
for data_file in glob.glob('{}/*.{}'.format(data_src_dir, ext)) :
shutil.copy(data_file, data_dst_dir)
tbui_from = '{}/tbui'.format(data_src_dir)
tbui_to = '{}/tbui'.format(data_dst_dir)
shutil.copytree(tbui_from, tbui_to)
nkui_from = '{}/nkui'.format(data_src_dir)
nkui_to = '{}/nkui'.format(data_dst_dir)
shutil.copytree(nkui_from, nkui_to)
#-------------------------------------------------------------------------------
def build_deploy_webpage(fips_dir, proj_dir) :
# if webpage dir exists, clear it first
ws_dir = util.get_workspace_dir(fips_dir)
webpage_dir = '{}/fips-deploy/oryol-samples-webpage'.format(ws_dir)
if os.path.isdir(webpage_dir) :
shutil.rmtree(webpage_dir)
os.makedirs(webpage_dir)
if emscripten.check_exists(fips_dir) :
project.gen(fips_dir, proj_dir, BuildConfig)
project.build(fips_dir, proj_dir, BuildConfig)
# export sample assets
if ExportAssets :
export_assets(fips_dir, proj_dir, webpage_dir)
# deploy the webpage
deploy_webpage(fips_dir, proj_dir, webpage_dir)
log.colored(log.GREEN, 'Generated Samples web page under {}.'.format(webpage_dir))
#-------------------------------------------------------------------------------
def serve_webpage(fips_dir, proj_dir) :
ws_dir = util.get_workspace_dir(fips_dir)
webpage_dir = '{}/fips-deploy/oryol-samples-webpage'.format(ws_dir)
p = util.get_host_platform()
if p == 'osx' :
try :
subprocess.call(
'open http://localhost:8000 ; python {}/mod/httpserver.py'.format(fips_dir),
cwd = webpage_dir, shell=True)
except KeyboardInterrupt :
pass
elif p == 'win':
try:
subprocess.call(
'cmd /c start http://localhost:8000 && python {}/mod/httpserver.py'.format(fips_dir),
cwd = webpage_dir, shell=True)
except KeyboardInterrupt:
pass
elif p == 'linux':
try:
subprocess.call(
'xdg-open http://localhost:8000; python {}/mod/httpserver.py'.format(fips_dir),
cwd = webpage_dir, shell=True)
except KeyboardInterrupt:
pass
#-------------------------------------------------------------------------------
def run(fips_dir, proj_dir, args) :
if len(args) > 0 :
if args[0] == 'build' :
build_deploy_webpage(fips_dir, proj_dir)
elif args[0] == 'serve' :
serve_webpage(fips_dir, proj_dir)
else :
log.error("Invalid param '{}', expected 'build' or 'serve'".format(args[0]))
else :
log.error("Param 'build' or 'serve' expected")
#-------------------------------------------------------------------------------
def help() :
log.info(log.YELLOW +
'fips webpage build\n' +
'fips webpage serve\n' +
log.DEF +
' build oryol samples webpage')