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

Change python-source to be relative to the file specifies it #1049

Merged
merged 1 commit into from
Aug 9, 2022
Merged
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
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