Skip to content

Commit

Permalink
make entropython directly installable with pip
Browse files Browse the repository at this point in the history
  • Loading branch information
jstucke committed Aug 9, 2024
1 parent 0f05511 commit 5753b3a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 28 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
*.egg-info
.idea
.vscode
/target
Cargo.lock
entropython.so
build
dist
12 changes: 5 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "entropython"
version = "1.0.2"
version = "1.1.0"
authors = ["Enkelmann <[email protected]>"]
edition = "2018"
description = "A Python module for efficient calculation of Shannon byte entropy based on Rust."
Expand All @@ -11,12 +11,10 @@ license = "MIT"
license-file = "LICENSE"
keywords = ["shannon", "entropy"]


[lib]
name = "entropython"
name = "libentropython"
path = "rust/lib.rs"
crate-type = ["cdylib"]

[dependencies.pyo3]
version = "0.17.1"
features = ["extension-module"]

[dependencies]
pyo3 = { version = "0.17.1", features = ["extension-module"] }
5 changes: 5 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
graft python
graft rust
include Cargo.toml
global-exclude */__pycache__/*
global-exclude *.pyc
35 changes: 17 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@ A Python module for efficient calculation of Shannon byte entropy based on Rust.

## Installation

Just run
To install the version from pypi.org run
```shell
pip install entropython
```

To install the latest version directly from GitHub run
```shell
pip git+https://github.com/fkie-cad/entropython
```
(you need to [have rust installed](https://www.rust-lang.org/tools/install))
or check out the repository and run
```shell
pip install .
```

## Usage

```python
from entropython import shannon_entropy, metric_entropy

bytes = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.'.encode('utf-8')
print(shannon_entropy(bytes))
print(metric_entropy(bytes))
```

## Build from Source

For building the binary from source, [Rust](https://www.rust-lang.org/) needs to be installed.

Run
```shell
cargo build --release
mv target/release/libentropython.so entropython.so # The renaming is necessary for Python to find the module
# Optional: Remove debug symbols from the binary to dramatically reduce its size.
strip entropython.so
byte_str = (
b'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod '
b'tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.'
)
print(shannon_entropy(byte_str))
print(metric_entropy(byte_str))
```

The built `entropython.so` itself has no dependencies.
19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[build-system]
requires = [
"setuptools",
"setuptools-rust",
"wheel"
]
build-backend = "setuptools.build_meta"

[project]
name = "entropython"
version = "1.1.0"

[tool.setuptools.packages]
find = { where = ["python"] }

[[tool.setuptools-rust.ext-modules]]
target = "entropython.libentropython"
path = "Cargo.toml"
binding = "PyO3"
6 changes: 6 additions & 0 deletions python/entropython/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .libentropython import shannon_entropy, metric_entropy

__all__ = [
'shannon_entropy',
'metric_entropy',
]
4 changes: 2 additions & 2 deletions src/lib.rs → rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn metric_entropy(bytes: &[u8]) -> f64 {
/// This module implements functions to compute the Shannon byte entropy of a byte array.
/// The computation itself is implemented in Rust for faster runtime.
#[pymodule]
fn entropython(_py: Python, m: &PyModule) -> PyResult<()> {
fn libentropython(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(shannon_entropy, m)?)?;
m.add_function(wrap_pyfunction!(metric_entropy, m)?)?;
Ok(())
Expand All @@ -60,4 +60,4 @@ mod tests {
assert!(shannon_entropy(&bytes) > 7.99f64);
assert!(shannon_entropy(&bytes) <= 8f64);
}
}
}

0 comments on commit 5753b3a

Please sign in to comment.