forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependency.py
139 lines (121 loc) · 4.43 KB
/
dependency.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Copyright (c) Mysten Labs
# SPDX-License-Identifier: Apache-2.0
import argparse
import os
import re
ROOT = os.path.join(os.path.dirname(__file__), "../")
PATTERN = None
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--project", default="move")
subparser = parser.add_subparsers(
dest="command",
description="""
Automatically manage the dependency path to Move repository.
Command "local" switches the dependency from git to local path.
Command "upgrade" upgrades the git revision. A repository can be
specified if we want to use a fork instead of upstream.
A revision or a branch also needs to be specified.
""",
)
subparser.add_parser("local")
upgrade = subparser.add_parser("upgrade")
upgrade.add_argument("--repo", required=False)
upgrade_group = upgrade.add_mutually_exclusive_group(required=True)
upgrade_group.add_argument("--rev")
upgrade_group.add_argument("--branch")
return parser.parse_args()
def scan_file(file, process_line, depth=0):
new_content = []
with open(file) as f:
for line in f.readlines():
new_content.append(process_line(line, depth))
with open(file, "w") as f:
f.writelines(new_content)
def scan_files(path, process_line, depth=0):
for file in os.listdir(path):
full_path = os.path.join(path, file)
if os.path.isdir(full_path):
scan_files(full_path, process_line, depth + 1)
elif file == "Cargo.toml":
scan_file(full_path, process_line, depth)
def try_match_line(line):
# Remove all spacing for easier pattern matching
line = line.strip().replace(' ', '')
m = PATTERN.match(line)
if m:
name = m.group(1)
if m.group(2) is None:
extra = ""
else:
# Add some spacing so it looks nicer
extra = m.group(2).replace(',', ', ').replace('=', ' = ')
return (name, extra)
return None
def switch_to_local(project):
default_path_map = {
"move": "move/language",
"narwhal": "narwhal",
}
# Packages that don't directly map to a directory under move/language
# go here as special cases. By default, we just use language/[name].
subpath_path_map = {
"move": {
"move-bytecode-utils": "tools/move-bytecode-utils",
"move-disassembler": "tools/move-disassembler",
"move-ir-types": "move-ir/types",
"move-cli": "tools/move-cli",
"move-core-types": "move-core/types",
"move-package": "tools/move-package",
"move-unit-test": "tools/move-unit-test",
"move-vm-runtime": "move-vm/runtime",
"move-vm-types": "move-vm/types",
"move-transactional-test-runner": "testing-infra/transactional-test-runner",
},
"narwhal": {},
}
def process_line(line, depth):
m = try_match_line(line)
if m:
(name, extra) = m
go_back = "".join(["../"] * (depth + 1))
path = default_path_map[project]
if project == "move":
subpath = subpath_path_map[project].get(name, name)
elif project == "narwhal":
subpath = name.replace("narwhal-", "")
return '{} = {{ path = "{}{}/{}"{} }}\n'.format(
name, go_back, path, subpath, extra
)
return line
scan_files(ROOT, process_line)
def upgrade_revision(project, repo, rev, branch):
assert (args.rev is None) != (args.branch is None)
def process_line(line, _):
m = try_match_line(line)
if m:
(name, extra) = m
return '{} = {{ git = "https://github.com/{}/{}", {} = "{}"{} }}\n'.format(
name, repo, project,
"branch" if branch else "rev",
branch if branch else rev,
extra
)
return line
scan_files(ROOT, process_line)
args = parse_args()
assert(args.project == "move" or args.project == "narwhal")
PATTERN = re.compile(
'(.+)={git="https://github.com/.+/' + args.project + '",(?:rev|branch)="[^"]+"(,.*)?}'
)
if args.command == "local":
switch_to_local(args.project)
else:
assert args.command == "upgrade"
repo = args.repo
if not repo:
if args.project == "move":
repo = "move-language"
else:
repo = "MystenLabs"
upgrade_revision(args.project, repo, args.rev, args.branch)