Skip to content

Commit

Permalink
Merge pull request #104 from EC-DIGIT-CSIRC/brctl_quick_fix
Browse files Browse the repository at this point in the history
fix: [brctl] fixes library ID parsing without double quotes
  • Loading branch information
cvandeplas authored Oct 8, 2024
2 parents c1578c6 + 73af646 commit 2935417
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
42 changes: 24 additions & 18 deletions src/sysdiagnose/parsers/brctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,31 +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(";", ",")

# ugly fixes
last_comma_index = json_str1.rfind(",")
json_str1_new = json_str1[:last_comma_index] + json_str1[last_comma_index + 1:]

first_brace_index = json_str1_new.find("}")
json_str1 = json_str1_new[:first_brace_index + 1]

# ugly fixes
last_comma_index = json_str2.rfind(",")
json_str2_new = json_str2[:last_comma_index] + json_str2[last_comma_index + 1:]

first_brace_index = json_str2_new.find("}")
json_str2 = json_str2_new[:first_brace_index + 1]
json_str1 = BrctlParser.__parse_apps_monitor2json(parts[1].strip())
json_str2 = BrctlParser.__parse_apps_monitor2json(parts[2].strip())

# Load the JSON strings into Python dictionaries
json1 = json.loads(json_str1)
json2 = json.loads(json_str2)

return json1, json2

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 library IDs
json_str = re.sub(r'([\w\.\-]+)', r'"\1"', json_str)

# ugly fixes
last_comma_index = json_str.rfind(",")
json_str_new = json_str[:last_comma_index] + json_str[last_comma_index + 1:]

first_brace_index = json_str_new.find("}")
json_str = json_str_new[:first_brace_index + 1]

return json_str

def parse_folder(brctl_folder):
container_list_file = [os.path.join(brctl_folder, 'brctl-container-list.txt')]
container_dump_file = [os.path.join(brctl_folder, 'brctl-dump.txt')]
Expand Down

0 comments on commit 2935417

Please sign in to comment.