diff --git a/Cargo.toml b/Cargo.toml index 1d49b2b31..d1ac33e9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/README.md b/README.md index bf7051a8d..f6bd23e86 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pyproject.toml b/pyproject.toml index ff09300bf..d51676c56 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/python/clvm_tools_rs/__init__.py b/python/clvm_tools_rs/__init__.py new file mode 100644 index 000000000..43342043f --- /dev/null +++ b/python/clvm_tools_rs/__init__.py @@ -0,0 +1 @@ +from .clvm_tools_rs import * diff --git a/python/clvm_tools_rs/cmds.py b/python/clvm_tools_rs/cmds.py new file mode 100644 index 000000000..90b849e54 --- /dev/null +++ b/python/clvm_tools_rs/cmds.py @@ -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) diff --git a/src/py/api.rs b/src/py/api.rs index 3b64b7b91..4fe093b8e 100644 --- a/src/py/api.rs +++ b/src/py/api.rs @@ -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); @@ -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::())?; m.add("CompError", py.get_type::())?; diff --git a/src/py/cmds.rs b/src/py/cmds.rs new file mode 100644 index 000000000..e1a6516b0 --- /dev/null +++ b/src/py/cmds.rs @@ -0,0 +1,39 @@ +use pyo3::prelude::*; + +use crate::classic::clvm_tools::cmds; + +#[pyfunction] +pub fn brun_main(args: Vec) { + cmds::brun(&args); +} + +#[pyfunction] +pub fn run_main(args: Vec) { + cmds::run(&args); +} + +#[pyfunction] +pub fn opc_main(args: Vec) { + cmds::opc(&args); +} + +#[pyfunction] +pub fn opd_main(args: Vec) { + cmds::opd(&args); +} + +#[pyfunction] +pub fn cldb_main(args: Vec) { + 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) +} diff --git a/src/py/mod.rs b/src/py/mod.rs index bebfa93e5..748696197 100644 --- a/src/py/mod.rs +++ b/src/py/mod.rs @@ -1,2 +1,3 @@ pub mod api; +mod cmds; pub mod pyval;