add shared header into espfs html generation

platformio
Thorsten von Eicken 5 years ago
parent 1046038d36
commit cdb8054e65
No known key found for this signature in database
GPG Key ID: C7F972A59D834B46
  1. 18
      mkespfs.py
  2. 32
      pio-genespfs.py

@ -18,7 +18,7 @@
# int32_t magic; # int32_t magic;
# int8_t flags; # int8_t flags;
# int8_t compression; # int8_t compression;
# int16_t nameLen; // including padding! # int16_t nameLen; // name must be null-terminated and NameLen includes padding!
# int32_t fileLenComp; # int32_t fileLenComp;
# int32_t fileLenDecomp; # int32_t fileLenDecomp;
# } __attribute__((packed)) EspFsHeader; # } __attribute__((packed)) EspFsHeader;
@ -34,25 +34,23 @@ def mkespfs(dir, outbuf):
FL_GZIP = 2 # gzipped file flag FL_GZIP = 2 # gzipped file flag
FL_LAST = 1 # last entry file flag FL_LAST = 1 # last entry file flag
f_html = list(Path(dir).rglob('*')) #.html'))
f_css = list(Path(dir).rglob('*.css'))
f_js = list(Path(dir).rglob('*.js'))
f_img = list(Path(dir).rglob('*.ico')) + list(Path(dir).rglob('*.png'))
f_all = f_html + f_css + f_js + f_img
sz = 0 sz = 0
for fn in Path(dir).rglob('*.*'):
for fn in f_all:
if not fn.is_file(): continue if not fn.is_file(): continue
out_path = fn.relative_to(dir).as_posix().encode('ascii') out_path = fn.relative_to(dir).as_posix().encode('ascii') + b'\000'
while len(out_path) & 3 != 0: out_path += b'\000' while len(out_path) & 3 != 0: out_path += b'\000'
info = fn.stat() info = fn.stat()
data_un = fn.read_bytes() data_un = fn.read_bytes()
data_comp = gzip.compress(data_un) data_comp = gzip.compress(data_un)
flag = FL_GZIP
if len(data_un) <= len(data_comp):
data_comp = data_un
flag = 0
print("Processing {} -> {}[{}], {}->{} bytes".format(fn, out_path, len(out_path), info.st_size, print("Processing {} -> {}[{}], {}->{} bytes".format(fn, out_path, len(out_path), info.st_size,
len(data_comp)), file=stderr) len(data_comp)), file=stderr)
header = struct.pack('<IBBHII', MAGIC, FL_GZIP, 0, len(out_path), len(data_comp), info.st_size) header = struct.pack('<IBBHII', MAGIC, flag, 0, len(out_path), len(data_comp), info.st_size)
outbuf.write(header) outbuf.write(header)
sz += len(header) sz += len(header)
outbuf.write(out_path) outbuf.write(out_path)

@ -7,13 +7,43 @@ Import("env")
from mkespfs import mkespfs from mkespfs import mkespfs
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
import shutil
dir = "html" dir = "html"
hdr = "head-"
temp = "temp-espfs"
espfile = "src/espfs_img.c" espfile = "src/espfs_img.c"
# create empty temporary dir to create fs image
shutil.rmtree(temp, ignore_errors=True)
Path(temp).mkdir()
# place all the files we want into a temp directory and prefix all .html with hdr
f_html = list(Path(dir).rglob('*.html'))
f_css = list(Path(dir).rglob('*.css'))
f_js = list(Path(dir).rglob('*.js'))
f_img = list(Path(dir).rglob('*.ico')) + list(Path(dir).rglob('*.png'))
# simply copy most files
for fn in f_css+f_js+f_img:
dst = Path(temp).joinpath(fn.relative_to(dir))
dst.parent.mkdir(exist_ok=True)
shutil.copyfile(fn, dst)
# prepend shared header to html files
for fn in f_html:
with open(Path(temp).joinpath(fn.relative_to(dir)), 'wb') as dst:
with open(Path(dir).joinpath(hdr), 'rb') as src:
shutil.copyfileobj(src, dst)
with open(fn, 'rb') as src:
shutil.copyfileobj(src, dst)
# generate espfs image
buf = BytesIO() buf = BytesIO()
espfsimg = mkespfs(dir, buf) espfsimg = mkespfs(temp, buf)
# remove temp tree
#shutil.rmtree(temp, ignore_errors=True)
# write as C file
fd = Path(espfile).open(mode='w') fd = Path(espfile).open(mode='w')
fd.write("unsigned char espfs_image[] "); fd.write("unsigned char espfs_image[] ");
fd.write("__attribute__((aligned(4))) "); fd.write("__attribute__((aligned(4))) ");

Loading…
Cancel
Save