Increase maximum number of Lua params (#2207)
[ExpressLRS.git] / src / python / build_html.py
blob05823ec898cc67d2da10d54d08fac21ffcf86205
1 Import("env")
2 import os
3 import re
4 import io
5 import tempfile
6 import filecmp
7 import shutil
8 import gzip
9 from external.minify import (html_minifier, rcssmin, rjsmin)
10 from external.wheezy.template.engine import Engine
11 from external.wheezy.template.ext.core import CoreExtension
12 from external.wheezy.template.loader import FileLoader
15 def get_version(env):
16 return '%s (%s)' % (env.get('GIT_VERSION'), env.get('GIT_SHA'))
18 def build_version(out, env):
19 out.write('const char *VERSION = "%s";\n\n' % get_version(env))
21 def compress(data):
22 """Compress data in one shot and return the compressed string.
23 Optional argument is the compression level, in range of 0-9.
24 """
25 buf = io.BytesIO()
26 with gzip.GzipFile(fileobj=buf, mode='wb', compresslevel=9, mtime=0.0) as f:
27 f.write(data)
28 return buf.getvalue()
30 def build_html(mainfile, var, out, env, isTX=False):
31 engine = Engine(
32 loader=FileLoader(["html"]),
33 extensions=[CoreExtension("@@")]
35 template = engine.get_template(mainfile)
36 data = template.render({
37 'VERSION': get_version(env),
38 'PLATFORM': re.sub("_via_.*", "", env['PIOENV']),
39 'isTX': isTX,
40 'sx127x': '-DRADIO_SX127X=1' in env['BUILD_FLAGS']
42 if mainfile.endswith('.html'):
43 data = html_minifier.html_minify(data)
44 if mainfile.endswith('.css'):
45 data = rcssmin.cssmin(data)
46 if mainfile.endswith('.js'):
47 data = rjsmin.jsmin(data)
48 out.write('static const char PROGMEM %s[] = {\n' % var)
49 out.write(','.join("0x{:02x}".format(c) for c in compress(data.encode('utf-8'))))
50 out.write('\n};\n\n')
52 def build_common(env, mainfile, isTX):
53 fd, path = tempfile.mkstemp()
54 try:
55 with os.fdopen(fd, 'w') as out:
56 build_version(out, env)
57 build_html(mainfile, "INDEX_HTML", out, env, isTX)
58 build_html("scan.js", "SCAN_JS", out, env, isTX)
59 build_html("mui.js", "MUI_JS", out, env)
60 build_html("elrs.css", "ELRS_CSS", out, env)
61 build_html("hardware.html", "HARDWARE_HTML", out, env, isTX)
62 build_html("hardware.js", "HARDWARE_JS", out, env)
63 build_html("cw.html", "CW_HTML", out, env)
64 build_html("cw.js", "CW_JS", out, env)
66 finally:
67 if not os.path.exists("include/WebContent.h") or not filecmp.cmp(path, "include/WebContent.h"):
68 shutil.copyfile(path, "include/WebContent.h")
69 os.remove(path)
71 target_name = env['PIOENV'].upper()
72 build_common(env, "index.html", not '_RX_' in target_name)