-
Notifications
You must be signed in to change notification settings - Fork 18
/
flatten.py
executable file
·114 lines (85 loc) · 3.57 KB
/
flatten.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
#!/usr/local/bin/python3
python = 3
try:
xrange
python = 2
except:
pass
if python == 2:
raise Exception("Use python3")
import base64
import codecs
import hashlib
import os
import re
import subprocess
UGLIFY = True
#UGLIFY = False
reScript = re.compile('<script(?:[^>]+)src="([^"]*)"(?:[^>]*)>((?:.|\n)*?)</script>')
reStyle = re.compile('<link(?:[^>]+)href="([^"]*)"(?:[^>]*)/(?:[^>]*)>')
reJpg = re.compile('url\\(([^)]+.jpg)\\)')
rePng = re.compile('url\\(([^)]+.png)\\)')
reGif = re.compile('url\\(([^)]+.gif)\\)')
reWoff = re.compile('url\\(([^)]+.woff)\\)')
def inlinify_script(match):
if match.group(2).strip() != '': raise Exception('script has body')
filename = match.group(1)
if filename == '/lib-client/node_modules/ethers/dist/ethers.min.js' and not UGLIFY:
filename = '/lib-client/node_modules/ethers/dist/ethers.js'
if filename.find('.min.') >= 0:
script = open(filename, 'rb').read().decode('utf8')
else:
if UGLIFY:
script = subprocess.check_output(['uglifyjs', filename]).decode('utf8')
else:
script = open(filename).read()
if filename == '/scripts/index.js':
undebug = script.replace('var DEBUG = true;', 'var DEBUG = false;')
if len(undebug) != len(script) + 1: raise Exception('DEBUG conversion error')
script = undebug
print("script", filename, len(script))
return '<script type="text/javascript">/* ' + filename + ' */ ' + script + '</script>'
def inlinify_style(match):
if match.group(0).find('rel="stylesheet"') == -1 or match.group(0).find('type="text/css"') == -1:
raise Exception('not a stylesheet')
if UGLIFY:
style = subprocess.check_output(['uglifycss', match.group(1)]).decode('utf8')
else:
style = open(match.group(1), 'rb').read().decode('utf8')
#style = reWoff.sub(inlinify_woff, style)
print("style", match.group(1), len(style))
return '<style type="text/css">/* ' + match.group(1) + ' */ ' + style + '</style>'
def inlinify_png(match):
png = open(match.group(1), 'rb').read()
print("png", match.group(1), len(png))
return 'url(data:image/png;base64,%s)' % base64.b64encode(png).decode('utf8')
def inlinify_jpg(match):
jpg = open(match.group(1), 'rb').read()
print("jpg", match.group(1), len(jpg))
return 'url(data:image/jpeg;base64,%s)' % base64.b64encode(jpg).decode('utf8')
def inlinify_gif(match):
gif = open(match.group(1), 'rb').read()
print("gif", match.group(1), len(gif))
return 'url(data:image/gif;base64,%s)' % base64.b64encode(gif).decode('utf8')
def inlinify_woff(match):
woff = open(match.group(1), "rb").read()
print("woff", match.group(1), len(woff))
return 'url(data:application/x-font-woff;charset=utf-8;base64,%s)' % base64.b64encode(woff).decode('utf8')
html = open('index.html').read()
print("html", "index.html", len(html))
html = reScript.sub(inlinify_script, html)
html = reWoff.sub(inlinify_woff, html)
html = reStyle.sub(inlinify_style, html)
html = rePng.sub(inlinify_png, html)
html = reJpg.sub(inlinify_jpg, html)
html = reGif.sub(inlinify_gif, html)
#html = reDevOnly.sub('PRODUCTION', html);
EthersHashTag = '<ETHERS_HASH>'
data = html.replace(EthersHashTag, '').encode('utf8')
if len(data) + len(EthersHashTag) != len(html.encode('utf8')):
raise Exception('ETHERS_HASH conversion bug')
ethersHash = hashlib.sha256(data).hexdigest()
data = html.replace(EthersHashTag, '0x' + ethersHash).encode('utf8');
open('./dist/index.html', 'wb').write(data)
print("hash: " + ethersHash)
print("html", "./dist/index.html", len(data))