Repository navigation
Expand file tree
/
Copy pathreplace_git_deps.py
More file actions
executable file
·93 lines (77 loc) · 2.63 KB
/
Copy pathreplace_git_deps.py
File metadata and controls
executable file
·93 lines (77 loc) · 2.63 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
"""Convert git-sourced deps to crates.io versions in root Cargo.toml.
Replaces git-sourced deps with their crates.io equivalents, preserving
version, features, package, optional, default-features, and other keys.
"""
import sys
import re
import os
import tomllib
GIT_KEYS = {'git', 'branch', 'rev', 'tag'}
def dep_value_str(spec):
kept = {k: v for k, v in spec.items() if k not in GIT_KEYS}
if not kept:
return ''
if list(kept.keys()) == ['version']:
return f'"{kept["version"]}"'
parts = []
for k, v in kept.items():
if isinstance(v, str):
parts.append(f'{k} = "{v}"')
elif isinstance(v, bool):
parts.append(f'{k} = {"true" if v else "false"}')
elif isinstance(v, list):
items = ', '.join(f'"{x}"' if isinstance(x, str) else str(x) for x in v)
parts.append(f'{k} = [{items}]')
elif isinstance(v, int):
parts.append(f'{k} = {v}')
else:
parts.append(f'{k} = {v}')
return '{ ' + ', '.join(parts) + ' }'
def main():
root = sys.argv[1] if len(sys.argv) > 1 else '.'
path = os.path.join(root, 'Cargo.toml')
with open(path, 'rb') as f:
data = tomllib.load(f)
ws_deps = data.get('workspace', {}).get('dependencies', {})
replacements = {}
for name, spec in ws_deps.items():
if isinstance(spec, dict) and 'git' in spec:
val = dep_value_str(spec)
if val:
replacements[name] = val
if not replacements:
print("No git deps found")
return False
with open(path) as f:
lines = f.readlines()
new_lines = []
changed = False
in_deps = False
dep_re = re.compile(r'^(\s*)([\w-]+)\s*=\s*')
for line in lines:
stripped = line.strip()
if stripped == '[workspace.dependencies]':
in_deps = True
new_lines.append(line)
elif in_deps and stripped.startswith('['):
in_deps = False
new_lines.append(line)
elif in_deps and (m := dep_re.match(line)):
name = m.group(2)
if name in replacements:
new_lines.append(f'{m.group(1)}{name} = {replacements[name]}\n')
changed = True
continue
new_lines.append(line)
else:
new_lines.append(line)
if changed:
with open(path, 'w') as f:
f.writelines(new_lines)
print(f"Converted {len(replacements)} git deps: {', '.join(sorted(replacements))}")
else:
print("No changes needed")
return changed
if __name__ == '__main__':
main()