Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: compile chain binary from remote #112

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions atomkraft/chain/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"),
Expand Down