Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure makefsdata.py generates valid variable names #1841

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}"
peterharperuk marked this conversation as resolved.
Show resolved Hide resolved

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
Loading