132 lines
4.7 KiB
Python
Executable file
132 lines
4.7 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
import os
|
|
import gzip
|
|
import argparse
|
|
import subprocess
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('-o', '--output', help='Output file name', default='stdout')
|
|
parser.add_argument('-W', '--webroot', help='Output file name', default='webdir/')
|
|
parser.add_argument('--gzip', dest='gzip', action='store_true')
|
|
parser.add_argument('--no-gzip', dest='gzip', action='store_false')
|
|
parser.set_defaults(gzip=False)
|
|
parser.add_argument('--minify', dest='minify', action='store_true')
|
|
parser.add_argument('--no-minify', dest='minify', action='store_false')
|
|
parser.set_defaults(minify=False)
|
|
parser.add_argument('--header', dest='header', action='store_true')
|
|
parser.add_argument('--no-header', dest='header', action='store_false')
|
|
parser.set_defaults(header=True)
|
|
parser.add_argument('input', nargs='+', default=os.getcwd())
|
|
args = parser.parse_args()
|
|
|
|
|
|
def mimeFromName(name):
|
|
if name.endswith(".html") or name.endswith(".htm") or name.endswith(".shtml") or name.endswith(
|
|
".shtm") or name.endswith(".ssi"):
|
|
return "text/html"
|
|
if name.endswith(".js"):
|
|
return "application/x-javascript"
|
|
if name.endswith(".css"):
|
|
return "text/css"
|
|
if name.endswith(".ico"):
|
|
return "image/x-icon"
|
|
if name.endswith(".gif"):
|
|
return "image/gif"
|
|
if name.endswith(".png"):
|
|
return "image/png"
|
|
if name.endswith(".jpg"):
|
|
return "image/jpeg"
|
|
if name.endswith(".bmp"):
|
|
return "image/bmp"
|
|
if name.endswith(".class"):
|
|
return "application/octet-stream"
|
|
if name.endswith(".ram"):
|
|
return "audio/x-pn-realaudio"
|
|
return "text/plain"
|
|
|
|
|
|
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)
|
|
|
|
|
|
f_fsdata_c = open(args.output, 'w')
|
|
f_fsdata_c.write('#include "httpd/fsdata.h"\n\n')
|
|
|
|
httpFiles = [file for file in args.input if (args.webroot in file)]
|
|
|
|
lastFileStruct = "NULL"
|
|
|
|
for file in httpFiles:
|
|
response = b''
|
|
|
|
webPath = ("/" + file.removeprefix(args.webroot)).replace("//", "/")
|
|
print("{} > {}".format(file, webPath))
|
|
|
|
mimeType = mimeFromName(file)
|
|
|
|
if args.header:
|
|
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]
|
|
response += b'Content-type: ' + mimeType.encode() + b'\r\n'
|
|
|
|
binFile = open(file, 'rb')
|
|
binData = binFile.read()
|
|
compEff = False
|
|
if args.minify:
|
|
# shell: binData | minify --html-keep-document-tags --mime text/html | minData
|
|
p = subprocess.Popen(["minify", "--html-keep-document-tags", "--mime", mimeType], stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE)
|
|
minData = p.communicate(binData)[0]
|
|
if len(minData) < len(binData):
|
|
print("- Minify: {} -> {}".format(len(binData), len(minData)))
|
|
compEff = True
|
|
binData = minData
|
|
|
|
if args.gzip:
|
|
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
|
|
binFile.close()
|
|
escFile = file.replace("/", "_").replace(".", "_")
|
|
escFileData = "data_" + escFile
|
|
escFileFile = "file_" + escFile
|
|
|
|
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 args.header == True
|
|
lastFileStruct = escFileFile
|
|
|
|
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)))
|