From 5f8489c499e3edd3675046227a2e0baae19c06bf Mon Sep 17 00:00:00 2001 From: Richard Speir Date: Fri, 2 Sep 2022 18:26:09 -0400 Subject: [PATCH 1/3] Converted makefsdata script to more platform-agnostic python version; modified cmakelists file to not use *nix shell commands. Project now builds under Windows as well as Ubuntu (MacOS not tested but should work) --- src/makefsdata.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/makefsdata.py diff --git a/src/makefsdata.py b/src/makefsdata.py new file mode 100644 index 0000000..0d80230 --- /dev/null +++ b/src/makefsdata.py @@ -0,0 +1,105 @@ +#!/usr/bin/python3 + +import os +import binascii + +#Create file to write output into +output = open('fsdata.c', 'w') + +#Traverse directory, generate list of files +files = list() +os.chdir('./fs') +for(dirpath, dirnames, filenames) in os.walk('.'): + files += [os.path.join(dirpath, file) for file in filenames] + +filenames = list() +varnames = list() + +#Generate appropriate HTTP headers +for file in files: + + if '404' in file: + header = "HTTP/1.0 404 File not found\r\n" + else: + header = "HTTP/1.0 200 OK\r\n" + + header += "Server: lwIP/pre-0.6 (http://www.sics.se/~adam/lwip/)\r\n" + + if '.html' in file: + header += "Content-type: text/html\r\n" + elif '.shtml' in file: + header += "Content-type: text/html\r\n" + elif '.jpg' in file: + header += "Content-type: image/jpeg\r\n" + elif '.gif' in file: + header += "Content-type: image/gif\r\n" + elif '.png' in file: + header += "Content-type: image/png\r\n" + elif '.class' in file: + header += "Content-type: application/octet-stream\r\n" + elif '.js' in file: + header += "Content-type: text/javascript\r\n" + elif '.css' in file: + header += "Content-type: text/css\r\n" + elif '.svg' in file: + header += "Content-type: image/svg+xml\r\n" + else: + header += "Content-type: text/plain\r\n" + + header += "\r\n" + + fvar = file[1:] #remove leading dot in filename + fvar = fvar.replace('/', '_') #replace *nix path separator with underscore + fvar = fvar.replace('\\', '_') #replace DOS path separator with underscore + fvar = fvar.replace('.', '_') #replace file extension dot with underscore + + output.write("static const unsigned char data{}[] = {{\n".format(fvar)) + output.write("\t/* {} */\n\t".format(file)) + + #first set of hex data encodes the filename + b = bytes(file[1:].replace('\\', '/'), 'utf-8') #change DOS path separator to forward slash + for byte in binascii.hexlify(b, b' ', 1).split(): + output.write("0x{}, ".format(byte.decode())) + output.write("0,\n\t") + + #second set of hex data is the HTTP header/mime type we generated above + b = bytes(header, 'utf-8') + count = 0 + for byte in binascii.hexlify(b, b' ', 1).split(): + output.write("0x{}, ".format(byte.decode())) + count = count + 1 + if(count == 10): + output.write("\n\t") + count = 0 + output.write("\n\t") + + #finally, dump raw hex data from files + with open(file, 'rb') as f: + count = 0 + while(byte := f.read(1)): + byte = binascii.hexlify(byte) + output.write("0x{}, ".format(byte.decode())) + count = count + 1 + if(count == 10): + output.write("\n\t") + count = 0 + output.write("};\n\n") + + filenames.append(file[1:]) + varnames.append(fvar) + +for i in range(len(filenames)): + prevfile = "NULL" + if(i > 0): + prevfile = "file" + varnames[i-1] + + output.write("const struct fsdata_file file{0}[] = {{{{ {1}, data{2}, ".format(varnames[i], prevfile, varnames[i])) + output.write("data{} + {}, ".format(varnames[i], len(filenames[i]) + 1)) + output.write("sizeof(data{}) - {}, ".format(varnames[i], len(filenames[i]) + 1)) + output.write("FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT}};\n") + +output.write("\n#define FS_ROOT file{}\n".format(varnames[-1])) +output.write("#define FS_NUMFILES {}\n".format(len(filenames))) + + + \ No newline at end of file From 4b3e78351dd236f213da9bebbb20df690d470476 Mon Sep 17 00:00:00 2001 From: Richard Speir Date: Fri, 2 Sep 2022 18:35:06 -0400 Subject: [PATCH 2/3] adding new .gitignore and CMakeLists --- .gitignore | 1 + src/CMakeLists.txt | 23 +++++++++-------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index bc9e916..bd30325 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build +.vscode cmake/credentials.cmake cmake/pico_sdk_import.cmake diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c07e262..dd6eece 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,21 +1,16 @@ set(PROGRAM_NAME pico_w_webserver) -set(MAKE_FS_DATA_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/external/makefsdata) +set(MAKE_FS_DATA_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/makefsdata.py) + +find_package(Python3 COMPONENTS Interpreter REQUIRED) -if (NOT EXISTS ${MAKE_FS_DATA_SCRIPT}) - file(DOWNLOAD - https://raw.githubusercontent.com/krzmaz/lwip/e15654409d14a238aec5ed4bd5516063938c9345/src/apps/http/makefsdata/makefsdata - ${MAKE_FS_DATA_SCRIPT} - ) -endif() -execute_process(COMMAND - perl ${MAKE_FS_DATA_SCRIPT} - WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} - ) execute_process(COMMAND - mv fsdata.c my_fsdata.c - WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} - ) + ${Python3_EXECUTABLE} ${MAKE_FS_DATA_SCRIPT} + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + RESULT_VARIABLE FSDATA_RESULT + ) + +file(RENAME fsdata.c my_fsdata.c) add_executable(${PROGRAM_NAME} main.cpp From a66ae73ee3b8bc7164af8a6e1091774155e1506c Mon Sep 17 00:00:00 2001 From: Richard Speir Date: Sat, 24 Sep 2022 21:19:51 -0400 Subject: [PATCH 3/3] Updated cmakelists --- src/CMakeLists.txt | 39 +++++++++++++---- src/makefsdata.py | 105 --------------------------------------------- 2 files changed, 30 insertions(+), 114 deletions(-) delete mode 100644 src/makefsdata.py diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dd6eece..ecdb47a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,14 +1,35 @@ set(PROGRAM_NAME pico_w_webserver) -set(MAKE_FS_DATA_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/makefsdata.py) - -find_package(Python3 COMPONENTS Interpreter REQUIRED) - -execute_process(COMMAND - ${Python3_EXECUTABLE} ${MAKE_FS_DATA_SCRIPT} - WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} - RESULT_VARIABLE FSDATA_RESULT - ) +if (CMAKE_HOST_WIN32) + set(MAKE_FS_DATA_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/external/makefsdata.py) + if(NOT EXISTS ${MAKE_FS_DATA_SCRIPT}) + file(DOWNLOAD + https://gist.githubusercontent.com/rspeir/99e6c28e98930872f684bbe2f7aa2dee/raw/8d2a0d904dc4b67872e6f8c2b7cb3d8be5666beb/makefsdata.py + ${MAKE_FS_DATA_SCRIPT} + ) + endif() + find_package(Python3 COMPONENTS Interpreter REQUIRED) + execute_process(COMMAND + ${Python3_EXECUTABLE} ${MAKE_FS_DATA_SCRIPT} + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + RESULT_VARIABLE FSDATA_RESULT + ) +else() + set(MAKE_FS_DATA_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/external/makefsdata) + if (NOT EXISTS ${MAKE_FS_DATA_SCRIPT}) + file(DOWNLOAD + https://raw.githubusercontent.com/krzmaz/lwip/e15654409d14a238aec5ed4bd5516063938c9345/src/apps/http/makefsdata/makefsdata + ${MAKE_FS_DATA_SCRIPT} + ) + endif() + execute_process(COMMAND + perl ${MAKE_FS_DATA_SCRIPT} + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + ECHO_OUTPUT_VARIABLE + ECHO_ERROR_VARIABLE + COMMAND_ERROR_IS_FATAL ANY + ) +endif() file(RENAME fsdata.c my_fsdata.c) diff --git a/src/makefsdata.py b/src/makefsdata.py deleted file mode 100644 index 0d80230..0000000 --- a/src/makefsdata.py +++ /dev/null @@ -1,105 +0,0 @@ -#!/usr/bin/python3 - -import os -import binascii - -#Create file to write output into -output = open('fsdata.c', 'w') - -#Traverse directory, generate list of files -files = list() -os.chdir('./fs') -for(dirpath, dirnames, filenames) in os.walk('.'): - files += [os.path.join(dirpath, file) for file in filenames] - -filenames = list() -varnames = list() - -#Generate appropriate HTTP headers -for file in files: - - if '404' in file: - header = "HTTP/1.0 404 File not found\r\n" - else: - header = "HTTP/1.0 200 OK\r\n" - - header += "Server: lwIP/pre-0.6 (http://www.sics.se/~adam/lwip/)\r\n" - - if '.html' in file: - header += "Content-type: text/html\r\n" - elif '.shtml' in file: - header += "Content-type: text/html\r\n" - elif '.jpg' in file: - header += "Content-type: image/jpeg\r\n" - elif '.gif' in file: - header += "Content-type: image/gif\r\n" - elif '.png' in file: - header += "Content-type: image/png\r\n" - elif '.class' in file: - header += "Content-type: application/octet-stream\r\n" - elif '.js' in file: - header += "Content-type: text/javascript\r\n" - elif '.css' in file: - header += "Content-type: text/css\r\n" - elif '.svg' in file: - header += "Content-type: image/svg+xml\r\n" - else: - header += "Content-type: text/plain\r\n" - - header += "\r\n" - - fvar = file[1:] #remove leading dot in filename - fvar = fvar.replace('/', '_') #replace *nix path separator with underscore - fvar = fvar.replace('\\', '_') #replace DOS path separator with underscore - fvar = fvar.replace('.', '_') #replace file extension dot with underscore - - output.write("static const unsigned char data{}[] = {{\n".format(fvar)) - output.write("\t/* {} */\n\t".format(file)) - - #first set of hex data encodes the filename - b = bytes(file[1:].replace('\\', '/'), 'utf-8') #change DOS path separator to forward slash - for byte in binascii.hexlify(b, b' ', 1).split(): - output.write("0x{}, ".format(byte.decode())) - output.write("0,\n\t") - - #second set of hex data is the HTTP header/mime type we generated above - b = bytes(header, 'utf-8') - count = 0 - for byte in binascii.hexlify(b, b' ', 1).split(): - output.write("0x{}, ".format(byte.decode())) - count = count + 1 - if(count == 10): - output.write("\n\t") - count = 0 - output.write("\n\t") - - #finally, dump raw hex data from files - with open(file, 'rb') as f: - count = 0 - while(byte := f.read(1)): - byte = binascii.hexlify(byte) - output.write("0x{}, ".format(byte.decode())) - count = count + 1 - if(count == 10): - output.write("\n\t") - count = 0 - output.write("};\n\n") - - filenames.append(file[1:]) - varnames.append(fvar) - -for i in range(len(filenames)): - prevfile = "NULL" - if(i > 0): - prevfile = "file" + varnames[i-1] - - output.write("const struct fsdata_file file{0}[] = {{{{ {1}, data{2}, ".format(varnames[i], prevfile, varnames[i])) - output.write("data{} + {}, ".format(varnames[i], len(filenames[i]) + 1)) - output.write("sizeof(data{}) - {}, ".format(varnames[i], len(filenames[i]) + 1)) - output.write("FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT}};\n") - -output.write("\n#define FS_ROOT file{}\n".format(varnames[-1])) -output.write("#define FS_NUMFILES {}\n".format(len(filenames))) - - - \ No newline at end of file