From 789ea75c63a40585438147498a7869ddd074c75b Mon Sep 17 00:00:00 2001 From: Nikhil Dabas Date: Tue, 20 Aug 2024 23:01:45 +0530 Subject: [PATCH] Ensure makefsdata.py generates valid variable names (#1841) * Ensure makefsdata.py generates valid variable names * Use mimetypes library in makefsdata.py * Avoid generating duplicate variable names --- src/rp2_common/pico_lwip/tools/makefsdata.py | 45 +++++++------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/rp2_common/pico_lwip/tools/makefsdata.py b/src/rp2_common/pico_lwip/tools/makefsdata.py index 0ef113dfc..88fca43a6 100755 --- a/src/rp2_common/pico_lwip/tools/makefsdata.py +++ b/src/rp2_common/pico_lwip/tools/makefsdata.py @@ -1,30 +1,8 @@ #!/usr/bin/env python3 import argparse +import mimetypes from pathlib import Path - -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" -} +import re response_types = { 200: "HTTP/1.0 200 OK", @@ -41,9 +19,9 @@ def process_file(input_dir, file): results = [] # Check content type - content_type = file_types[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" @@ -103,8 +81,12 @@ 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) + + # 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}" @@ -121,7 +103,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 @@ -165,6 +147,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)