fiatlux/firmware/mkwebfs.py

103 lines
3.7 KiB
Python
Raw Normal View History

2021-09-07 12:58:56 +00:00
#!/usr/bin/env python3
2021-09-08 22:54:14 +00:00
import os
2021-09-09 00:35:15 +00:00
import gzip
2021-09-07 12:58:56 +00:00
incHttpHeader = True
2021-09-09 00:35:15 +00:00
useCompression = True
2021-09-08 22:54:14 +00:00
fileDir = "fsdata/fs/"
2021-09-07 12:58:56 +00:00
2021-09-08 22:54:14 +00:00
def dumpBin2CHex(f, b):
oStr = "\t"
n = 0
for val in b:
oStr += hex(val) + ", "
n += 1
if n % 8 == 0:
oStr += "\n\t"
oStr += "\n"
f.write(oStr)
2021-09-07 12:58:56 +00:00
2021-09-08 23:02:58 +00:00
f_fsdata_c = open('fsdata/fsdata.c', 'w')
2021-09-08 22:54:14 +00:00
f_fsdata_c.write('#include "httpd/fsdata.h"\n\n')
2021-09-07 12:58:56 +00:00
2021-09-08 22:54:14 +00:00
httpFiles = []
for root, dirs, files in os.walk(fileDir):
for file in files:
httpFiles.append("{}/{}".format(root, file).replace("//", "/"))
2021-09-07 12:58:56 +00:00
2021-09-08 22:54:14 +00:00
lastFileStruct = "NULL"
2021-09-07 12:58:56 +00:00
2021-09-08 22:54:14 +00:00
for file in httpFiles:
response = b''
if incHttpHeader:
print(file)
if("404" in file):
response = b'HTTP/1.0 404 File not found\r\n'
else:
response = b'HTTP/1.0 200 OK\r\n'
response += b"lwIP/1.4.1 (http://savannah.nongnu.org/projects/lwip)\r\n"
fext = file.split('.')[-1]
ctype = b'Content-type: text/plain\r\n'
if(fext.endswith("html") or fext.endswith("htm") or fext.endswith("shtml") or fext.endswith("shtm") or fext.endswith("ssi")):
ctype = b'Content-type: text/html\r\n'
if(fext.endswith("js")):
ctype = b'Content-type: application/x-javascript\r\n'
if(fext.endswith("css")):
ctype = b'Content-type: text/css\r\n'
if(fext.endswith("ico")):
ctype = b'Content-type: image/x-icon\r\n'
if(fext.endswith("gif")):
ctype = b'Content-type: image/gif\r\n'
if(fext.endswith("png")):
ctype = b'Content-type: image/png\r\n'
if(fext.endswith("jpg")):
ctype = b'Content-type: image/jpeg\r\n'
if(fext.endswith("bmp")):
ctype = b'Content-type: image/bmp\r\n'
if(fext.endswith("class")):
ctype = b'Content-type: application/octet-stream\r\n'
if(fext.endswith("ram")):
ctype = b'Content-type: audio/x-pn-realaudio\r\n'
response += ctype
2021-09-09 00:35:15 +00:00
2021-09-08 22:54:14 +00:00
binFile = open(file, 'rb')
2021-09-09 00:35:15 +00:00
binData = binFile.read()
compEff = False
if useCompression:
compData = gzip.compress(binData, 9)
if len(compData) < len(binData):
compEff = True
print("- Compressed from {} to {}".format(len(binData), len(compData)))
binData = compData
else:
print("- Compression skipped Orig: {} Comp: {}".format(len(binData), len(compData)))
binFile.close()
if compEff:
response += b'Content-Encoding: gzip\r\n'
response += b"\r\n"
response += binData
2021-09-08 22:54:14 +00:00
binFile.close()
escFile = file.replace("/", "_").replace(".", "_")
escFileData = "data_" + escFile
escFileFile = "file_" + escFile
webPath = ("/" + file.replace(fileDir, "")).replace("//", "/")
2021-09-07 12:58:56 +00:00
2021-09-08 22:54:14 +00:00
f_fsdata_c.write('static const unsigned char {}[] = {{\n'.format(escFileData))
f_fsdata_c.write('\t/* LOCAL:{} */\n'.format(file))
f_fsdata_c.write('\t/* WEB: {} */\n'.format(webPath))
fnameBin = webPath.encode("ascii") + b'\0'
dumpBin2CHex(f_fsdata_c, fnameBin)
dumpBin2CHex(f_fsdata_c, response)
f_fsdata_c.write("};\n\n")
f_fsdata_c.write("const struct fsdata_file {}[] = {{{{\n {},\n {}, {} + {}, sizeof({}) - {}, 1 }}}};\n\n"
.format(escFileFile, lastFileStruct, escFileData, escFileData, len(fnameBin), escFileData, len(fnameBin)))
#TODO: The last value is 1 if incHttpHeader == True
lastFileStruct = escFileFile
2021-09-07 12:58:56 +00:00
2021-09-08 22:54:14 +00:00
f_fsdata_c.write("\n")
f_fsdata_c.write("#define FS_ROOT {}\n\n".format(lastFileStruct))
f_fsdata_c.write("#define FS_NUMFILES {}\n\n".format(len(httpFiles)))