Script
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
7m9 2021-09-09 00:54:14 +02:00
parent 51ed3b3b1c
commit 82f0634376

185
firmware/mkwebfs.py Normal file → Executable file
View file

@ -1,118 +1,85 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
incHttpHeader = True incHttpHeader = True
fileDir = "fsdata/fs/"
open(OUTPUT, "> fsdata.c"); def dumpBin2CHex(f, b):
print(OUTPUT oStr = "\t"
"#include \"httpd/fsdata.h\"\n\n"); n = 0
for val in b:
chdir("fs"); oStr += hex(val) + ", "
open(FILES, "find . -type f |"); n += 1
if n % 8 == 0:
while ($file = < FILES >) { oStr += "\n\t"
oStr += "\n"
chop($file); f.write(oStr)
if incHttpHeader == 1:
open(HEADER, "> /tmp/header") | | die $!;
if ($file =~ / 404 /) {
print(HEADER "HTTP/1.0 404 File not found\r\n");
} else {
print(HEADER "HTTP/1.0 200 OK\r\n");
}
print(HEADER "lwIP/1.4.1 (http://savannah.nongnu.org/projects/lwip)\r\n");
if ($file =~ / \.html$ / | | $file =~ / \.htm$ / | | $file =~ / \.shtml$ / | | $file =~ / \.shtm$ / | | $file =~ / \.ssi$ / ) {
print(HEADER "Content-type: text/html\r\n");
} elsif($file =~ / \.js$ / ) {
print(HEADER "Content-type: application/x-javascript\r\n\r\n");
} elsif($file =~ / \.css$ / ) {
print(HEADER "Content-type: text/css\r\n\r\n");
} elsif($file =~ / \.ico$ / ) {
print(HEADER "Content-type: image/x-icon\r\n\r\n");
} elsif($file =~ / \.gif$ / ) {
print(HEADER "Content-type: image/gif\r\n");
} elsif($file =~ / \.png$ / ) {
print(HEADER "Content-type: image/png\r\n");
} elsif($file =~ / \.jpg$ / ) {
print(HEADER "Content-type: image/jpeg\r\n");
} elsif($file =~ / \.bmp$ / ) {
print(HEADER "Content-type: image/bmp\r\n\r\n");
} else {
print(HEADER "Content-type: text/plain\r\n");
}
print(HEADER "\r\n");
close(HEADER);
unless($file =~ / \.plain$ / | | $file =~ / cgi / ) {
system("cat /tmp/header $file > /tmp/file");
} else {
system("cp $file /tmp/file");
}
} else {
system("cp $file /tmp/file");
}
open(FILE, "/tmp/file");
unlink("/tmp/file");
unlink("/tmp/header");
$file = ~ s /\.//;
$fvar = $file;
$fvar = ~ s - / -_ - g;
$fvar = ~ s -\.-_ - g;
print(OUTPUT
"static const unsigned char data".$fvar.
"[] = {\n");
print(OUTPUT
"\t/* $file */\n\t");
for ($j = 0; $j < length($file); $j++) {
printf(OUTPUT "0x%02X, ", unpack("C", substr($file, $j, 1)));
}
printf(OUTPUT
"0,\n");
$i = 0; f_fsdata_c = open('fsdata.c', 'w')
while (read(FILE, $data, 1)) { f_fsdata_c.write('#include "httpd/fsdata.h"\n\n')
if ($i == 0) {
print(OUTPUT "\t");
}
printf(OUTPUT "0x%02X, ", unpack("C", $data));
$i++;
if ($i == 10) {
print(OUTPUT "\n");
$i = 0;
}
}
print(OUTPUT
"};\n\n");
close(FILE);
push( @ fvars, $fvar);
push( @ files, $file);
}
for ($i = 0; $i < @ fvars; $i++) { httpFiles = []
$file = $files[$i]; for root, dirs, files in os.walk(fileDir):
$fvar = $fvars[$i]; for file in files:
httpFiles.append("{}/{}".format(root, file).replace("//", "/"))
if ($i == 0) { lastFileStruct = "NULL"
$prevfile = "NULL";
} else {
$prevfile = "file".$fvars[$i - 1];
}
print(OUTPUT
"const struct fsdata_file file".$fvar.
"[] = {{\n$prevfile,\ndata$fvar, ");
print(OUTPUT
"data$fvar + ".(length($file) + 1).",\n");
print(OUTPUT
"sizeof(data$fvar) - ".(length($file) + 1).",\n");
print(OUTPUT $incHttpHeader.
"\n}};\n\n");
}
print(OUTPUT for file in httpFiles:
"#define FS_ROOT file$fvars[$i - 1]\n\n"); response = b''
print(OUTPUT if incHttpHeader:
"#define FS_NUMFILES $i\n"); 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
response += b"\r\n"
binFile = open(file, 'rb')
response += binFile.read()
binFile.close()
escFile = file.replace("/", "_").replace(".", "_")
escFileData = "data_" + escFile
escFileFile = "file_" + escFile
webPath = ("/" + file.replace(fileDir, "")).replace("//", "/")
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
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)))