Skip to content

Commit

Permalink
Package license files in .dist-info
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Mar 11, 2022
1 parent 9d9ee0e commit 2cee279
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* Package license files in `.dist-info/license_files` following PEP 639 in [#837](https://github.com/PyO3/maturin/pull/837)
* Stop testing Python 3.6 on CI since it's already EOL in [#840](https://github.com/PyO3/maturin/pull/840)

## [0.12.10] - 2022-03-09

* Add support for `pyo3-ffi` by ijl in [#804](https://github.com/PyO3/maturin/pull/804)
Expand Down
42 changes: 15 additions & 27 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct Metadata21 {
pub maintainer: Option<String>,
pub maintainer_email: Option<String>,
pub license: Option<String>,
// https://peps.python.org/pep-0639/#license-file-multiple-use
pub license_files: Vec<PathBuf>,
pub classifiers: Vec<String>,
pub requires_dist: Vec<String>,
pub provides_dist: Vec<String>,
Expand Down Expand Up @@ -144,10 +146,7 @@ impl Metadata21 {
}
if let Some(license_path) = file {
let license_path = manifest_path.join(license_path);
self.license = Some(fs::read_to_string(&license_path).context(format!(
"Failed to read license file specified in pyproject.toml, which should be at {}",
license_path.display()
))?);
self.license_files.push(license_path);
}
if let Some(license_text) = text {
self.license = Some(license_text.clone());
Expand Down Expand Up @@ -351,6 +350,7 @@ impl Metadata21 {
author: authors,
author_email,
license: cargo_toml.package.license.clone(),
license_files: Vec::new(),

// Values provided through `[project.metadata.maturin]`
classifiers,
Expand Down Expand Up @@ -404,6 +404,13 @@ impl Metadata21 {
add_vec("Requires-External", &self.requires_external);
add_vec("Provides-Extra", &self.provides_extra);

let license_files: Vec<String> = self
.license_files
.iter()
.map(|path| path.file_name().unwrap().to_str().unwrap().to_string())
.collect();
add_vec("License-File", &license_files);

let mut add_option = |name, value: &Option<String>| {
if let Some(some) = value.clone() {
fields.push((name, some));
Expand Down Expand Up @@ -791,33 +798,14 @@ mod test {
"boltons; (sys_platform == 'win32') and extra == 'test'"
]
);
assert_eq!(metadata.license.as_ref().unwrap(), "MIT");

let license_file = &metadata.license_files[0];
assert_eq!(license_file.file_name().unwrap(), "LICENSE");

let content = metadata.to_file_contents();
let pkginfo: Result<python_pkginfo::Metadata, _> = content.parse();
assert!(pkginfo.is_ok());

let license = metadata
.to_vec()
.into_iter()
.filter_map(|(key, val)| {
if key.starts_with("License") {
Some(val)
} else {
None
}
})
.next()
.unwrap();
let mut lines = license.split_inclusive('\n');
assert_eq!(
lines.next().unwrap(),
"Copyright (c) 2018-present konstin\r\n"
);
assert_eq!(lines.next().unwrap(), "\t\r\n");
assert_eq!(
lines.next().unwrap(),
"\tPermission is hereby granted, free of charge, to any\r\n"
);
}

#[test]
Expand Down
11 changes: 11 additions & 0 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,5 +833,16 @@ pub fn write_dist_info(
)?;
}

if !metadata21.license_files.is_empty() {
let license_files_dir = dist_info_dir.join("license_files");
writer.add_directory(&license_files_dir)?;
for path in &metadata21.license_files {
let filename = path.file_name().with_context(|| {
format!("missing file name for license file {}", path.display())
})?;
writer.add_file(license_files_dir.join(filename), path)?;
}
}

Ok(())
}
1 change: 1 addition & 0 deletions test-crates/pyo3-pure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ name = "pyo3-pure"
version = "2.1.2"
edition = "2018"
description = "Implements a dummy function (get_fortytwo.DummyClass.get_42()) in rust"
license = "MIT"

[dependencies]
pyo3 = { version = "0.15.1", features = ["abi3-py36", "extension-module"] }
Expand Down

0 comments on commit 2cee279

Please sign in to comment.