diff --git a/scripts/get-libwasm-version.py b/scripts/get-libwasm-version.py deleted file mode 100755 index 7b50cae5873..00000000000 --- a/scripts/get-libwasm-version.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python3 -""" -The purpose of this script is to output the version of the libwasm library that is -specified in the go.mod file in the wasm module. - -This should be passed as a build argument to the Dockerfile to determine which static library should -be added. - -usage: get-libwasm-version.py [-h] [--get-version | --no-get-version | --get-checksum | --no-get-checksum] [--wasm-library WASM_LIBRARY] [--wasm-go-mod-path WASM_GO_MOD_PATH] - -Wasm dockerfile utility - -options: - -h, --help show this help message and exit - --get-version, --no-get-version - Get the current version of CosmWasm specified in wasm module. - --get-checksum, --no-get-checksum - Returns the checksum of the libwasm library for the provided version. - --wasm-library WASM_LIBRARY - The name of the library to return the checksum for. - --wasm-go-mod-path WASM_GO_MOD_PATH - The relative path to the go.mod file for the wasm module. -""" - -import argparse -import requests - -WASM_IMPORT = "github.com/CosmWasm/wasmvm/v2" - - -def _get_wasm_version(wasm_go_mod_path: str) -> str: - """get the version of the cosm wasm module from the go.mod file""" - with open(wasm_go_mod_path, "r") as f: - for line in f: - if WASM_IMPORT in line: - return _extract_wasm_version(line) - raise ValueError(f"Could not find {WASM_IMPORT} in {wasm_go_mod_path}") - - -def _get_wasm_lib_checksum(wasm_version: str, wasm_lib: str) -> str: - """get the checksum of the wasm library for the given version""" - checksums_url = f"https://github.com/CosmWasm/wasmvm/releases/download/{wasm_version}/checksums.txt" - resp = requests.get(checksums_url) - resp.raise_for_status() - - for line in resp.text.splitlines(): - if wasm_lib in line: - return line.split(" ")[0].strip() - - raise ValueError(f"Could not find {wasm_lib} in {checksums_url}") - - -def _extract_wasm_version(line: str) -> str: - """extract the version from a line in the go.mod file""" - return line.split(" ")[1].strip() - - -def _parse_args() -> argparse.Namespace: - parser = argparse.ArgumentParser(description="Wasm dockerfile utility") - - group = parser.add_mutually_exclusive_group() - group.add_argument( - "--get-version", - action=argparse.BooleanOptionalAction, - help="Get the current version of CosmWasm specified in wasm module.", - ) - group.add_argument( - "--get-checksum", - action=argparse.BooleanOptionalAction, - help="Returns the checksum of the libwasm library for the provided version." - ) - parser.add_argument( - "--wasm-library", - default="libwasmvm_muslc.x86_64.a", - help="The name of the library to return the checksum for." - ) - parser.add_argument( - "--wasm-go-mod-path", - default="modules/light-clients/08-wasm/go.mod", - help="The relative path to the go.mod file for the wasm module." - ) - return parser.parse_args() - - -def main(args: argparse.Namespace): - if args.get_version: - version = _get_wasm_version(args.wasm_go_mod_path) - print(version) - return - if args.get_checksum: - checksum = _get_wasm_lib_checksum(_get_wasm_version(args.wasm_go_mod_path), args.wasm_library) - print(checksum) - return - - -if __name__ == "__main__": - main(_parse_args()) diff --git a/scripts/get-libwasm-version.sh b/scripts/get-libwasm-version.sh new file mode 100644 index 00000000000..853ece4ccf1 --- /dev/null +++ b/scripts/get-libwasm-version.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env bash + +# Constants +WASM_IMPORT="github.com/CosmWasm/wasmvm/v2" + +# Functions +function show_help { + echo "Wasm dockerfile utility + +Usage: $0 [--get-version | --get-checksum] [--wasm-library ] [--wasm-go-mod-path ] + +Options: + --get-version Get the current version of CosmWasm specified in wasm module. + --get-checksum Returns the checksum of the libwasm library for the provided version. + --wasm-library The name of the library to return the checksum for (default: libwasmvm_muslc.x86_64.a). + --wasm-go-mod-path The relative path to the go.mod file for the wasm module (default: modules/light-clients/08-wasm/go.mod). +" +} + +function get_wasm_version { + local wasm_go_mod_path="$1" + if [[ ! -f "$wasm_go_mod_path" ]]; then + echo "Error: go.mod file not found at $wasm_go_mod_path" + exit 1 + fi + + grep "$WASM_IMPORT" "$wasm_go_mod_path" | awk '{print $2}' || { + echo "Error: Could not find $WASM_IMPORT in $wasm_go_mod_path" + exit 1 + } +} + +function get_wasm_lib_checksum { + local wasm_version="$1" + local wasm_lib="$2" + local checksums_url="https://github.com/CosmWasm/wasmvm/releases/download/${wasm_version}/checksums.txt" + + local checksum + checksum=$(curl -sSL "$checksums_url" | grep "$wasm_lib" | awk '{print $1}') || { + echo "Error: Could not retrieve checksum for $wasm_lib from $checksums_url" + exit 1 + } + + if [[ -z "$checksum" ]]; then + echo "Error: Could not find $wasm_lib in $checksums_url" + exit 1 + fi + + echo "$checksum" +} + +# Default values +wasm_library="libwasmvm_muslc.x86_64.a" +wasm_go_mod_path="modules/light-clients/08-wasm/go.mod" + +# Parse arguments +get_version=false +get_checksum=false + +while [[ $# -gt 0 ]]; do + case "$1" in + --get-version) + get_version=true + shift + ;; + --get-checksum) + get_checksum=true + shift + ;; + --wasm-library) + wasm_library="$2" + shift 2 + ;; + --wasm-go-mod-path) + wasm_go_mod_path="$2" + shift 2 + ;; + -h|--help) + show_help + exit 0 + ;; + *) + echo "Error: Unknown argument $1" + show_help + exit 1 + ;; + esac +done + +# Main logic +if $get_version; then + get_wasm_version "$wasm_go_mod_path" + exit 0 +fi + +if $get_checksum; then + wasm_version=$(get_wasm_version "$wasm_go_mod_path") + get_wasm_lib_checksum "$wasm_version" "$wasm_library" + exit 0 +fi + +echo "Error: No action specified. Use --get-version or --get-checksum." +show_help +exit 1