Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: src layout for python #501

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,25 @@ To create a mixed rust/python project, create a folder with your module name (i.
my-project
├── Cargo.toml
├── my_project
   ├── __init__.py
   └── bar.py
├── __init__.py
└── bar.py
├── Readme.md
└── src
   └── lib.rs
└── lib.rs
```

Alternatively, you can also put the python sources in a folder called `python_src`, which resembles the [src layout](https://hynek.me/articles/testing-packaging/#src) without conflicting with rust's `src` directory:

```
my-project
├── Cargo.toml
├── python_src
│ └── my_project
│ ├── __init__.py
│ └── bar.py
├── Readme.md
└── src
└── lib.rs
```

maturin will add the native extension as a module in your python folder. When using develop, maturin will copy the native library and for cffi also the glue code to your python folder. You should add those files to your gitignore.
Expand Down
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"]
Loading