Skip to content

Commit

Permalink
chg: [parser] unify with parse_path function
Browse files Browse the repository at this point in the history
  • Loading branch information
cvandeplas committed May 29, 2024
1 parent 36cd16b commit dc0688e
Show file tree
Hide file tree
Showing 46 changed files with 178 additions and 56 deletions.
9 changes: 6 additions & 3 deletions parsers/accessibility_tcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import glob
import os
import sys
import misc

version_string = "sysdiagnose-Accessibility-TCC.py v2020-20-20 Version 1.0"

Expand All @@ -34,10 +35,12 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return misc.json_serializable(sqlite2json.sqlite2struct(path))


def get_accessibility_tcc(dbpath, ios_version=13):
tcc = sqlite2json.sqlite2struct(dbpath)
return tcc
# return sqlite2json.dump2json(tcc)
return parse_path(dbpath)


def print_accessibility_tcc(inputfile):
Expand Down
6 changes: 6 additions & 0 deletions parsers/appinstallation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import os
import sqlite3
import sys
import misc


version_string = "sysdiagnose-appinstallation.py v2019-11-22 Version 2.0"

Expand All @@ -43,6 +45,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return misc.json_serializable(sqlite2json.sqlite2struct(path))


def get_appinstallation(dbpath, ios_version=13):
# FIXME result can contain data in binary form. Apply the misc.json_serializable function to convert it to clean JSON.
if ios_version < 13:
Expand Down
4 changes: 4 additions & 0 deletions parsers/brctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def get_log_files(log_root_path: str) -> list:
return [os.path.join(log_root_path, log_folder) for log_folder in log_folders]


def parse_path(path: str) -> list | dict:
return parsebrctl(path)


def parselistfile(container_list_file):
containers = {"containers": []}
result = []
Expand Down
4 changes: 4 additions & 0 deletions parsers/containermanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return multilinelog.extract_from_file(path)


def parsecontainermanager(loglist):
for logfile in loglist:
return multilinelog.extract_from_file(logfile)
Expand Down
23 changes: 23 additions & 0 deletions parsers/demo_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
from optparse import OptionParser
import os
import json

version_string = "sysdiagnose-demo-parser.py v2023-04-26 Version 1.0"

Expand All @@ -32,6 +33,28 @@ def get_log_files(log_root_path: str) -> list:
return [os.path.join(log_root_path, log_files) for log_files in log_files]


def parse_path(path: str) -> list | dict:
'''
this is the function that will be called
'''
json_object = {}
return json_object


def parse_path_to_folder(path: str, output: str) -> bool:
'''
this is the function that will be called
'''
try:
json_object = {}
with open(os.path.join(output, "demo_output.json"), "w") as f:
json.dump(json_object, f)
return True
except Exception as e:
print(f"Error: {e}")
return False


def demo_function(filepath, ios_version=16, output=None):
"""
This is the function that will be called
Expand Down
6 changes: 5 additions & 1 deletion parsers/itunesstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import json
import os
import sys

import misc

version_string = "sysdiagnose-itunesstore.py v2020-20-19 Version 1.0"

Expand All @@ -37,6 +37,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return misc.json_serializable(sqlite2json.sqlite2struct(path))


def get_itunesstore(dbpath, ios_version=13):
itunes = sqlite2json.sqlite2struct(dbpath)
return json.loads(sqlite2json.dump2json(itunes))
Expand Down
17 changes: 17 additions & 0 deletions parsers/logarchive.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def get_logs(filename, ios_version=13, output=None): # FIXME #18 hard cod
Parse the system_logs.logarchive. When running on OS X, use native tools.
On other system use a 3rd party library.
"""
if output is not None:
output = os.path.join(output, "logarchive")
os.makedirs(output, exist_ok=True)
if (platform.system() == "Darwin"):
if output is not None:
output = os.path.join(output, "logarchive.json")
Expand All @@ -65,6 +68,20 @@ def get_logs(filename, ios_version=13, output=None): # FIXME #18 hard cod
return None


def parse_path(path: str) -> list | dict:
return get_logs(path, output=None)


def parse_path_to_folder(path: str, output: str) -> bool:
result = get_logs(path, output=output)
if len(result['data']) > 0:
return True
else:
print("Error:")
print(json.dumps(result, indent=4))
return False


def get_logs_on_osx(filename, output):
cmd_line = cmd_parsing_osx % (filename)
return __execute_cmd_and_get_result(cmd_line, output)
Expand Down
4 changes: 4 additions & 0 deletions parsers/mobileactivation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return multilinelog.extract_from_file(path)


def parsemobactiv(loglist):
for logfile in loglist:
return multilinelog.extract_from_file(logfile)
Expand Down
4 changes: 4 additions & 0 deletions parsers/mobileinstallation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return multilinelog.extract_from_file(path)


def parsemobinstall(loglist):
events = {"events": []}
for logfile in loglist:
Expand Down
4 changes: 4 additions & 0 deletions parsers/networkextension.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return misc.load_plist_file_as_json(path)


def parseplist(fname):
return misc.load_plist_file_as_json(fname)

Expand Down
4 changes: 4 additions & 0 deletions parsers/networkextensioncache.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return misc.load_plist_file_as_json(path)


def parseplist(fname):
pl = misc.load_plist_file_as_json(fname)

Expand Down
4 changes: 4 additions & 0 deletions parsers/olddsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def get_log_files(log_root_path: str) -> dict:
return log_files


def parse_path(path: str) -> list | dict:
return misc.load_plist_file_as_json(path)


def parse_olddsc_file(filepath: str) -> dict:
return misc.load_plist_file_as_json(filepath)

Expand Down
4 changes: 4 additions & 0 deletions parsers/powerlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return sqlite2json.sqlite2struct(path)


def get_powerlogs(dbpath, ios_version=13):

powerlogs = sqlite2json.sqlite2struct(dbpath)
Expand Down
4 changes: 4 additions & 0 deletions parsers/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return parse_ps(path)


def parse_ps(filename, ios_version=16):
processes = {}
try:
Expand Down
4 changes: 4 additions & 0 deletions parsers/psthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return parse_ps_thread(path)


def parse_ps_thread(filename, ios_version=13):
with open(filename, "r") as fd:
input = fd.readlines()
Expand Down
4 changes: 4 additions & 0 deletions parsers/shutdownlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return parse_shutdownlog(path)


def parse_shutdownlog(filepath, ios_version=16):
"""
This is the function that will be called
Expand Down
4 changes: 4 additions & 0 deletions parsers/spindumpnosymbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return parsespindumpNS(path)


def parsespindumpNS(file):
with open(file, 'r') as f_in:
# init section
Expand Down
4 changes: 4 additions & 0 deletions parsers/swcutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return parseswcutil(path)


def parseswcutil(file):
with open(file, 'r') as f_in:
# init section
Expand Down
4 changes: 4 additions & 0 deletions parsers/sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return getProductInfo(path)


# --------------------------------------------------------------------------- #
# XXX FIXME: this is obviously a very generic function and could be generalized and moved to misc/

Expand Down
4 changes: 4 additions & 0 deletions parsers/taskinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return get_tasks(path)


# --------------------------------------------------------------------------- #
def get_num_tasks(filename, ios_version=13):
"""
Expand Down
1 change: 0 additions & 1 deletion parsers/uuid2path.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import sys
import json
import pprint
import plistlib
from optparse import OptionParser

Expand Down
4 changes: 4 additions & 0 deletions parsers/wifi_known_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return misc.load_plist_file_as_json(path)


# --------------------------------------------------------------------------- #
# XXX #26 FIXME: this is obviously a very generic function and could be generalized and moved to misc/

Expand Down
4 changes: 4 additions & 0 deletions parsers/wifiscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def get_log_files(log_root_path: str) -> list:
return log_files


def parse_path(path: str) -> list | dict:
return parsewifiscan([path])


def parsewifiscan(wifi_data: list):
output = []
for data in wifi_data:
Expand Down
4 changes: 4 additions & 0 deletions parsers/wifisecurity.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def get_log_files(log_root_path: str) -> list:
return [os.path.join(log_root_path, log_files) for log_files in log_files]


def parse_path(path: str) -> list | dict:
return get_wifi_security_log(path)


def get_wifi_security_log(filepath, ios_version=16):
"""
Parse ./WiFi/security.txt and extract block of interest:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_parsers_accessibility_tcc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from parsers.accessibility_tcc import get_accessibility_tcc, get_log_files
from parsers.accessibility_tcc import parse_path, get_log_files
from tests import SysdiagnoseTestCase
import unittest

Expand All @@ -10,7 +10,7 @@ def test_get_accessibility_tcc(self):
files = get_log_files(log_root_path)
for file in files:
print(f'Parsing {file}')
result = get_accessibility_tcc(file)
result = parse_path(file)
self.assertTrue('admin' in result)
self.assertTrue('policies' in result)
self.assertTrue('active_policy' in result)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_parsers_appinstallation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from parsers.appinstallation import get_appinstallation, get_log_files
from parsers.appinstallation import parse_path, get_log_files
from tests import SysdiagnoseTestCase
import unittest

Expand All @@ -10,7 +10,7 @@ def test_get_appinstallation(self):
files = get_log_files(log_root_path)
for file in files:
print(f'Parsing {file}')
result = get_appinstallation(file)
result = parse_path(file)
self.assertTrue('application' in result)
self.assertTrue('asset' in result)
self.assertTrue('client' in result)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_parsers_brctl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from parsers.brctl import parsebrctl, get_log_files
from parsers.brctl import parse_path, get_log_files
from tests import SysdiagnoseTestCase
import unittest

Expand All @@ -10,7 +10,7 @@ def test_parsebrctl(self):
folders = get_log_files(log_root_path)
for folder in folders:
print(f'Parsing {folder}')
result = parsebrctl(folder)
result = parse_path(folder)
if result:
self.assertTrue('containers' in result)
self.assertTrue('boot_history' in result)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_parsers_containermanager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from parsers.containermanager import parsecontainermanager, get_log_files
from parsers.containermanager import parse_path, get_log_files
from tests import SysdiagnoseTestCase
import unittest

Expand All @@ -10,7 +10,7 @@ def test_parsecontainermanager(self):
files = get_log_files(log_root_path)
for file in files:
print(f'Parsing {file}')
result = parsecontainermanager([file])
result = parse_path(file)
for item in result['events']:
self.assertTrue('timestamp' in item)
self.assertTrue('loglevel' in item)
Expand Down
Loading

0 comments on commit dc0688e

Please sign in to comment.