Skip to content

Commit

Permalink
Change python-source to be relative to the file specifies it
Browse files Browse the repository at this point in the history
Now that project root is changed to the directory containing
`pyproject.toml` in PyO3#1044, `python-source` specified in `Cargo.toml`
should be treated as relative to `Cargo.toml`.

We should also change `data` to do the same.
  • Loading branch information
messense committed Aug 8, 2022
1 parent 6bc8d5f commit 9c9abf0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Add `python-source` option to `[tool.maturin]` section of pyproject.toml in [#1046](https://github.com/PyO3/maturin/pull/1046)
* Deprecate support for specifying python metadata in `Cargo.toml` in [#1048](https://github.com/PyO3/maturin/pull/1048).
Please migrate to [PEP 621](https://peps.python.org/pep-0621/) instead.
* Change `python-source` to be relative to the file specifies it in [#1049](https://github.com/PyO3/maturin/pull/1049)

## [0.13.1] - 2022-07-26

Expand Down
28 changes: 14 additions & 14 deletions src/project_layout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{CargoToml, Metadata21, PyProjectToml};
use anyhow::{bail, format_err, Context, Result};
use std::borrow::Cow;
use std::env;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -88,18 +87,22 @@ impl ProjectResolver {
.filter(|name| name.contains('.'))
.unwrap_or(&module_name);

let py_src = pyproject
.and_then(|x| x.python_source())
.or_else(|| extra_metadata.python_source.as_ref().map(Path::new));
let data = pyproject
.and_then(|x| x.data())
.or_else(|| extra_metadata.data.as_ref().map(Path::new));
let project_root = if pyproject_file.is_file() {
pyproject_file.parent().unwrap_or(manifest_dir)
} else {
manifest_dir
};
let project_layout = ProjectLayout::determine(project_root, extension_name, py_src, data)?;
let py_root = match pyproject.and_then(|x| x.python_source()) {
Some(py_src) => py_src.to_path_buf(),
None => match extra_metadata.python_source.as_ref() {
Some(py_src) => manifest_dir.join(py_src),
None => project_root.to_path_buf(),
},
};
let data = pyproject
.and_then(|x| x.data())
.or_else(|| extra_metadata.data.as_ref().map(Path::new));
let project_layout = ProjectLayout::determine(project_root, extension_name, py_root, data)?;
Ok(Self {
project_layout,
cargo_toml_path: manifest_file,
Expand Down Expand Up @@ -152,20 +155,17 @@ impl ProjectResolver {

impl ProjectLayout {
/// Checks whether a python module exists besides Cargo.toml with the right name
pub fn determine(
fn determine(
project_root: impl AsRef<Path>,
module_name: &str,
py_src: Option<impl AsRef<Path>>,
python_root: PathBuf,
data: 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 = py_src.map_or(Cow::Borrowed(project_root), |py_src| {
Cow::Owned(project_root.join(py_src))
});
let (python_module, rust_module, extension_name) = if parts.len() > 1 {
let mut rust_module = python_root.to_path_buf();
let mut rust_module = python_root.clone();
rust_module.extend(&parts[0..parts.len() - 1]);
(
python_root.join(parts[0]),
Expand Down

0 comments on commit 9c9abf0

Please sign in to comment.