From 54ff12e53a19a0048a90bcdf204f6106706b01c7 Mon Sep 17 00:00:00 2001 From: Nikhil Dabas Date: Sat, 17 Aug 2024 19:34:17 +0100 Subject: [PATCH 1/3] Ensure makefsdata.py generates valid variable names --- src/rp2_common/pico_lwip/tools/makefsdata.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rp2_common/pico_lwip/tools/makefsdata.py b/src/rp2_common/pico_lwip/tools/makefsdata.py index 0ef113dfc..2bbfac5c6 100755 --- a/src/rp2_common/pico_lwip/tools/makefsdata.py +++ b/src/rp2_common/pico_lwip/tools/makefsdata.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import argparse from pathlib import Path +import re file_types = { "html": "text/html", @@ -41,7 +42,7 @@ def process_file(input_dir, file): results = [] # Check content type - content_type = file_types[file.suffix[1:].lower()] + content_type = file_types.get(file.suffix[1:].lower()) if content_type is None: raise RuntimeError(f"Unsupported file type {file.suffix}") @@ -103,8 +104,7 @@ def process_file_list(fd, input): # make a variable name var_name = str(file.relative_to(input_dir)) - var_name = var_name.replace(".", "_") - var_name = var_name.replace("/", "_") + var_name = re.sub(r"\W+", "_", var_name, flags=re.ASCII) data_var = f"data_{var_name}" file_var = f"file_{var_name}" @@ -121,7 +121,7 @@ def process_file_list(fd, input): if byte_count % 16 == 0: fd.write("\n") if byte_count % 16 != 0: - fd.write("\n") + fd.write("\n") fd.write(f"}};\n\n") # set the flags From d32d1413c9cb08e94ed996af68cc2e8db04c32f7 Mon Sep 17 00:00:00 2001 From: Nikhil Dabas Date: Sun, 18 Aug 2024 21:33:00 +0100 Subject: [PATCH 2/3] Use mimetypes library in makefsdata.py --- src/rp2_common/pico_lwip/tools/makefsdata.py | 34 +++++--------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/src/rp2_common/pico_lwip/tools/makefsdata.py b/src/rp2_common/pico_lwip/tools/makefsdata.py index 2bbfac5c6..464c3f793 100755 --- a/src/rp2_common/pico_lwip/tools/makefsdata.py +++ b/src/rp2_common/pico_lwip/tools/makefsdata.py @@ -1,32 +1,9 @@ #!/usr/bin/env python3 import argparse +import mimetypes from pathlib import Path import re -file_types = { - "html": "text/html", - "htm": "text/html", - "shtml": "text/html", - "shtm": "text/html", - "ssi": "text/html", - "gif": "image/gif", - "png": "image/png", - "jpg": "image/jpeg", - "bmp": "image/bmp", - "ico": "image/x-icon", - "class": "application/octet-stream", - "cls": "application/octet-stream", - "js": "application/javascript", - "ram": "application/javascript", - "css": "text/css", - "swf": "application/x-shockwave-flash", - "xml": "text/xml", - "xsl": "application/pdf", - "pdf": "text/xml", - "json": "application/json", - "svg": "image/svg+xml" -} - response_types = { 200: "HTTP/1.0 200 OK", 400: "HTTP/1.0 400 Bad Request", @@ -42,9 +19,9 @@ def process_file(input_dir, file): results = [] # Check content type - content_type = file_types.get(file.suffix[1:].lower()) + content_type, _ = mimetypes.guess_type(file) if content_type is None: - raise RuntimeError(f"Unsupported file type {file.suffix}") + content_type = "application/octet-stream" # file name data = f"/{file.relative_to(input_dir)}\x00" @@ -165,6 +142,11 @@ def run_tool(): ) args = parser.parse_args() print(args.input) + + mimetypes.init() + for ext in [".shtml", ".shtm", ".ssi"]: + mimetypes.add_type("text/html", ext) + with open(args.output, "w") as fd: process_file_list(fd, args.input) From edf876359e442dd93dd6249cb3a321d85b902bad Mon Sep 17 00:00:00 2001 From: Nikhil Dabas Date: Mon, 19 Aug 2024 16:28:57 +0100 Subject: [PATCH 3/3] Avoid generating duplicate variable names --- src/rp2_common/pico_lwip/tools/makefsdata.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/rp2_common/pico_lwip/tools/makefsdata.py b/src/rp2_common/pico_lwip/tools/makefsdata.py index 464c3f793..88fca43a6 100755 --- a/src/rp2_common/pico_lwip/tools/makefsdata.py +++ b/src/rp2_common/pico_lwip/tools/makefsdata.py @@ -82,6 +82,11 @@ def process_file_list(fd, input): # make a variable name var_name = str(file.relative_to(input_dir)) var_name = re.sub(r"\W+", "_", var_name, flags=re.ASCII) + + # Add a suffix if the variable name is used already + if any(d["data_var"] == f"data_{var_name}" for d in data): + var_name += f"_{len(data)}" + data_var = f"data_{var_name}" file_var = f"file_{var_name}"