Skip to content

Commit 3391e03

Browse files
authored
Replace update_bazel_workspace.sh with a python script (#1271)
This script is (IMO) more readable, but the real reason for this change is that it raises an error message when the binary package fails to download. (The shell script silently generated a bogus hash instead, because the shell's `set -e` builtin does not affect commands executing inside a $() context. It seemed just as easy to rewrite the script in Python as to fix that. This change also updates some outdated filename references.
1 parent 5d347a6 commit 3391e03

File tree

6 files changed

+75
-77
lines changed

6 files changed

+75
-77
lines changed

bazel/revisions.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# This file is automatically updated by emsdk/scripts/update_bazel_workspace.sh
1+
# This file is automatically updated by emsdk/scripts/update_bazel_workspace.py
22
# DO NOT MODIFY
33

44
EMSCRIPTEN_TAGS = {

scripts/create_release.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def main(args):
5353
f.write(json.dumps(release_info, indent=2))
5454
f.write('\n')
5555

56-
subprocess.check_call([os.path.join(script_dir, 'update_bazel_workspace.sh')], cwd=root_dir)
56+
subprocess.check_call(
57+
[sys.executable, os.path.join(script_dir, 'update_bazel_workspace.py')],
58+
cwd=root_dir)
5759

5860
branch_name = 'version_' + new_version
5961

scripts/update_bazel_workspace.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
# This script will update emsdk/bazel/revisons.bzl to the latest version of
3+
# emscripten. It reads emsdk/emscripten-releases-tags.json to get the latest
4+
# version number. Then, it downloads the prebuilts for that version and computes
5+
# the sha256sum for the archive. It then puts all this information into the
6+
# emsdk/bazel/revisions.bzl file.
7+
8+
import hashlib
9+
import json
10+
import os
11+
import requests
12+
import sys
13+
14+
STORAGE_URL = 'https://storage.googleapis.com/webassembly/emscripten-releases-builds'
15+
16+
EMSDK_ROOT = os.path.dirname(os.path.dirname(__file__))
17+
RELEASES_TAGS_FILE = EMSDK_ROOT + '/emscripten-releases-tags.json'
18+
BAZEL_REVISIONS_FILE = EMSDK_ROOT + '/bazel/revisions.bzl'
19+
20+
21+
def get_latest_info():
22+
with open(RELEASES_TAGS_FILE) as f:
23+
info = json.load(f)
24+
latest = info['aliases']['latest']
25+
return latest, info['releases'][latest]
26+
27+
28+
def get_sha(platform, archive_fmt, latest_hash, arch_suffix=''):
29+
r = requests.get(f'{STORAGE_URL}/{platform}/{latest_hash}/wasm-binaries{arch_suffix}.{archive_fmt}')
30+
r.raise_for_status()
31+
print(f'Fetching {r.url}')
32+
h = hashlib.new('sha256')
33+
for chunk in r.iter_content(chunk_size=1024):
34+
h.update(chunk)
35+
return h.hexdigest()
36+
37+
38+
def revisions_item(version, latest_hash):
39+
return f'''\
40+
"{version}": struct(
41+
hash = "{latest_hash}",
42+
sha_linux = "{get_sha('linux', 'tbz2', latest_hash)}",
43+
sha_mac = "{get_sha('mac', 'tbz2', latest_hash)}",
44+
sha_mac_arm64 = "{get_sha('mac', 'tbz2', latest_hash, '-arm64')}",
45+
sha_win = "{get_sha('win', 'zip', latest_hash)}",
46+
),
47+
'''
48+
49+
50+
def insert_revision(item):
51+
with open(BAZEL_REVISIONS_FILE, 'r') as f:
52+
lines = f.readlines()
53+
54+
lines.insert(lines.index('EMSCRIPTEN_TAGS = {\n') + 1, item)
55+
56+
with open(BAZEL_REVISIONS_FILE, 'w') as f:
57+
f.write(''.join(lines))
58+
59+
60+
def main(argv):
61+
version, latest_hash = get_latest_info()
62+
item = revisions_item(version, latest_hash)
63+
print('inserting item:')
64+
print(item)
65+
insert_revision(item)
66+
67+
68+
if __name__ == '__main__':
69+
sys.exit(main(sys.argv))

scripts/update_bazel_workspace.sh

Lines changed: 0 additions & 73 deletions
This file was deleted.

test/test_bazel.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ HASH=$(grep "\"${VER}\"" emscripten-releases-tags.json \
1414
| grep -v latest \
1515
| cut -f4 -d\")
1616

17-
FAILMSG="!!! scripts/update_bazel_toolchain.sh needs to be run !!!"
17+
FAILMSG="!!! scripts/update_bazel_workspace.py needs to be run !!!"
1818

1919
# Ensure the WORKSPACE file is up to date with the latest version.
2020
grep ${VER} bazel/revisions.bzl || (echo ${FAILMSG} && false)

test/test_bazel_mac.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ HASH=$(grep "\"${VER}\"" emscripten-releases-tags.json \
1414
| grep -v latest \
1515
| cut -f4 -d\")
1616

17-
FAILMSG="!!! scripts/update_bazel_toolchain.sh needs to be run !!!"
17+
FAILMSG="!!! scripts/update_bazel_workspace.py needs to be run !!!"
1818

1919
# Ensure the WORKSPACE file is up to date with the latest version.
2020
grep ${VER} bazel/revisions.bzl || (echo ${FAILMSG} && false)

0 commit comments

Comments
 (0)