|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Cross-platform setup script for dashd and test blockchain data. |
| 3 | +
|
| 4 | +Downloads the Dash Core binary and regtest test data for integration tests. |
| 5 | +Outputs DASHD_PATH and DASHD_DATADIR lines suitable for appending to GITHUB_ENV |
| 6 | +or evaluating in a shell. |
| 7 | +
|
| 8 | +Environment variables: |
| 9 | + DASHVERSION - Dash Core version (default: 23.0.2) |
| 10 | + TEST_DATA_VERSION - Test data release version (default: v0.0.2) |
| 11 | + TEST_DATA_REPO - GitHub repo for test data (default: dashpay/regtest-blockchain) |
| 12 | + CACHE_DIR - Cache directory (default: ~/.rust-dashcore-test) |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import platform |
| 17 | +import sys |
| 18 | +import tarfile |
| 19 | +import urllib.request |
| 20 | +import zipfile |
| 21 | + |
| 22 | +DASHVERSION = os.environ.get("DASHVERSION", "23.0.2") |
| 23 | +TEST_DATA_VERSION = os.environ.get("TEST_DATA_VERSION", "v0.0.2") |
| 24 | +TEST_DATA_REPO = os.environ.get("TEST_DATA_REPO", "dashpay/regtest-blockchain") |
| 25 | + |
| 26 | + |
| 27 | +def get_cache_dir(): |
| 28 | + if "CACHE_DIR" in os.environ: |
| 29 | + return os.environ["CACHE_DIR"] |
| 30 | + home = os.environ.get("HOME") or os.environ.get("USERPROFILE") |
| 31 | + if not home: |
| 32 | + sys.exit("Cannot determine home directory: neither HOME nor USERPROFILE is set") |
| 33 | + return os.path.join(home, ".rust-dashcore-test") |
| 34 | + |
| 35 | + |
| 36 | +def get_asset_info(): |
| 37 | + """Return (asset_filename, binary_relative_path) for the current platform.""" |
| 38 | + system = platform.system() |
| 39 | + machine = platform.machine() |
| 40 | + |
| 41 | + if system == "Linux": |
| 42 | + arch = "aarch64" if machine in ("aarch64", "arm64") else "x86_64" |
| 43 | + asset = f"dashcore-{DASHVERSION}-{arch}-linux-gnu.tar.gz" |
| 44 | + elif system == "Darwin": |
| 45 | + arch = "arm64" if machine == "arm64" else "x86_64" |
| 46 | + asset = f"dashcore-{DASHVERSION}-{arch}-apple-darwin.tar.gz" |
| 47 | + elif system == "Windows": |
| 48 | + asset = f"dashcore-{DASHVERSION}-win64.zip" |
| 49 | + else: |
| 50 | + sys.exit(f"Unsupported platform: {system}") |
| 51 | + |
| 52 | + return asset |
| 53 | + |
| 54 | + |
| 55 | +def log(msg): |
| 56 | + print(msg, file=sys.stderr) |
| 57 | + |
| 58 | + |
| 59 | +def download(url, dest): |
| 60 | + log(f"Downloading {url} ...") |
| 61 | + urllib.request.urlretrieve(url, dest) |
| 62 | + |
| 63 | + |
| 64 | +def extract(archive_path, dest_dir): |
| 65 | + if archive_path.endswith(".zip"): |
| 66 | + with zipfile.ZipFile(archive_path, "r") as zf: |
| 67 | + zf.extractall(dest_dir) |
| 68 | + else: |
| 69 | + with tarfile.open(archive_path, "r:gz") as tf: |
| 70 | + tf.extractall(dest_dir) |
| 71 | + |
| 72 | + |
| 73 | +def setup_dashd(cache_dir): |
| 74 | + """Download and extract dashd binary. Returns the path to the dashd binary.""" |
| 75 | + asset = get_asset_info() |
| 76 | + dashd_dir = os.path.join(cache_dir, f"dashcore-{DASHVERSION}") |
| 77 | + |
| 78 | + ext = ".exe" if platform.system() == "Windows" else "" |
| 79 | + dashd_bin = os.path.join(dashd_dir, "bin", f"dashd{ext}") |
| 80 | + |
| 81 | + if os.path.isfile(dashd_bin): |
| 82 | + log(f"dashd {DASHVERSION} already available") |
| 83 | + return dashd_bin |
| 84 | + |
| 85 | + log(f"Downloading dashd {DASHVERSION}...") |
| 86 | + archive_path = os.path.join(cache_dir, asset) |
| 87 | + url = f"https://github.com/dashpay/dash/releases/download/v{DASHVERSION}/{asset}" |
| 88 | + download(url, archive_path) |
| 89 | + extract(archive_path, cache_dir) |
| 90 | + os.remove(archive_path) |
| 91 | + log(f"Downloaded dashd to {dashd_dir}") |
| 92 | + |
| 93 | + if not os.path.isfile(dashd_bin): |
| 94 | + sys.exit(f"Expected binary not found after extraction: {dashd_bin}") |
| 95 | + |
| 96 | + return dashd_bin |
| 97 | + |
| 98 | + |
| 99 | +def setup_test_data(cache_dir): |
| 100 | + """Download and extract test blockchain data. Returns the datadir path.""" |
| 101 | + test_data_dir = os.path.join( |
| 102 | + cache_dir, f"regtest-blockchain-{TEST_DATA_VERSION}", "regtest-40000" |
| 103 | + ) |
| 104 | + blocks_dir = os.path.join(test_data_dir, "regtest", "blocks") |
| 105 | + |
| 106 | + if os.path.isdir(blocks_dir): |
| 107 | + log(f"Test blockchain data {TEST_DATA_VERSION} already available") |
| 108 | + return test_data_dir |
| 109 | + |
| 110 | + log(f"Downloading test blockchain data {TEST_DATA_VERSION}...") |
| 111 | + parent_dir = os.path.join(cache_dir, f"regtest-blockchain-{TEST_DATA_VERSION}") |
| 112 | + os.makedirs(parent_dir, exist_ok=True) |
| 113 | + |
| 114 | + archive_path = os.path.join(cache_dir, "regtest-40000.tar.gz") |
| 115 | + url = f"https://github.com/{TEST_DATA_REPO}/releases/download/{TEST_DATA_VERSION}/regtest-40000.tar.gz" |
| 116 | + download(url, archive_path) |
| 117 | + extract(archive_path, parent_dir) |
| 118 | + os.remove(archive_path) |
| 119 | + log(f"Downloaded test data to {test_data_dir}") |
| 120 | + |
| 121 | + return test_data_dir |
| 122 | + |
| 123 | + |
| 124 | +def main(): |
| 125 | + cache_dir = get_cache_dir() |
| 126 | + os.makedirs(cache_dir, exist_ok=True) |
| 127 | + |
| 128 | + dashd_path = setup_dashd(cache_dir) |
| 129 | + datadir = setup_test_data(cache_dir) |
| 130 | + |
| 131 | + # Output lines for GITHUB_ENV or shell eval |
| 132 | + print(f"DASHD_PATH={dashd_path}") |
| 133 | + print(f"DASHD_DATADIR={datadir}") |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + main() |
0 commit comments