Skip to content

Commit

Permalink
Ensure makefsdata.py generates valid variable names (#1841)
Browse files Browse the repository at this point in the history
* Ensure makefsdata.py generates valid variable names

* Use mimetypes library in makefsdata.py

* Avoid generating duplicate variable names
  • Loading branch information
ndabas authored Aug 20, 2024
1 parent e930bf3 commit 789ea75
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions src/rp2_common/pico_lwip/tools/makefsdata.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down Expand Up @@ -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}"

Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 789ea75

Please sign in to comment.