|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import toml |
| 4 | +import sys |
| 5 | + |
| 6 | +source, rust = sys.argv[1:3] |
| 7 | + |
| 8 | +with open(source) as f: |
| 9 | + origin_content = toml.load(f) |
| 10 | + |
| 11 | +with open(rust) as f: |
| 12 | + rust_content = toml.load(f) |
| 13 | + |
| 14 | +# first rewrite rust dependencies to make sure they are version pinned |
| 15 | +package_versions = {} |
| 16 | +for p in rust_content['package']: |
| 17 | + if p['name'] in package_versions: |
| 18 | + package_versions[p['name']].append(p['version']) |
| 19 | + else: |
| 20 | + package_versions[p['name']] = [p['version']] |
| 21 | + |
| 22 | +for p in rust_content['package']: |
| 23 | + if 'dependencies' in p: |
| 24 | + deps = [] |
| 25 | + for dep in p['dependencies']: |
| 26 | + if ' ' in dep: |
| 27 | + deps.append(dep) |
| 28 | + else: |
| 29 | + deps.append(dep + " " + package_versions[dep][0]) |
| 30 | + p['dependencies'] = deps |
| 31 | + |
| 32 | +# do the same with the origin ones |
| 33 | +origin_packages = set() |
| 34 | +package_versions = {} |
| 35 | +for p in origin_content['package']: |
| 36 | + origin_packages.add((p['name'], p['version'])) |
| 37 | + |
| 38 | + if p['name'] in package_versions: |
| 39 | + package_versions[p['name']].append(p['version']) |
| 40 | + else: |
| 41 | + package_versions[p['name']] = [p['version']] |
| 42 | + |
| 43 | +for p in origin_content['package']: |
| 44 | + if 'dependencies' in p: |
| 45 | + deps = [] |
| 46 | + for dep in p['dependencies']: |
| 47 | + if ' ' in dep: |
| 48 | + deps.append(dep) |
| 49 | + else: |
| 50 | + deps.append(dep + " " + package_versions[dep][0]) |
| 51 | + p['dependencies'] = deps |
| 52 | + |
| 53 | + |
| 54 | +# then merge the rust in the origin ones |
| 55 | +# to do that, we can't add the same dependency twice. |
| 56 | +# TODO: do we need to care about the dependencies? (make it a super set?) |
| 57 | +for p in rust_content['package']: |
| 58 | + tup = (p['name'], p['version']) |
| 59 | + if tup not in origin_packages: |
| 60 | + origin_content['package'].append(p) |
| 61 | + |
| 62 | +output = toml.dumps(origin_content) |
| 63 | +print(output) |
0 commit comments