Add tlmConfirm to tlm_dl ota packet-structure (#2991)
[ExpressLRS.git] / src / python / build_html.py
blob36cf50a1c1af375203c5a8fdadaa05c36034579d
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 has_sub_ghz = '-DRADIO_SX127X=1' in env['BUILD_FLAGS'] or '-DRADIO_LR1121=1' in env['BUILD_FLAGS']
37 if '-DRADIO_SX128X=1' in env['BUILD_FLAGS']:
38 chip = 'SX128X'
39 elif '-DRADIO_SX127X=1' in env['BUILD_FLAGS']:
40 chip = 'SX127X'
41 elif '-DRADIO_LR1121=1' in env['BUILD_FLAGS']:
42 chip = 'LR1121'
43 if 'ESP8285' in env['PIOENV']:
44 is8285 = True
45 else:
46 is8285 = False
47 data = template.render({
48 'VERSION': get_version(env),
49 'PLATFORM': re.sub("_via_.*", "", env['PIOENV']),
50 'isTX': isTX,
51 'hasSubGHz': has_sub_ghz,
52 'chip': chip,
53 'is8285': is8285
55 if mainfile.endswith('.html'):
56 data = html_minifier.html_minify(data)
57 if mainfile.endswith('.css'):
58 data = rcssmin.cssmin(data)
59 if mainfile.endswith('.js'):
60 data = rjsmin.jsmin(data)
61 out.write('static const char PROGMEM %s[] = {\n' % var)
62 out.write(','.join("0x{:02x}".format(c) for c in compress(data.encode('utf-8'))))
63 out.write('\n};\n\n')
65 def build_common(env, mainfile, isTX):
66 fd, path = tempfile.mkstemp()
67 try:
68 with os.fdopen(fd, 'w') as out:
69 build_version(out, env)
70 build_html(mainfile, "INDEX_HTML", out, env, isTX)
71 build_html("scan.js", "SCAN_JS", out, env, isTX)
72 build_html("mui.js", "MUI_JS", out, env)
73 build_html("elrs.css", "ELRS_CSS", out, env)
74 build_html("hardware.html", "HARDWARE_HTML", out, env, isTX)
75 build_html("hardware.js", "HARDWARE_JS", out, env)
76 build_html("cw.html", "CW_HTML", out, env)
77 build_html("cw.js", "CW_JS", out, env)
78 build_html("lr1121.html", "LR1121_HTML", out, env)
79 build_html("lr1121.js", "LR1121_JS", out, env)
81 finally:
82 if not os.path.exists("include/WebContent.h") or not filecmp.cmp(path, "include/WebContent.h"):
83 shutil.copyfile(path, "include/WebContent.h")
84 os.remove(path)
86 target_name = env['PIOENV'].upper()
87 build_common(env, "index.html", not '_RX_' in target_name)