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

First crack at making some tools install with pip #11

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ license = "Apache-2.0"
keywords = ["chia", "chialisp", "clvm"]
categories = ["command-line-utilities"]

[package.metadata.maturin]
python-source = "python"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ Theory of operation of the modern compiler: ./HOW_CHIALISP_IS_COMPILED.md
This repo can be installed via cargo

cargo install clvm_tools_rs


or via pip

pip install clvm_tools_rs@git+https://github.com/Chia-Network/clvm_tools_rs.git@e17412032aa7d3b8b1d1f931893fb5802eee626a

Note: `pip` installs a subset of the tools installed by `cargo`, including `brun`, `run`, `opc` and `opd`.


The most current version of the language is in the rollup branch:

20221005-rollup
Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ build-backend = "maturin"

[tool.maturin]
bindings = "pyo3"

[project]
name = "clvm_tools_rs"

[project.scripts]
brun = "clvm_tools_rs.cmds:brun"
run = "clvm_tools_rs.cmds:run"
opc = "clvm_tools_rs.cmds:opc"
opd = "clvm_tools_rs.cmds:opd"
cldb = "clvm_tools_rs.cmds:cldb"
1 change: 1 addition & 0 deletions python/clvm_tools_rs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .clvm_tools_rs import *
16 changes: 16 additions & 0 deletions python/clvm_tools_rs/cmds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sys

from clvm_tools_rs.clvm_tools_rs import cmds


# this dynamic bit of programming may look super-complicated,
# but it's just me being lazy and automating with a macro to
# produce code of the form:
# brun = lambda: cmds.brun_main(sys.argv)
# run = lambda: cmds.run_main(sys.argv)
# etc.


for cmd in "brun run opc opd cldb shrink clisp_to_json repl".split():
code = f"{cmd} = lambda: cmds.{cmd}_main(sys.argv)"
exec(code)
4 changes: 4 additions & 0 deletions src/py/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ use crate::util::version;

use crate::py::pyval::{clvm_value_to_python, python_value_to_clvm};

use super::cmds::create_cmds_module;

create_exception!(mymodule, CldbError, PyException);
create_exception!(mymodule, CompError, PyException);

Expand Down Expand Up @@ -370,6 +372,8 @@ pub fn compose_run_function(

#[pymodule]
fn clvm_tools_rs(py: Python, m: &PyModule) -> PyResult<()> {
m.add_submodule(create_cmds_module(py)?)?;

m.add("CldbError", py.get_type::<CldbError>())?;
m.add("CompError", py.get_type::<CompError>())?;

Expand Down
39 changes: 39 additions & 0 deletions src/py/cmds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use pyo3::prelude::*;

use crate::classic::clvm_tools::cmds;

#[pyfunction]
pub fn brun_main(args: Vec<String>) {
cmds::brun(&args);
}

#[pyfunction]
pub fn run_main(args: Vec<String>) {
cmds::run(&args);
}

#[pyfunction]
pub fn opc_main(args: Vec<String>) {
cmds::opc(&args);
}

#[pyfunction]
pub fn opd_main(args: Vec<String>) {
cmds::opd(&args);
}

#[pyfunction]
pub fn cldb_main(args: Vec<String>) {
cmds::cldb(&args);
}

/// A Python module implemented in Rust.
pub fn create_cmds_module(py: Python) -> PyResult<&'_ PyModule> {
let m = PyModule::new(py, "cmds")?;
m.add_function(wrap_pyfunction!(brun_main, m)?)?;
m.add_function(wrap_pyfunction!(run_main, m)?)?;
m.add_function(wrap_pyfunction!(opc_main, m)?)?;
m.add_function(wrap_pyfunction!(opd_main, m)?)?;
m.add_function(wrap_pyfunction!(cldb_main, m)?)?;
Ok(m)
}
1 change: 1 addition & 0 deletions src/py/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod api;
mod cmds;
pub mod pyval;