Skip to content

Commit

Permalink
python_src layout
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Apr 22, 2021
1 parent ecffb57 commit 62ab6b6
Show file tree
Hide file tree
Showing 16 changed files with 423 additions and 6 deletions.
25 changes: 20 additions & 5 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,30 @@ 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> {
let python_package_dir = project_root.as_ref().join(module_name);
if python_package_dir.is_dir() {
if !python_package_dir.join("__init__.py").is_file() {
bail!("Found a directory with the module name ({}) next to Cargo.toml, which indicates a mixed python/rust project, but the directory didn't contain an __init__.py file.", module_name)
let python_dir_root = project_root.as_ref().join(module_name);
let python_dir_python = project_root.as_ref().join("python_src").join(module_name);
let python_dir;
if python_dir_root.is_dir() {
python_dir = Some(python_dir_root);
} else if python_dir_python.is_dir() {
python_dir = Some(python_dir_python);
} else {
python_dir = None;
}

if let Some(python_dir) = python_dir {
if !python_dir.join("__init__.py").is_file() {
bail!(
"Found a directory with the module name ({}) next to Cargo.toml, \
which indicates a mixed python/rust project, \
but the directory didn't contain an __init__.py file.",
module_name
)
}

println!("🍹 Building a mixed python/rust project");

Ok(ProjectLayout::Mixed(python_package_dir))
Ok(ProjectLayout::Mixed(python_dir))
} else {
Ok(ProjectLayout::PureRust)
}
Expand Down
2 changes: 1 addition & 1 deletion test-crates/pyo3-mixed/pyo3_mixed/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .python_module.double import double
from .my_submodule.double import double
from .pyo3_mixed import get_21


Expand Down
283 changes: 283 additions & 0 deletions test-crates/pyo3-src-layout/Cargo.lock

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

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

[package.metadata.maturin.scripts]
get_42 = "pyo3_mixed:get_42"

[package.metadata.maturin]
classifier = [
"Programming Language :: Python",
"Programming Language :: Rust"
]

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

[lib]
name = "pyo3_src_layout"
crate-type = ["cdylib"]
30 changes: 30 additions & 0 deletions test-crates/pyo3-src-layout/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_src_layout

assert pyo3_src_layout.get_42() == 42

print("SUCCESS")
3 changes: 3 additions & 0 deletions test-crates/pyo3-src-layout/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["maturin>=0.10,<0.11"]
build-backend = "maturin"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .my_submodule.double import double
from .pyo3_src_layout import get_21


def get_42() -> int:
return double(get_21)
Empty file.
Loading

0 comments on commit 62ab6b6

Please sign in to comment.