From 194c157390c7fdc88d597126ceed6930a1107ab5 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Mon, 29 Aug 2022 17:17:03 +0200 Subject: [PATCH] compile chain binary from remote --- atomkraft/chain/__init__.py | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/atomkraft/chain/__init__.py b/atomkraft/chain/__init__.py index 79a6456..f595916 100644 --- a/atomkraft/chain/__init__.py +++ b/atomkraft/chain/__init__.py @@ -1,8 +1,11 @@ import json +import shutil +import subprocess import time from pathlib import Path from typing import Optional +import copier import tomlkit import typer from atomkraft.utils import project, query, update @@ -43,6 +46,50 @@ def config( tomlkit.dump(data, f) +@app.command() +def remote( + path: str = typer.Argument(..., help="Local path or git URL", show_default=False), + ref: Optional[str] = typer.Option( + str, help="VCS tag/commit to use for git URL", show_default=False + ), +): + """Setup chain binary from remote or local path + + Raises: + RuntimeError: If more than one executable is compiled. + """ + chain_src_dir = project.project_root() / ".atomkraft" / "src" / Path(path).stem + + # use copier to copy to chain_src_dir (supports local or remote Git) + copier.run_auto(path, chain_src_dir, vcs_ref=ref, quiet=True) + + # to find compiled executable, we store the current timestamp + ts = time.time() + # compile the source + subprocess.run(["make", "build"], capture_output=True, cwd=chain_src_dir) + # consider the only executables that are modified after the timestamp + new_files = [ + e + for e in chain_src_dir.glob("*/*") + if e.stat().st_mtime > ts and int(oct(e.stat().st_mode)[-3]) & 1 + ] + + if len(new_files) != 1: + # there should be only one executable + raise RuntimeError("Expected one executable file") + + # store the executable in `.atomkraft/bin` + bin_dir = project.project_root() / ".atomkraft" / "bin" + bin_dir.mkdir(exist_ok=True) + binary_file = shutil.copy2(new_files[0], bin_dir) + + # update the chain config + with open(f"{project.project_root()}/chain.toml") as f: + data = update(tomlkit.load(f), "binary", str(binary_file)) + with open(f"{project.project_root()}/chain.toml", "w") as f: + tomlkit.dump(data, f) + + @app.command() def testnet( silent: bool = typer.Option(False, help="Silent mode. Print no output"),