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

More .cproject normalization #79

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
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
2,401 changes: 2,398 additions & 3 deletions misc/firmware-eraser/.cproject

Large diffs are not rendered by default.

1,604 changes: 1,602 additions & 2 deletions src/bootloader-uart-xmodem/.cproject

Large diffs are not rendered by default.

2,640 changes: 2,638 additions & 2 deletions src/ncp-uart-hw/.cproject

Large diffs are not rendered by default.

2,575 changes: 2,573 additions & 2 deletions src/ot-rcp/.cproject

Large diffs are not rendered by default.

2,750 changes: 2,748 additions & 2 deletions src/rcp-uart-802154/.cproject

Large diffs are not rendered by default.

2,171 changes: 2,169 additions & 2 deletions src/zwave_ncp_serial_api_controller/.cproject

Large diffs are not rendered by default.

35 changes: 27 additions & 8 deletions tools/normalize_cproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import xml.etree.ElementTree as ET


def json_dumps_compact(obj: dict | list) -> str:
NEWLINE_SENTINEL = "7200872e315d2518866d8a02e8258034"


def json_dumps(obj: dict | list) -> str:
"""Compactly dump JSON into a string."""
return json.dumps(obj, separators=(",", ":"), indent=None)
return json.dumps(obj, separators=(", ", ": "), indent=4)


cproject_path = pathlib.Path(sys.argv[1])
Expand All @@ -25,9 +28,7 @@ def json_dumps_compact(obj: dict | list) -> str:
copied_files.sort(
key=lambda f: (f["generated"], f["projectPath"], f["version"])
)
storage_module.attrib["projectCommon.copiedFiles"] = json_dumps_compact(
copied_files
)
storage_module.attrib["projectCommon.copiedFiles"] = json_dumps(copied_files)

if "cppBuildConfig.projectBuiltInState" in storage_module.attrib:
project_built_in_state = json.loads(
Expand All @@ -39,16 +40,34 @@ def json_dumps_compact(obj: dict | list) -> str:
resolved_options = json.loads(state["resolvedOptionsStr"])
resolved_options.sort(key=lambda o: o["optionId"])

state["resolvedOptionsStr"] = json_dumps_compact(resolved_options)
state["resolvedOptionsStr"] = json_dumps(resolved_options)

if "builtinIncludesStr" in state:
state["builtinIncludesStr"] = NEWLINE_SENTINEL.join(
state["builtinIncludesStr"].split()
)

storage_module.attrib["cppBuildConfig.projectBuiltInState"] = (
json_dumps_compact(project_built_in_state)
storage_module.attrib["cppBuildConfig.projectBuiltInState"] = json_dumps(
project_built_in_state
)

if "projectCommon.referencedModules" in storage_module.attrib:
referenced_modules = json.loads(
storage_module.attrib["projectCommon.referencedModules"]
)
storage_module.attrib["projectCommon.referencedModules"] = json_dumps(
referenced_modules
)

# Normalize self-closing tag spacing
xml_text = ET.tostring(tree, encoding="unicode", xml_declaration=False)
xml_text = xml_text.replace(" />", "/>")

# Replace newlines with literals
xml_text = xml_text.replace("
", "\n")
xml_text = xml_text.replace(NEWLINE_SENTINEL, "\n")
xml_text = re.sub(r"\s*\\n\s*", "\n\\n", xml_text, flags=re.MULTILINE)

# Only touch the filesystem if we need to
if processing_instructions + xml_text != cproject:
cproject_path.write_text(processing_instructions + xml_text)
Loading