Skip to content

Commit

Permalink
Merge pull request #1100 from messense/pyproject-toml-readme-license
Browse files Browse the repository at this point in the history
Change readme and license paths in `pyproject.toml` to be relative to `pyproject.toml`
  • Loading branch information
messense authored Sep 12, 2022
2 parents df2b539 + ac98e08 commit 09b15ce
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ 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)
It's technically a **breaking change**, but previously it doesn't work properly.

## [0.13.2] - 2022-08-14

Expand Down
14 changes: 7 additions & 7 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ fn path_to_content_type(path: &Path) -> String {
impl Metadata21 {
/// Merge metadata with pyproject.toml, where pyproject.toml takes precedence
///
/// manifest_path must be the directory, not the file
/// pyproject_dir must be the directory containing pyproject.toml
pub fn merge_pyproject_toml(
&mut self,
manifest_path: impl AsRef<Path>,
pyproject_dir: impl AsRef<Path>,
pyproject_toml: &PyProjectToml,
) -> Result<()> {
let manifest_path = manifest_path.as_ref();
let pyproject_dir = pyproject_dir.as_ref();
if let Some(project) = &pyproject_toml.project {
self.name = project.name.clone();

Expand All @@ -104,7 +104,7 @@ impl Metadata21 {

match &project.readme {
Some(pyproject_toml::ReadMe::RelativePath(readme_path)) => {
let readme_path = manifest_path.join(readme_path);
let readme_path = pyproject_dir.join(readme_path);
let description = Some(fs::read_to_string(&readme_path).context(format!(
"Failed to read readme specified in pyproject.toml, which should be at {}",
readme_path.display()
Expand All @@ -121,7 +121,7 @@ impl Metadata21 {
bail!("file and text fields of 'project.readme' are mutually-exclusive, only one of them should be specified");
}
if let Some(readme_path) = file {
let readme_path = manifest_path.join(readme_path);
let readme_path = pyproject_dir.join(readme_path);
let description = Some(fs::read_to_string(&readme_path).context(format!(
"Failed to read readme specified in pyproject.toml, which should be at {}",
readme_path.display()
Expand All @@ -145,7 +145,7 @@ impl Metadata21 {
bail!("file and text fields of 'project.license' are mutually-exclusive, only one of them should be specified");
}
if let Some(license_path) = file {
let license_path = manifest_path.join(license_path);
let license_path = pyproject_dir.join(license_path);
self.license_files.push(license_path);
}
if let Some(license_text) = text {
Expand All @@ -158,7 +158,7 @@ impl Metadata21 {
// We're already emitting the License-Files metadata without issue.
// license-files.globs = ["LICEN[CS]E*", "COPYING*", "NOTICE*", "AUTHORS*"]
let license_include_targets = ["LICEN[CS]E*", "COPYING*", "NOTICE*", "AUTHORS*"];
let escaped_manifest_string = glob::Pattern::escape(manifest_path.to_str().unwrap());
let escaped_manifest_string = glob::Pattern::escape(pyproject_dir.to_str().unwrap());
let escaped_manifest_path = Path::new(&escaped_manifest_string);
for pattern in license_include_targets.iter() {
for license_path in
Expand Down
3 changes: 2 additions & 1 deletion src/project_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ impl ProjectResolver {
let mut metadata21 = Metadata21::from_cargo_toml(&cargo_toml, &manifest_dir)
.context("Failed to parse Cargo.toml into python metadata")?;
if let Some(pyproject) = pyproject {
metadata21.merge_pyproject_toml(&manifest_dir, pyproject)?;
let pyproject_dir = pyproject_file.parent().unwrap();
metadata21.merge_pyproject_toml(&pyproject_dir, pyproject)?;
}
let extra_metadata = cargo_toml.remaining_core_metadata();

Expand Down
17 changes: 17 additions & 0 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ fn rewrite_cargo_toml(
fn add_crate_to_source_distribution(
writer: &mut SDistWriter,
pyproject_toml_path: impl AsRef<Path>,
pyproject: &PyProjectToml,
manifest_path: impl AsRef<Path>,
prefix: impl AsRef<Path>,
known_path_deps: &HashMap<String, PathBuf>,
Expand Down Expand Up @@ -199,6 +200,20 @@ fn add_crate_to_source_distribution(
PathBuf::from("pyproject.toml"),
pyproject_toml_path.to_path_buf(),
));
// Add readme and license 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)));
}
}
} else {
bail!(
"pyproject.toml was not included by `cargo package`. \
Expand Down Expand Up @@ -298,6 +313,7 @@ pub fn source_distribution(
add_crate_to_source_distribution(
&mut writer,
&build_context.pyproject_toml_path,
pyproject,
&path_dep,
&root_dir.join(LOCAL_DEPENDENCIES_FOLDER).join(name),
&known_path_deps,
Expand All @@ -314,6 +330,7 @@ pub fn source_distribution(
add_crate_to_source_distribution(
&mut writer,
&build_context.pyproject_toml_path,
pyproject,
&manifest_path,
&root_dir,
&known_path_deps,
Expand Down

0 comments on commit 09b15ce

Please sign in to comment.