parse.py (1301B)
1 #!/usr/bin/env python3 2 # This Source Code Form is subject to the terms of the Mozilla Public 3 # License, v. 2.0. If a copy of the MPL was not distributed with this 4 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 6 7 import sys 8 9 import yaml 10 11 12 def has_pkg_section(p, section, arch): 13 has_section = section in p.keys() 14 if has_section: 15 for pkg in p[section]: 16 if type(pkg) is str: 17 yield pkg 18 else: 19 next_section = next(iter(pkg.keys())) 20 if "on " in next_section or f"to {arch}" in next_section: 21 yield from has_pkg_section(pkg, next(iter(pkg.keys())), arch) 22 23 24 def iter_pkgs(part, all_pkgs, arch): 25 for section in ["build-packages", "stage-packages"]: 26 for pkg in has_pkg_section(part, section, arch): 27 if pkg not in all_pkgs: 28 if ":" in pkg and pkg.split(":")[1] != arch: 29 continue 30 all_pkgs.append(pkg) 31 32 33 def parse(yaml_file, arch): 34 all_pkgs = [] 35 with open(yaml_file) as inp: 36 snap = yaml.safe_load(inp) 37 parts = snap["parts"] 38 for p in parts: 39 iter_pkgs(parts[p], all_pkgs, arch) 40 return " ".join(all_pkgs) 41 42 43 if __name__ == "__main__": 44 print(parse(sys.argv[1], sys.argv[2]))