forked from wasmerio/wasmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.py
359 lines (315 loc) · 12.1 KB
/
publish.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#! /usr/bin/env python3
"""This is a script for publishing the wasmer crates to crates.io.
It should be run in the root of wasmer like `python3 scripts/publish.py --help`.
Please lint with pylint and format with black.
$ pylint scripts/publish.py
--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)
$ black scripts/publish.py
"""
import argparse
import itertools
import os
import re
import subprocess
import time
import sys
import typing
from pprint import pprint
try:
# try import tomllib from standard Python library: New in version 3.11.
import tomllib
except ModuleNotFoundError:
try:
# if tomllib is missing, this is an older python3 version. Try
# importing the equivalent third-party library tomli:
import tomli as tomllib
except ModuleNotFoundError:
print("Please install tomli, `pip3 install tomli`")
sys.exit(1)
try:
from graphlib import TopologicalSorter
except ImportError:
print("Incompatible python3 version: lacks graphlib standard library module.")
sys.exit(1)
SETTINGS = {
# extra features to put when publishing, for example wasmer-cli needs a
# compiler by default otherwise it won't work standalone
"publish_features": {
"wasmer-cli": "default,cranelift",
"wasmer-wasix": "sys,wasmer/sys",
"wasmer-wasix-types": "wasmer/sys",
"wasmer-wasix-experimental-io-devices": "wasmer-wasix/sys,wasmer/sys",
"wasmer-wast": "wasmer/sys",
"wai-bindgen-wasmer": "sys",
"wasmer-cache": "wasmer/sys",
"wasmer-emscripten": "wasmer/sys"
},
# workspace members we want to publish but whose path doesn't start by
# "./lib/"
"non-lib-workspace-members": set(["tests/lib/wast"]),
}
def get_latest_version_for_crate(crate_name: str) -> typing.Optional[str]:
"""Fetches the latest version of a given crate name in local cargo registry."""
output = subprocess.run(
["cargo", "search", crate_name], capture_output=True, check=True
)
rexp_src = f'^{crate_name} = "([^"]+)"'
prog = re.compile(rexp_src)
haystack = output.stdout.decode("utf-8")
for line in haystack.splitlines():
result = prog.match(line)
if result:
return result.group(1)
return None
class Crate:
"""Represents a workspace crate that is to be published to crates.io."""
def __init__(
self,
dependencies: typing.List[str],
cargo_manifest: dict,
cargo_file_path="Cargo.toml",
):
self.name = cargo_manifest["package"]["name"]
self.dependencies = dependencies
self.cargo_manifest = cargo_manifest
if not os.path.isabs(cargo_file_path):
cargo_file_path = os.path.join(os.getcwd(), cargo_file_path)
self.cargo_file_path = cargo_file_path
def __str__(self):
return f"{self.name}: {self.dependencies} {self.cargo_file_path} {self.path()}"
@property
def path(self) -> str:
"""Return the absolute filesystem path containing this crate."""
return os.path.dirname(self.cargo_file_path)
@property
def version(self) -> str:
"""Return the crate's version according to its manifest."""
return self.cargo_manifest["package"]["version"]
class Publisher:
"""A class responsible for figuring out dependencies,
creating a topological sorting in order to publish them
to crates.io in a valid order."""
def __init__(self, version=None, dry_run=True, verbose=True):
self.dry_run: bool = dry_run
self.verbose: bool = verbose
# open workspace Cargo.toml
with open("Cargo.toml", "rb") as file:
data = tomllib.load(file)
if version is None:
version = data["package"]["version"]
self.version: str = version
if self.verbose and not self.dry_run:
print(f"Publishing version {self.version}")
elif self.verbose and self.dry_run:
print(f"Publishing version {self.version} dry run!")
# define helper function
check_local_dep_fn = lambda t: isinstance(t[1], dict) and "path" in t[1]
members = set(
map(
lambda p: p + "/Cargo.toml",
filter(
lambda path: (
path.startswith("lib/") and os.path.exists(path + "/Cargo.toml")
)
or path in SETTINGS["non-lib-workspace-members"],
itertools.chain(
data["workspace"]["members"],
map(
lambda p: p[1]["path"],
filter(check_local_dep_fn, data["dependencies"].items()),
),
),
),
)
)
crates = []
for member in members:
with open(member, "rb") as file:
member_data = tomllib.load(file)
def return_dependencies(toml) -> typing.List[str]:
acc = set()
stack = [toml]
while len(stack) > 0:
toml = stack.pop()
if "dependencies" in toml:
acc.update(
list(
map(
lambda dep: dep[1]["package"]
if "package" in dep[1]
else dep[0],
filter(
check_local_dep_fn, toml["dependencies"].items()
),
)
)
)
if "target" in toml:
stack.append(toml["target"])
for key, value in toml.items():
if key.startswith("cfg"):
stack.append(value)
return list(acc)
dependencies = return_dependencies(member_data)
crates.append(Crate(dependencies, member_data, cargo_file_path=member))
self.crates = crates
self.crate_index: typing.Dict[str, Crate] = {c.name: c for c in crates}
self.create_publish_order()
self.starting_dir = os.getcwd()
def create_publish_order(self):
"""Creates a valid publish order by topologically
sorting crates using the dependency graph."""
topological_sorter = TopologicalSorter()
for crate in self.crates:
topological_sorter.add(crate.name, *crate.dependencies)
self.publish_order: typing.List[str] = [*topological_sorter.static_order()]
def is_crate_already_published(self, crate_name: str) -> bool:
"""Checks if a given crate name is already published
with the version string we intend to publish with."""
found_string: str = get_latest_version_for_crate(crate_name)
if found_string is None:
return False
return self.version == found_string
def publish_crate(self, crate_name: str):
# pylint: disable=broad-except
"""Publish a given crate by name."""
status = None
try:
crate = self.crate_index[crate_name]
os.chdir(crate.path)
extra_args = []
if crate_name in SETTINGS["publish_features"]:
extra_args = ["--features", SETTINGS["publish_features"][crate_name]]
if self.dry_run:
print(f"In dry-run: not publishing crate `{crate_name}`")
command = ["cargo", "publish", "--dry-run"] + extra_args
if self.verbose:
print(*command)
output = subprocess.run(command, check=True)
if self.verbose:
print(output)
else:
command = ["cargo", "publish"] + extra_args
if self.verbose:
print(*command)
output = subprocess.run(command, check=True)
if self.verbose:
print(output)
if self.verbose:
print("Success.")
except Exception as exc:
if self.verbose:
print(f"Failed to publish {crate_name}.")
print(exc)
status = exc
finally:
os.chdir(self.starting_dir)
return status
def publish(self):
# pylint: disable=too-many-branches
"""Publish all packages in workspace."""
if self.verbose and self.dry_run:
print("(Dry run, not actually publishing anything)")
if self.verbose:
print("Publishing order:")
pprint(self.publish_order)
status = {}
failures = 0
for crate_name in self.publish_order:
print(f"Publishing `{crate_name}`...")
if not self.is_crate_already_published(crate_name):
status[crate_name] = self.publish_crate(crate_name)
if status[crate_name]:
failures = +1
else:
print(f"`{crate_name}` was already published!")
continue
# sleep for 16 seconds between crates to ensure the crates.io
# index has time to update
if not self.dry_run:
print(
"Sleeping for 16 seconds to allow the `crates.io` index to update..."
)
time.sleep(16)
else:
print("In dry-run: not sleeping for crates.io to update.")
if failures > 0 and self.verbose:
print(f"encountered {failures} failures.")
for key, value in status.items():
if value is None:
result = "ok"
else:
result = str(value)
print(f"{key}\t{result}")
if self.verbose:
print(f"Published {len(status) - failures} crates.")
return failures
def main():
"""Main executable function."""
os.environ["WASMER_PUBLISH_SCRIPT_IS_RUNNING"] = "1"
parser = argparse.ArgumentParser(
description="Publish the Wasmer crates to crates.io"
)
subparsers = parser.add_subparsers(dest="subcommand")
health_cmd = subparsers.add_parser(
"health-check",
help="""Check the dependency graph is a tree, meaning a non-cyclic
planar graph. Combine with verbosity to print a topological sorting
of the graph.""",
)
health_cmd.add_argument(
"-v",
"--verbose",
default=False,
action="store_true",
help="Be verbose.",
)
health_cmd.add_argument(
"--print-dependencies",
default=False,
action="store_true",
help="For each crate, print its dependencies.",
)
publish_cmd = subparsers.add_parser(
"publish", help="Publish Wasmer crates to crates.io."
)
publish_cmd.add_argument(
"--version",
default=None,
type=str,
help="""Define the semver target triple (Default is automatically
read from workspace Cargo.toml.""",
)
publish_cmd.add_argument(
"--dry-run",
default=False,
action="store_true",
help="""Run the script without actually publishing anything to
crates.io""",
)
publish_cmd.add_argument(
"-v",
"--verbose",
default=False,
action="store_true",
help="Be verbose.",
)
args = parser.parse_args()
if args.subcommand == "health-check":
verbose = args.verbose
publisher = Publisher(verbose=verbose)
if verbose:
print(f"Version is {publisher.version}")
pprint(publisher.publish_order)
if args.print_dependencies:
for crate in publisher.crates:
print(f"{crate.name}: {crate.dependencies}")
return 0
if args.subcommand == "publish":
verbose = args.verbose
publisher = Publisher(dry_run=args.dry_run, verbose=verbose)
return publisher.publish()
return 0
if __name__ == "__main__":
main()