Skip to content

Commit

Permalink
Add path option for Python source
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Aug 5, 2021
1 parent 24d0ee1 commit dba8a5f
Show file tree
Hide file tree
Showing 15 changed files with 395 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,31 @@ pub enum ProjectLayout {

impl ProjectLayout {
/// Checks whether a python module exists besides Cargo.toml with the right name
pub fn determine(project_root: impl AsRef<Path>, module_name: &str) -> Result<ProjectLayout> {
pub fn determine(
project_root: impl AsRef<Path>,
module_name: &str,
py_src: Option<impl AsRef<Path>>,
) -> Result<ProjectLayout> {
// A dot in the module name means the extension module goes into the module folder specified by the path
let parts: Vec<&str> = module_name.split('.').collect();
let project_root = project_root.as_ref();
let python_root = if let Some(py_src) = py_src {
project_root.join(py_src.as_ref())
} else {
project_root.to_path_buf()
};
let (python_module, rust_module, extension_name) = if parts.len() > 1 {
let mut rust_module = project_root.to_path_buf();
rust_module.extend(&parts[0..parts.len() - 1]);
(
project_root.join(parts[0]),
python_root.join(parts[0]),
rust_module,
parts[parts.len() - 1].to_string(),
)
} else {
(
project_root.join(module_name),
project_root.join(module_name),
python_root.join(module_name),
python_root.join(module_name),
module_name.to_string(),
)
};
Expand Down
3 changes: 2 additions & 1 deletion src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ impl BuildOptions {
.filter(|name| name.contains('.'))
.unwrap_or(&module_name);

let project_layout = ProjectLayout::determine(manifest_dir, extension_name)?;
let py_src = extra_metadata.python_source.clone();
let project_layout = ProjectLayout::determine(manifest_dir, extension_name, py_src)?;

let mut cargo_extra_args = split_extra_args(&self.cargo_extra_args)?;
if let Some(ref target) = self.target {
Expand Down
1 change: 1 addition & 0 deletions src/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub struct RemainingCoreMetadata {
pub project_url: Option<HashMap<String, String>>,
pub provides_extra: Option<Vec<String>>,
pub description_content_type: Option<String>,
pub python_source: Option<String>,
}

#[cfg(test)]
Expand Down
257 changes: 257 additions & 0 deletions test-crates/pyo3-mixed-py-subdir/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test-crates/pyo3-mixed-py-subdir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
authors = ["konstin <[email protected]>"]
name = "pyo3-mixed-py-subdir"
version = "2.1.3"
description = "Implements a dummy function combining rust and python"
readme = "Readme.md"
edition = "2018"

[dependencies]
pyo3 = { version = "0.14.0", features = ["extension-module"] }

[lib]
name = "pyo3_mixed_py_subdir"
crate-type = ["cdylib"]

[package.metadata.maturin]
python-source = "python"
30 changes: 30 additions & 0 deletions test-crates/pyo3-mixed-py-subdir/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# pyo3-mixed

A package for testing maturin with a mixed pyo3/python project.

## Usage

```bash
pip install .
```

```python
import pyo3_mixed
assert pyo3_mixed.get_42() == 42
```

## Testing

Install tox:

```bash
pip install tox
```

Run it:

```bash
tox
```

The tests are in `test_pyo3_mixed.py`, while the configuration is in tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3

import pyo3_mixed_py_subdir as pyo3_mixed

assert pyo3_mixed.get_42() == 42

print("SUCCESS")
14 changes: 14 additions & 0 deletions test-crates/pyo3-mixed-py-subdir/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[build-system]
requires = ["maturin>=0.11,<0.12"]
build-backend = "maturin"

[project]
name = "pyo3-mixed-py-subdir"
classifiers = [
"Programming Language :: Python",
"Programming Language :: Rust"
]
requires-python = ">=3.6"

[project.scripts]
get_42 = "pyo3_mixed_py_subdir:get_42"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .python_module.double import double
from .pyo3_mixed_py_subdir import get_21


def get_42() -> int:
return double(get_21)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import Callable


def double(fn: Callable[[], int]) -> int:
return 2 * fn()
Loading

0 comments on commit dba8a5f

Please sign in to comment.