summaryrefslogtreecommitdiffstats
path: root/scripts/vendor-cargo-workspace-package
blob: a8c4579388cdd060bdb814235fdde2f10ec68774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3

import argparse
import toml
from copy import deepcopy
from os.path import dirname, join, relpath

parser = argparse.ArgumentParser(description='vendor cargo workspace package')
parser.add_argument('--input')
parser.add_argument('--output')
parser.add_argument('--workspace')
args = parser.parse_args()

pkg_input = toml.load(args.input)
pkg_output = deepcopy(pkg_input)
workspace = toml.load(args.workspace)

rel = relpath(dirname(args.workspace), dirname(args.input))

for section, data in pkg_input.items():
    if not isinstance(data, dict):
        continue
    for key, value in data.items():
        if not isinstance(value, dict):
            continue
        if not 'workspace' in value:
            continue
        if not value['workspace']:
            continue
        if not 'workspace' in workspace:
            print(f'missing section "workspace" {args.workspace}')
            exit(1)
        if section in workspace['workspace']:
            wsection = section
        else:
            wsection = section[4:] if section.startswith('dev-') else section
            if not wsection in workspace['workspace']:
                print(f'missing section "workspace.{section}" in {args.workspace} for {args.input}')
                exit(1)
        try:
            copy = deepcopy(workspace['workspace'][wsection][key])
        except KeyError:
            print(f'missing "workspace.{section}.{key}" in {args.workspace} for {args.input}')
            exit(1)
        if isinstance(copy, dict):
            if 'path' in copy:
                copy['path'] = join(rel, copy['path'])
            pkg_output[section][key].pop('workspace')
            pkg_output[section][key].update(copy)
        else:
            pkg_output[section][key] = copy


with open(args.output, 'w') as f:
    toml.dump(pkg_output, f)