From 9c9abf01d0e6cc18b11d69a9d56474b13c0858d9 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 9 Aug 2022 00:06:12 +0800 Subject: [PATCH] Change `python-source` to be relative to the file specifies it Now that project root is changed to the directory containing `pyproject.toml` in #1044, `python-source` specified in `Cargo.toml` should be treated as relative to `Cargo.toml`. We should also change `data` to do the same. --- Changelog.md | 1 + src/project_layout.rs | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Changelog.md b/Changelog.md index c9b85a373..f776a7583 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/src/project_layout.rs b/src/project_layout.rs index 663925698..d05519a8f 100644 --- a/src/project_layout.rs +++ b/src/project_layout.rs @@ -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}; @@ -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, @@ -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, module_name: &str, - py_src: Option>, + python_root: PathBuf, data: Option>, ) -> Result { // 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]),