Skip to content

Commit

Permalink
fix: expose Rust functions to Python for CLI use
Browse files Browse the repository at this point in the history
- Added Python bindings for encrypt_from_file
- Added Python bindings for decrypt_from_storage
- Added Python bindings for streaming_decrypt_from_storage
- Updated __all__ to expose new functions
- Fixed CLI test dependencies

This fixes the CLI test failures by exposing the necessary Rust
functions to Python for use in the CLI implementation.
  • Loading branch information
dirvine committed Nov 29, 2024
1 parent 13b4b56 commit 0806b0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ module-name = "self_encryption._self_encryption"
python-packages = ["self_encryption"]
include = ["self_encryption/**/*"]
manifest-path = "Cargo.toml"
develop = true

[project.scripts]
self-encryption = "self_encryption.cli:cli"
10 changes: 7 additions & 3 deletions self_encryption/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
try:
from . import _self_encryption
from _self_encryption import *
from ._self_encryption import *
from .cli import cli
except ImportError as e:
import sys
print(f"Error importing self_encryption: {e}", file=sys.stderr)
raise

__all__ = ['cli']
__all__ = [
'cli',
'encrypt_from_file',
'decrypt_from_storage',
'streaming_decrypt_from_storage',
]
30 changes: 26 additions & 4 deletions src/python.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
use pyo3::prelude::*;

#[pymodule]
fn self_encryption(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn _self_encryption(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
// Define constants directly rather than using macros
m.add("MIN_CHUNK_SIZE", 1)?; // From lib.rs
m.add("MIN_ENCRYPTABLE_BYTES", 3)?; // 3 * MIN_CHUNK_SIZE
m.add("MAX_CHUNK_SIZE", 1024 * 1024)?; // 1MiB default
m.add("MIN_CHUNK_SIZE", 1)?; // From lib.rs
m.add("MIN_ENCRYPTABLE_BYTES", 3)?; // 3 * MIN_CHUNK_SIZE
m.add("MAX_CHUNK_SIZE", 1024 * 1024)?; // 1MiB default
m.add("COMPRESSION_QUALITY", 6)?;

// Expose functions needed by CLI
m.add_function(wrap_pyfunction!(encrypt_from_file, m)?)?;
m.add_function(wrap_pyfunction!(decrypt_from_storage, m)?)?;
m.add_function(wrap_pyfunction!(streaming_decrypt_from_storage, m)?)?;

Ok(())
}

#[pyfunction]
fn encrypt_from_file(file_path: &str, output_dir: &str) -> PyResult<(DataMap, Vec<XorName>)> {
let path = std::path::Path::new(file_path);
let out_path = std::path::Path::new(output_dir);
Ok(crate::encrypt_from_file(path, out_path)?)
}

#[pyfunction]
fn decrypt_from_storage(data_map: &DataMap, output_file: &str, chunks_dir: &str) -> PyResult<()> {
let out_path = std::path::Path::new(output_file);
let chunks_path = std::path::Path::new(chunks_dir);
Ok(crate::decrypt_from_storage(data_map, out_path, |hash| {
let chunk_path = chunks_path.join(hex::encode(hash));
std::fs::read(chunk_path).map_err(|e| Error::Generic(e.to_string()))
})?)
}

0 comments on commit 0806b0e

Please sign in to comment.