-
Notifications
You must be signed in to change notification settings - Fork 233
feat(systemextensionsctl): add functionality #597
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
Open
georgettica
wants to merge
15
commits into
kellyjonbrazil:dev
Choose a base branch
from
georgettica:georgettica/feat/systemextensionsctl
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6a1a73c
feat(systemextensionsctl): add functionality
georgettica 48b0313
Merge branch 'dev' into georgettica/feat/systemextensionsctl
kellyjonbrazil 91ae7e5
fix: shorten and add email
georgettica 467afcd
Update systemextensionsctl.py
georgettica 99fbe4b
just add tests that fail
georgettica 4819f0d
add test data and change a bit test code
georgettica 18f01b4
Merge branch 'dev' into georgettica/feat/systemextensionsctl
georgettica 1525b33
forgot to remove empty reads
georgettica 03de306
revamped to make tests pass
georgettica fdb18e3
review notes
georgettica 912b3e5
filename typo
georgettica 901c047
Update systemextensionsctl.py
georgettica 203762e
Merge branch 'dev' into georgettica/feat/systemextensionsctl
kellyjonbrazil 23ef46c
Merge branch 'dev' into georgettica/feat/systemextensionsctl
kellyjonbrazil ff01aef
Merge branch 'dev' into georgettica/feat/systemextensionsctl
kellyjonbrazil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| """jc - JSON Convert `systemextensionsctl list` command output parser | ||
|
|
||
| Usage: | ||
|
|
||
| $ systemextensionsctl list | jc --systemextensionsctl | ||
|
|
||
| Compatibility: | ||
|
|
||
| macOS | ||
|
Comment on lines
+7
to
+9
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need this section |
||
|
|
||
| Example: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing a Schema section |
||
|
|
||
| $ systemextensionsctl list | jc --systemextensionsctl -p | ||
| { | ||
| "total_extensions": 1, | ||
| "sections": [ | ||
| { | ||
| "category": "com.apple.system_extension.network_extension", | ||
| "description": "Go to 'System Settings > General > Login Items & Extensions > Network Extensions' to modify these system extension(s)", | ||
| "entries": [ | ||
| { | ||
| "enabled": "*", | ||
| "active": "*", | ||
georgettica marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "teamID": "XXX", | ||
| "bundleID": "YYY", | ||
| "version": "QQQ", | ||
| "name": "ZZZ", | ||
| "state": "activated enabled" | ||
georgettica marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| ] | ||
| }, | ||
|
|
||
| ] | ||
| } | ||
|
|
||
| """ | ||
|
|
||
| import jc.utils | ||
| import jc.parsers.universal | ||
| import re | ||
|
|
||
| class info(): | ||
| version = '1.0' | ||
| description = '`systemextensionsctl list` command parser' | ||
| author = 'Ron Green' | ||
| author_email = '11993626+georgettica@users.noreply.github.com' | ||
| compatible = ['darwin'] | ||
georgettica marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| magic_commands = ['systemextensionsctl list'] | ||
| def parse(data, raw=False, quiet=False): | ||
| """ | ||
| Parses the output of `systemextensionsctl list` command. | ||
|
|
||
| Parameters: | ||
|
|
||
| data: (string) Text data to parse | ||
|
|
||
| Returns: | ||
|
|
||
| Dictionary with parsed data | ||
| """ | ||
| jc.utils.compatibility(__name__, info.compatible, quiet) | ||
| if jc.utils.has_data(data): | ||
|
|
||
| lines = data.strip().splitlines() | ||
| total_extensions = None | ||
| sections = [] | ||
| section = None | ||
| headers = None | ||
|
|
||
| line_iter = iter(lines) | ||
| for line in line_iter: | ||
| line = line.strip('\n') | ||
| if line.endswith('extension(s)'): | ||
| # Extract total_extensions | ||
| match = re.match(r'(\d+)\s+extension\(s\)', line) | ||
| if match: | ||
| total_extensions = int(match.group(1)) | ||
| elif line.startswith('--- '): | ||
| # Only process sections if total_extensions > 0 | ||
| if total_extensions is not None and total_extensions > 0: | ||
| category_line = line | ||
| # Extract category and description | ||
| category_and_desc = line[4:] | ||
| # Category is up to the first '(' | ||
| if '(' in category_and_desc and ')' in category_and_desc: | ||
| category = category_and_desc.split('(')[0].strip() | ||
| description = category_and_desc[category_and_desc.find('(')+1:category_and_desc.rfind(')')].strip() | ||
| else: | ||
| category = category_and_desc.strip() | ||
| description = '' | ||
| section = { | ||
| 'category': category, | ||
| 'description': description, | ||
| 'entries': [] | ||
| } | ||
| sections.append(section) | ||
| try: | ||
| # Read the header line | ||
| headers_line = next(line_iter).strip() | ||
| # Check if headers_line contains expected headers | ||
| expected_headers = ['enabled', 'active', 'teamID', 'bundleID (version)', 'name', '[state]'] | ||
| actual_headers = [h.strip().lower() for h in headers_line.split('\t')] | ||
| if not all(header.lower() in actual_headers for header in expected_headers): | ||
| # Headers do not match expected format | ||
| headers = None | ||
| continue | ||
| headers = headers_line.split('\t') | ||
| headers = [h.strip() for h in headers] | ||
| except StopIteration: | ||
| # No more lines; headers not found | ||
| headers = None | ||
| continue | ||
| else: | ||
| # If total_extensions is 0 or not set, skip processing sections | ||
| continue | ||
| else: | ||
| # Data line | ||
| if not line.strip(): | ||
| continue | ||
| if not section or not headers: | ||
| # No section defined or headers missing; skip the line | ||
| continue | ||
| fields = line.split('\t') | ||
| # Pad fields with empty strings if needed | ||
| if len(fields) < len(headers): | ||
| fields = fields + ['']*(len(headers) - len(fields)) | ||
| entry = {} | ||
| for h, f in zip(headers, fields): | ||
| entry[h] = f.strip() | ||
| # Optionally parse the 'bundleID (version)' field | ||
| if 'bundleID (version)' in entry: | ||
| bundleID_version = entry['bundleID (version)'] | ||
| match = re.match(r'(.+)\s+\((.+)\)', bundleID_version) | ||
| if match: | ||
| bundleID = match.group(1).strip() | ||
| version = match.group(2).strip() | ||
| entry['bundleID'] = bundleID | ||
| entry['version'] = version | ||
| else: | ||
| entry['bundleID'] = bundleID_version.strip() | ||
| entry['version'] = '' | ||
| del entry['bundleID (version)'] | ||
| # Optionally parse the '[state]' field | ||
| if '[state]' in entry: | ||
| state = entry['[state]'] | ||
| if state.startswith('[') and state.endswith(']'): | ||
| state_content = state[1:-1] | ||
| entry['state'] = state_content.strip() | ||
| else: | ||
| entry['state'] = state.strip() | ||
| del entry['[state]'] | ||
| # Add entry to the current section | ||
| section['entries'].append(entry) | ||
|
|
||
| # Finalize the result | ||
| if total_extensions is not None: | ||
| result = { | ||
| 'total_extensions': total_extensions, | ||
| 'sections': sections | ||
| } | ||
| return result | ||
| else: | ||
| return {} | ||
| else: | ||
| return {} | ||
4 changes: 4 additions & 0 deletions
4
tests/fixtures/generic/systemextensionsctl-no-extensions.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "total_extensions": 0, | ||
| "sections": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 0 extension(s) | ||
| --- com.apple.system_extension.network_extension (No extensions found) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "total_extensions": 1, | ||
| "sections": [ | ||
| { | ||
| "category": "com.apple.system_extension.network_extension", | ||
| "description": "Go to 'System Settings > General > Login Items & Extensions > Network Extensions' to modify these system extension(s)", | ||
| "entries": [ | ||
| { | ||
| "enabled": "*", | ||
| "active": "*", | ||
| "teamID": "XXX", | ||
| "bundleID": "YYY", | ||
| "version": "QQQ", | ||
| "name": "ZZZ", | ||
| "state": "activated enabled" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| 1 extension(s) | ||
| --- com.apple.system_extension.network_extension (Go to 'System Settings > General > Login Items & Extensions > Network Extensions' to modify these system extension(s)) | ||
| enabled active teamID bundleID (version) name [state] | ||
| * * XXX YYY (QQQ) ZZZ [activated enabled] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import os | ||
| import json | ||
| import unittest | ||
| import jc.parsers.systemextensionsctl | ||
|
|
||
| THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
|
|
||
| class MyTests(unittest.TestCase): | ||
|
|
||
| # Input data from fixtures | ||
| with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/systemextensionsctl.out'), 'r', encoding='utf-8') as f: | ||
| systemextensionsctl_output = f.read() | ||
|
|
||
| with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/systemextensionsctl-no-extensions.out'), 'r', encoding='utf-8') as f: | ||
| systemextensionsctl_no_extensions_output = f.read() | ||
|
|
||
| # Expected output data from fixtures | ||
| with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/systemextensionsctl.json'), 'r', encoding='utf-8') as f: | ||
| systemextensionsctl_expected = json.loads(f.read()) | ||
|
|
||
| with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/systemextensionsctl-no-extensions.json'), 'r', encoding='utf-8') as f: | ||
| systemextensionsctl_no_extensions_expected = json.loads(f.read()) | ||
|
|
||
| def test_systemextensionsctl(self): | ||
| """ | ||
| Test 'systemextensionsctl list' with placeholder data | ||
| """ | ||
| self.assertEqual( | ||
| jc.parsers.systemextensionsctl.parse( | ||
| self.systemextensionsctl_output, quiet=True), | ||
| self.systemextensionsctl_expected | ||
| ) | ||
|
|
||
| def test_systemextensionsctl_empty(self): | ||
| """ | ||
| Test 'systemextensionsctl list' with empty input | ||
| """ | ||
| self.assertEqual( | ||
| jc.parsers.systemextensionsctl.parse( | ||
| '', quiet=True), | ||
| {} | ||
| ) | ||
|
|
||
| def test_systemextensionsctl_no_extensions(self): | ||
| """ | ||
| Test 'systemextensionsctl list' with no extensions | ||
| """ | ||
| self.assertEqual( | ||
| jc.parsers.systemextensionsctl.parse( | ||
| self.systemextensionsctl_no_extensions_output, quiet=True), | ||
| self.systemextensionsctl_no_extensions_expected | ||
| ) | ||
|
|
||
| def test_systemextensionsctl_nodata(self): | ||
| """ | ||
| Test 'systemextensionsctl list' with no data | ||
| """ | ||
| self.assertEqual(jc.parsers.systemextensionsctl.parse('', quiet=True), {}) | ||
|
|
||
| def test_systemextensionsctl_incorrect_format(self): | ||
| """ | ||
| Test 'systemextensionsctl list' with incorrect format | ||
| """ | ||
| incorrect_data = 'This is not the correct format.' | ||
| self.assertEqual( | ||
| jc.parsers.systemextensionsctl.parse(incorrect_data, quiet=True), {} | ||
| ) | ||
|
|
||
| def test_systemextensionsctl_trailing_newline(self): | ||
| """ | ||
| Test 'systemextensionsctl list' with trailing newline | ||
| """ | ||
| cmd_output = self.systemextensionsctl_output + '\n' | ||
| self.assertEqual( | ||
| jc.parsers.systemextensionsctl.parse(cmd_output, quiet=True), | ||
| self.systemextensionsctl_expected | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure to add the new parser to
lib.py.