|
| 1 | +#!/usr/bin/env python |
| 2 | +# This script merges two Arduino Board Manager package json files. |
| 3 | +# Usage: |
| 4 | +# python merge_packages.py package_esp8266com_index.json version/new/package_esp8266com_index.json |
| 5 | +# Written by Ivan Grokhotkov, 2015 |
| 6 | +# |
| 7 | +from __future__ import print_function |
| 8 | +import json |
| 9 | +import sys |
| 10 | + |
| 11 | +def load_package(filename): |
| 12 | + pkg = json.load(open(filename))['packages'][0] |
| 13 | + print("Loaded package {0} from {1}".format(pkg['name'], filename)) |
| 14 | + print("{0} platform(s), {1} tools".format(len(pkg['platforms']), len(pkg['tools']))) |
| 15 | + return pkg |
| 16 | + |
| 17 | +def merge_objects(versions, obj): |
| 18 | + for o in obj: |
| 19 | + name = o['name'].encode('ascii') |
| 20 | + ver = o['version'].encode('ascii') |
| 21 | + if not name in versions: |
| 22 | + print("found new object, {0}".format(name)) |
| 23 | + versions[name] = {} |
| 24 | + if not ver in versions[name]: |
| 25 | + print("found new version {0} for object {1}".format(ver, name)) |
| 26 | + versions[name][ver] = o |
| 27 | + return versions |
| 28 | + |
| 29 | + |
| 30 | +def main(args): |
| 31 | + if len(args) < 3: |
| 32 | + print("Usage: {0} <package1> <package2>".format(args[0])) |
| 33 | + return 1 |
| 34 | + |
| 35 | + tools = {} |
| 36 | + platforms = {} |
| 37 | + pkg1 = load_package(args[1]) |
| 38 | + tools = merge_objects(tools, pkg1['tools']); |
| 39 | + platforms = merge_objects(platforms, pkg1['platforms']); |
| 40 | + pkg2 = load_package(args[2]) |
| 41 | + tools = merge_objects(tools, pkg2['tools']); |
| 42 | + platforms = merge_objects(platforms, pkg2['platforms']); |
| 43 | + |
| 44 | + pkg1['tools'] = [] |
| 45 | + pkg1['platforms'] = [] |
| 46 | + |
| 47 | + for name in tools: |
| 48 | + for version in tools[name]: |
| 49 | + print("Adding tool {0}-{1}".format(name, version)) |
| 50 | + pkg1['tools'].append(tools[name][version]) |
| 51 | + |
| 52 | + for name in platforms: |
| 53 | + for version in platforms[name]: |
| 54 | + print("Adding platform {0}-{1}".format(name, version)) |
| 55 | + pkg1['platforms'].append(platforms[name][version]) |
| 56 | + |
| 57 | + json.dump({'packages':[pkg1]}, sys.stdout, indent=2) |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + sys.exit(main(sys.argv)) |
0 commit comments