From 841990b2c1538f3190ccc315931eee11806be54a Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Thu, 15 Aug 2024 10:43:43 -0400 Subject: [PATCH] More `.cproject` normalization (#79) * Make `.cproject` normalization split up JSON with newlines * Run it on the repo * One more pass --- misc/firmware-eraser/.cproject | 2401 +++++++++++++- src/bootloader-uart-xmodem/.cproject | 1604 +++++++++- src/ncp-uart-hw/.cproject | 2640 +++++++++++++++- src/ot-rcp/.cproject | 2575 ++++++++++++++- src/rcp-uart-802154/.cproject | 2750 ++++++++++++++++- src/zwave_ncp_serial_api_controller/.cproject | 2171 ++++++++++++- tools/normalize_cproject.py | 35 +- 7 files changed, 14155 insertions(+), 21 deletions(-) diff --git a/misc/firmware-eraser/.cproject b/misc/firmware-eraser/.cproject index b18de704..2a0af9e6 100644 --- a/misc/firmware-eraser/.cproject +++ b/misc/firmware-eraser/.cproject @@ -23,7 +23,1479 @@ - + @@ -264,7 +1736,761 @@ - + @@ -330,7 +2556,176 @@ - + diff --git a/src/bootloader-uart-xmodem/.cproject b/src/bootloader-uart-xmodem/.cproject index 5c14b55e..69e89172 100644 --- a/src/bootloader-uart-xmodem/.cproject +++ b/src/bootloader-uart-xmodem/.cproject @@ -23,7 +23,1452 @@ - + @@ -245,7 +1690,162 @@ - + diff --git a/src/ncp-uart-hw/.cproject b/src/ncp-uart-hw/.cproject index 0f1c0557..335c9129 100644 --- a/src/ncp-uart-hw/.cproject +++ b/src/ncp-uart-hw/.cproject @@ -23,7 +23,2138 @@ - + @@ -515,7 +2646,512 @@ - + diff --git a/src/ot-rcp/.cproject b/src/ot-rcp/.cproject index bed3f49b..5aeb0069 100644 --- a/src/ot-rcp/.cproject +++ b/src/ot-rcp/.cproject @@ -23,7 +23,2241 @@ - + @@ -445,7 +2679,344 @@ - + diff --git a/src/rcp-uart-802154/.cproject b/src/rcp-uart-802154/.cproject index 75846ce3..6123b45e 100644 --- a/src/rcp-uart-802154/.cproject +++ b/src/rcp-uart-802154/.cproject @@ -23,7 +23,2353 @@ - + @@ -499,7 +2845,407 @@ - + diff --git a/src/zwave_ncp_serial_api_controller/.cproject b/src/zwave_ncp_serial_api_controller/.cproject index 09078e5b..4de3b38f 100644 --- a/src/zwave_ncp_serial_api_controller/.cproject +++ b/src/zwave_ncp_serial_api_controller/.cproject @@ -23,7 +23,1669 @@ - + @@ -367,7 +2029,512 @@ - + diff --git a/tools/normalize_cproject.py b/tools/normalize_cproject.py index 4d4e7116..70a67dda 100644 --- a/tools/normalize_cproject.py +++ b/tools/normalize_cproject.py @@ -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]) @@ -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( @@ -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)