monakhos/ansible/parse_arch_packages.py

42 lines
1 KiB
Python

import json
import sys
import yaml
import fileinput
packages_tree = None
yaml_config = ''.join(sys.stdin.readlines())
packages_tree = yaml.safe_load(yaml_config)
assert packages_tree is not None, "Must have load valid package tree"
def flatten_packages(item):
packages = []
if isinstance(item, str):
return {'name': item}
if isinstance(item, dict) and 'name' in item:
return item
if isinstance(item, list):
return [flatten_packages(p) for p in item]
if isinstance(item, dict):
packages = []
for k, v in item.items():
packages += flatten_packages(v)
return packages
return []
flat_packages = flatten_packages(packages_tree)
native_names = []
aur_names = []
for p in flat_packages:
assert isinstance(p, dict)
if p['name'].startswith('aur/'):
aur_names.append(p['name'].split('/')[1])
continue
native_names.append(p['name'])
print(json.dumps({
"native": native_names,
"aur": aur_names
}))
# print(' '.join(names))