From a273d11860b8ad24e22fd0c5422913096a47a4f2 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 13 Sep 2022 09:40:55 +0800 Subject: [PATCH] Change `sdist-include` paths to be relative to `pyproject.toml` --- Changelog.md | 3 +- src/source_distribution.rs | 59 +++++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/Changelog.md b/Changelog.md index 021f9844e..c05fcfd14 100644 --- a/Changelog.md +++ b/Changelog.md @@ -17,9 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Add library search paths in Cargo target directory to rpath in editable mode on Linux in [#1094](https://github.com/PyO3/maturin/pull/1094) * Remove default manifest path for `maturin sdist` command in [#1097](https://github.com/PyO3/maturin/pull/1097) * Fix sdist when `pyproject.toml` isn't in the same dir of `Cargo.toml` in [#1099](https://github.com/PyO3/maturin/pull/1099) -* Change readme and license paths in `pyproject.toml` to be relative to `pyproject.toml` in [#1100](https://github.com/PyO3/maturin/pull/1100) +* Change readme and license paths in `pyproject.toml` to be relative to `pyproject.toml` in [#1100](https://github.com/PyO3/maturin/pull/1100). It's technically a **breaking change**, but previously it doesn't work properly. * Add python source files specified in pyproject.toml to sdist in [#1102](https://github.com/PyO3/maturin/pull/1102) +* Change `sdist-include` paths to be relative to `pyproject.toml` in [#1103](https://github.com/PyO3/maturin/pull/1103) ## [0.13.2] - 2022-08-14 diff --git a/src/source_distribution.rs b/src/source_distribution.rs index 04b202a69..eaddce37a 100644 --- a/src/source_distribution.rs +++ b/src/source_distribution.rs @@ -123,7 +123,6 @@ fn rewrite_cargo_toml( fn add_crate_to_source_distribution( writer: &mut SDistWriter, pyproject_toml_path: impl AsRef, - pyproject: &PyProjectToml, manifest_path: impl AsRef, prefix: impl AsRef, known_path_deps: &HashMap, @@ -202,27 +201,6 @@ fn add_crate_to_source_distribution( PathBuf::from("pyproject.toml"), pyproject_toml_path.to_path_buf(), )); - // Add readme, license and python source files - if let Some(project) = pyproject.project.as_ref() { - if let Some(pyproject_toml::ReadMe::RelativePath(readme)) = project.readme.as_ref() - { - target_source.push((PathBuf::from(readme), pyproject_dir.join(readme))); - } - if let Some(pyproject_toml::License { - file: Some(license), - text: None, - }) = project.license.as_ref() - { - target_source.push((PathBuf::from(license), pyproject_dir.join(license))); - } - if let Some(python_source) = pyproject.python_source() { - for entry in ignore::Walk::new(pyproject_dir.join(python_source)) { - let path = entry?.into_path(); - let relative_path = path.strip_prefix(&pyproject_dir)?; - target_source.push((relative_path.to_path_buf(), path)); - } - } - } } else { bail!( "pyproject.toml was not included by `cargo package`. \ @@ -323,7 +301,6 @@ pub fn source_distribution( add_crate_to_source_distribution( &mut writer, &pyproject_toml_path, - pyproject, &path_dep, &root_dir.join(LOCAL_DEPENDENCIES_FOLDER).join(name), &known_path_deps, @@ -340,7 +317,6 @@ pub fn source_distribution( add_crate_to_source_distribution( &mut writer, &pyproject_toml_path, - pyproject, &manifest_path, &root_dir, &known_path_deps, @@ -356,15 +332,44 @@ pub fn source_distribution( writer.add_file(&target, &cargo_lock_path)?; } + // Add readme, license and python source files + let pyproject_dir = pyproject_toml_path.parent().unwrap(); + if let Some(project) = pyproject.project.as_ref() { + if let Some(pyproject_toml::ReadMe::RelativePath(readme)) = project.readme.as_ref() { + writer.add_file(root_dir.join(readme), pyproject_dir.join(readme))?; + } + if let Some(pyproject_toml::License { + file: Some(license), + text: None, + }) = project.license.as_ref() + { + writer.add_file(root_dir.join(license), pyproject_dir.join(license))?; + } + if let Some(python_source) = pyproject.python_source() { + for entry in ignore::Walk::new(pyproject_dir.join(python_source)) { + let source = entry?.into_path(); + let target = root_dir.join(source.strip_prefix(&pyproject_dir)?); + if source.is_dir() { + writer.add_directory(target)?; + } else { + writer.add_file(target, &source)?; + } + } + } + } if let Some(include_targets) = pyproject.sdist_include() { for pattern in include_targets { println!("📦 Including files matching \"{}\"", pattern); - for source in glob::glob(&manifest_dir.join(pattern).to_string_lossy()) + for source in glob::glob(&pyproject_dir.join(pattern).to_string_lossy()) .expect("No files found for pattern") .filter_map(Result::ok) { - let target = root_dir.join(&source.strip_prefix(manifest_dir)?); - writer.add_file(target, source)?; + let target = root_dir.join(&source.strip_prefix(pyproject_dir)?); + if source.is_dir() { + writer.add_directory(target)?; + } else { + writer.add_file(target, source)?; + } } } }