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
sebpuetz committed Jul 26, 2020
1 parent 5ef33e4 commit 658b25f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased


### Added

* `[tool.maturin]` now supports `sdist-include = ["path/**/*"]` to
include arbitrary files in source distributions ([#296](https://github.com/PyO3/maturin/pull/296)).
* cffi is installed if it's missing and python is running inside a virtualenv.
* Enable mixed layouts with Python source in different sub-directories.

## 0.8.0 - 2020-04-03

Expand Down
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ my-project
   └── lib.rs
```

You can also specify the directory housing the package through the `--py-src` argument.

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.

With cffi you can do `from .my_project import lib` and then use `lib.my_native_function`, with pyo3/rust-cpython you can directly `from .my_project import my_native_function`.
Expand Down
15 changes: 13 additions & 2 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,19 @@ 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);
pub fn determine(
project_root: impl AsRef<Path>,
module_name: &str,
py_src: Option<impl AsRef<Path>>,
) -> Result<ProjectLayout> {
let python_package_dir = if let Some(py_src) = py_src {
project_root
.as_ref()
.join(py_src.as_ref())
.join(module_name)
} else {
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)
Expand Down
6 changes: 5 additions & 1 deletion src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub struct BuildOptions {
/// directory in the project's target directory
#[structopt(short, long, parse(from_os_str))]
pub out: Option<PathBuf>,
/// Path to the Python source in mixed projects.
#[structopt(long, parse(from_os_str))]
pub py_src: Option<PathBuf>,
/// [deprecated, use --manylinux instead] Don't check for manylinux compliance
#[structopt(long = "skip-auditwheel")]
pub skip_auditwheel: bool,
Expand All @@ -80,6 +83,7 @@ impl Default for BuildOptions {
interpreter: Some(vec![]),
bindings: None,
manifest_path: PathBuf::from("Cargo.toml"),
py_src: None,
out: None,
skip_auditwheel: false,
target: None,
Expand Down Expand Up @@ -120,7 +124,7 @@ impl BuildOptions {
.unwrap_or_else(|| &cargo_toml.package.name)
.to_owned();

let project_layout = ProjectLayout::determine(manifest_dir, &module_name)?;
let project_layout = ProjectLayout::determine(manifest_dir, &module_name, self.py_src)?;

let target = Target::from_target_triple(self.target.clone())?;

Expand Down
5 changes: 4 additions & 1 deletion src/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use crate::PythonInterpreter;
use crate::Target;
use anyhow::{anyhow, format_err, Context, Result};
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};

/// Installs a crate by compiling it and copying the shared library to the right directory
///
/// Works only in a virtualenv.
#[allow(clippy::too_many_arguments)]
pub fn develop(
bindings: Option<String>,
manifest_file: &Path,
Expand All @@ -20,6 +21,7 @@ pub fn develop(
venv_dir: &Path,
release: bool,
strip: bool,
py_src: Option<PathBuf>,
) -> Result<()> {
let target = Target::from_target_triple(None)?;

Expand All @@ -32,6 +34,7 @@ pub fn develop(
manifest_path: manifest_file.to_path_buf(),
out: None,
skip_auditwheel: false,
py_src,
target: None,
cargo_extra_args,
rustc_extra_args,
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ enum Opt {
/// Use as `--rustc-extra-args="--my-arg"`
#[structopt(long = "rustc-extra-args")]
rustc_extra_args: Vec<String>,
/// Path to the Python source in mixed projects.
#[structopt(long, parse(from_os_str))]
py_src: Option<PathBuf>,
},
/// Build only a source distribution (sdist) without compiling.
///
Expand Down Expand Up @@ -438,6 +441,7 @@ fn run() -> Result<()> {
rustc_extra_args,
release,
strip,
py_src,
} => {
let venv_dir = match (env::var_os("VIRTUAL_ENV"),env::var_os("CONDA_PREFIX")) {
(Some(dir), None) => PathBuf::from(dir),
Expand All @@ -456,6 +460,7 @@ fn run() -> Result<()> {
&venv_dir,
release,
strip,
py_src,
)?;
}
Opt::SDist { manifest_path, out } => {
Expand Down
1 change: 1 addition & 0 deletions tests/test_develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn test_develop(package: impl AsRef<Path>, bindings: Option<String>) -> Result<(
&venv_dir,
false,
cfg!(feature = "faster-tests"),
None,
)?;

check_installed(&package.as_ref(), &python)?;
Expand Down

0 comments on commit 658b25f

Please sign in to comment.