From c7302552d49338a3ca4d347d992f23d47fdbf259 Mon Sep 17 00:00:00 2001 From: Dario Borreguero Rincon Date: Mon, 7 Oct 2024 17:15:06 +0200 Subject: [PATCH] fix: [brctl] fixes library ID parsing without double quotes --- .vscode/launch.json | 8 +++++++ src/sysdiagnose/parsers/brctl.py | 41 +++++++++++++++++++------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index a93b188..dfa914c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -133,6 +133,14 @@ "args": "-c public parse demo_parser", "cwd": "${workspaceFolder}/" }, + { + "name": "Python Debugger: parse brctl", + "type": "debugpy", + "request": "launch", + "module": "sysdiagnose.main", + "args": "-c public parse brctl", + "cwd": "${workspaceFolder}/" + }, { "name": "Python Debugger: parse logarchive", "type": "debugpy", diff --git a/src/sysdiagnose/parsers/brctl.py b/src/sysdiagnose/parsers/brctl.py index ef204f0..fe0dc9e 100644 --- a/src/sysdiagnose/parsers/brctl.py +++ b/src/sysdiagnose/parsers/brctl.py @@ -328,30 +328,37 @@ def parse_apps_monitor(data): parts = data.split("=======================") # Extract the JSON strings from each part - json_str1 = parts[1].strip().replace("=", ":").replace("\\", "").replace( - "\"{(n \"", "[\"").replace("\"n)}\"", "\"]").replace(",n ", ",").replace(";", ",") - json_str2 = parts[2].strip().replace("=", ":").replace("\\", "").replace( - "\"{(n \"", "[\"").replace("\"n)}\"", "\"]").replace(",n ", ",").replace(";", ",") + json_str1 = BrctlParser.parse_apps_monitor2json(parts[1].strip()) + json_str2 = BrctlParser.parse_apps_monitor2json(parts[2].strip()) - # ugly fixes - last_comma_index = json_str1.rfind(",") - json_str1_new = json_str1[:last_comma_index] + json_str1[last_comma_index + 1:] + # Load the JSON strings into Python dictionaries + json1 = json.loads(json_str1) + json2 = json.loads(json_str2) + + return json1, json2 - first_brace_index = json_str1_new.find("}") - json_str1 = json_str1_new[:first_brace_index + 1] + def parse_apps_monitor2json(data): + # replace = by : + json_str = data.replace("=", ":") + # remove literal string '\n' + json_str = re.sub(r'\\n\s*', '', json_str) + # remove char \ + # replace start of array string representation "{( by [ + # replace end of array string representation )}" by ] + # remove char " + json_str = json_str.replace("\\", "").replace('"{(', '[').replace(')}";', '],').replace('"','') + # adds double quotes to all bundle IDs or App IDs + json_str = re.sub(r'([\w\.]+)', r'"\1"', json_str) # ugly fixes - last_comma_index = json_str2.rfind(",") - json_str2_new = json_str2[:last_comma_index] + json_str2[last_comma_index + 1:] + last_comma_index = json_str.rfind(",") + json_str_new = json_str[:last_comma_index] + json_str[last_comma_index + 1:] - first_brace_index = json_str2_new.find("}") - json_str2 = json_str2_new[:first_brace_index + 1] + first_brace_index = json_str_new.find("}") + json_str = json_str_new[:first_brace_index + 1] - # Load the JSON strings into Python dictionaries - json1 = json.loads(json_str1) - json2 = json.loads(json_str2) + return json_str - return json1, json2 def parse_folder(brctl_folder): container_list_file = [os.path.join(brctl_folder, 'brctl-container-list.txt')]