Merge branch '2.0.x-maintenance' into master-merge-commit
[ExpressLRS.git] / src / python / build_html.py
blobe8b04098f1da17750661572b12b71fe6c495a0e1
1 Import("env")
2 import os
3 import re
4 import io
5 import tempfile
6 import filecmp
7 import shutil
8 import elrs_helpers
10 import gzip
11 from minify import (html_minifier, rcssmin, rjsmin)
13 def get_version(env):
14 return '%s (%s) %s' % (env.get('GIT_VERSION'), env.get('GIT_SHA'), env.get('REG_DOMAIN'))
16 def build_version(out, env):
17 out.write('const char *VERSION = "%s";\n\n' % get_version(env))
19 def compress(data):
20 """Compress data in one shot and return the compressed string.
21 Optional argument is the compression level, in range of 0-9.
22 """
23 buf = io.BytesIO()
24 with gzip.GzipFile(fileobj=buf, mode='wb', compresslevel=9, mtime=0.0) as f:
25 f.write(data)
26 return buf.getvalue()
28 def build_html(mainfile, var, out, env):
29 with open('html/%s' % mainfile, 'r') as file:
30 data = file.read()
31 if mainfile.endswith('.html'):
32 data = html_minifier.html_minify(data).replace('@VERSION@', get_version(env)).replace('@PLATFORM@', re.sub("_via_.*", "", env['PIOENV']))
33 if mainfile.endswith('.css'):
34 data = rcssmin.cssmin(data)
35 if mainfile.endswith('.js'):
36 data = rjsmin.jsmin(data)
37 out.write('static const char PROGMEM %s[] = {\n' % var)
38 out.write(','.join("0x{:02x}".format(c) for c in compress(data.encode('utf-8'))))
39 out.write('\n};\n\n')
41 def build_common(env, mainfile):
42 fd, path = tempfile.mkstemp()
43 try:
44 with os.fdopen(fd, 'w') as out:
45 build_version(out, env)
46 build_html(mainfile, "INDEX_HTML", out, env)
47 build_html("scan.js", "SCAN_JS", out, env)
48 build_html("main.css", "CSS", out, env)
49 build_html("logo.svg", "FLAG", out, env)
50 finally:
51 if not os.path.exists("include/WebContent.h") or not filecmp.cmp(path, "include/WebContent.h"):
52 shutil.copyfile(path, "include/WebContent.h")
53 os.remove(path)
55 platform = env.get('PIOPLATFORM', '')
57 if platform in ['espressif8266']:
58 build_common(env, "rx_index.html")
59 elif platform in ['espressif32']:
60 build_common(env, "tx_index.html")