Created
January 12, 2026 08:23
-
-
Save Firesphere/148ee4cd9c0d2e44af9271827daa7a2e to your computer and use it in GitHub Desktop.
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
| def parse_apt_source(sources) -> dict: | |
| key_name_map = { | |
| "types": "type", | |
| "uris": "url", | |
| "suites": "distribution", | |
| "components": "components", | |
| "enabled": "enabled" | |
| } | |
| tmplist = [] | |
| tmpsource = {} | |
| do_continue = False | |
| for line in sources: | |
| if not len(line.strip()) > 2 or line.startswith('#'): | |
| continue | |
| if line == '<--PyInfraSeparator-->': | |
| tmplist.append(tmpsource) | |
| tmpsource = { | |
| 'options': {} | |
| } | |
| do_continue = False | |
| continue | |
| if 'architectures' in line.lower(): | |
| tmpsource['options']['Architectures'] = line.split(': ')[1].split(' ') | |
| if '-----BEGIN PGP PUBLIC KEY BLOCK-----' in line: | |
| tmpsource['options'] = {'Signed-By': 'Inline PGP key'} | |
| do_continue = True | |
| continue | |
| if do_continue: | |
| if '-----END PGP PUBLIC KEY BLOCK-----' in line: | |
| do_continue = False | |
| continue | |
| key, value = (line.split(': ')) | |
| if key.lower() in key_name_map: | |
| if key_name_map[key.lower()] in tmpsource: | |
| tmplist.append(tmpsource) | |
| tmpsource = {} | |
| if key.lower() == 'components': | |
| value = value.split(' ') | |
| tmpsource[key_name_map[key.lower()]] = value | |
| import json | |
| print(json.dumps(tmplist, indent=2)) | |
| return tmplist | |
| class AptSourcesSources(FactBase): | |
| @override | |
| def command(self): | |
| return "for f in /etc/apt/sources.list.d/*.sources; do cat $f && echo \"<--PyInfraSeparator-->\"; done" | |
| @override | |
| def requires_command(self) -> str: | |
| return "apt" | |
| @override | |
| def process(self, output) -> list: | |
| repos = parse_apt_source(output) | |
| return repos | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an incredibly hacky, one-hour throw-together of a
ubuntu.sourceslist parser. Please don't judge me for hacking this ugly thing together?