Skip to content

Commit

Permalink
Metadata-Version 2.2
Browse files Browse the repository at this point in the history
Since PEP 643 is accepted we can upgrade to Metadata-Version 2.2
  • Loading branch information
konstin committed Jun 7, 2021
1 parent 7c3ea09 commit 9f0bd63
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 34 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Add PEP 656 musllinux support in [#543](https://github.com/PyO3/maturin/pull/543)
* `--manylinux` is now called `--compatibility` and supports musllinux
* Metadata-Version is now 2.2

## 0.10.6 - 2021-05-21

Expand Down
2 changes: 0 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,5 +426,3 @@ The `sysconfig` folder contains the output of `python -m sysconfig` for differen
You need to install `cffi` and `virtualenv` (`pip install cffi virtualenv`) to run the tests.

There are two optional hacks that can speed up the tests (over 80s to 17s on my machine). By running `cargo build --release --manifest-path test-crates/cargo-mock/Cargo.toml` you can activate a cargo cache avoiding to rebuild the pyo3 test crates with every python version. Delete `target/test-cache` to clear the cache (e.g. after changing a test crate) or remove `test-crates/cargo-mock/target/release/cargo` to deactivate it. By running the tests with the `faster-tests` feature, binaries are stripped and wheels are only stored and not compressed.

You might want to have look into my by now slightly outdated [blog post](https://blog.schuetze.link/2018/07/21/a-dive-into-packaging-native-python-extensions.html) which explains the intricacies of building native python packages.
4 changes: 2 additions & 2 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::module_writer::write_python_part;
use crate::module_writer::WheelWriter;
use crate::module_writer::{write_bin, write_bindings_module, write_cffi_module};
use crate::source_distribution::source_distribution;
use crate::Metadata21;
use crate::Metadata22;
use crate::PyProjectToml;
use crate::PythonInterpreter;
use crate::Target;
Expand Down Expand Up @@ -126,7 +126,7 @@ pub struct BuildContext {
/// Whether this project is pure rust or rust mixed with python
pub project_layout: ProjectLayout,
/// Python Package Metadata 2.1
pub metadata21: Metadata21,
pub metadata21: Metadata22,
/// The `[console_scripts]` for the entry_points.txt
pub scripts: HashMap<String, String>,
/// The name of the crate
Expand Down
8 changes: 4 additions & 4 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::cross_compile::{find_sysconfigdata, is_cross_compiling, parse_sysconf
use crate::python_interpreter::InterpreterKind;
use crate::BuildContext;
use crate::CargoToml;
use crate::Metadata21;
use crate::Metadata22;
use crate::PythonInterpreter;
use crate::Target;
use anyhow::{bail, format_err, Context, Result};
Expand Down Expand Up @@ -130,7 +130,7 @@ impl BuildOptions {

let cargo_toml = CargoToml::from_path(&manifest_file)?;
let manifest_dir = manifest_file.parent().unwrap();
let metadata21 = Metadata21::from_cargo_toml(&cargo_toml, &manifest_dir)
let metadata21 = Metadata22::from_cargo_toml(&cargo_toml, &manifest_dir)
.context("Failed to parse Cargo.toml into python metadata")?;
let extra_metadata = cargo_toml.remaining_core_metadata();
let scripts = cargo_toml.scripts();
Expand Down Expand Up @@ -251,7 +251,7 @@ impl BuildOptions {

/// Uses very simple PEP 440 subset parsing to determine the
/// minimum supported python minor version for interpreter search
fn get_min_python_minor(metadata21: &Metadata21) -> Option<usize> {
fn get_min_python_minor(metadata21: &Metadata22) -> Option<usize> {
if let Some(requires_python) = &metadata21.requires_python {
let regex = Regex::new(r#">=3\.(\d+)(?:\.\d)?"#).unwrap();
if let Some(captures) = regex.captures(&requires_python) {
Expand Down Expand Up @@ -763,7 +763,7 @@ mod test {
// Nothing specified
let cargo_toml = CargoToml::from_path("test-crates/pyo3-pure/Cargo.toml").unwrap();
let metadata21 =
Metadata21::from_cargo_toml(&cargo_toml, &"test-crates/pyo3-pure").unwrap();
Metadata22::from_cargo_toml(&cargo_toml, &"test-crates/pyo3-pure").unwrap();
assert_eq!(get_min_python_minor(&metadata21), None);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub use crate::build_options::BuildOptions;
pub use crate::cargo_toml::CargoToml;
pub use crate::compile::compile;
pub use crate::develop::develop;
pub use crate::metadata::{Metadata21, WheelMetadata};
pub use crate::metadata::{Metadata22, WheelMetadata};
pub use crate::module_writer::{
write_dist_info, ModuleWriter, PathWriter, SDistWriter, WheelWriter,
};
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use human_panic::setup_panic;
use keyring::{Keyring, KeyringError};
use maturin::{
develop, source_distribution, write_dist_info, BridgeModel, BuildOptions, CargoToml,
Metadata21, PathWriter, PlatformTag, PyProjectToml, PythonInterpreter, Target,
Metadata22, PathWriter, PlatformTag, PyProjectToml, PythonInterpreter, Target,
};
use std::env;
use std::path::PathBuf;
Expand Down Expand Up @@ -428,7 +428,7 @@ fn pep517(subcommand: Pep517Command) -> Result<()> {
} => {
let cargo_toml = CargoToml::from_path(&manifest_path)?;
let manifest_dir = manifest_path.parent().unwrap();
let metadata21 = Metadata21::from_cargo_toml(&cargo_toml, &manifest_dir)
let metadata21 = Metadata22::from_cargo_toml(&cargo_toml, &manifest_dir)
.context("Failed to parse Cargo.toml into python metadata")?;
let cargo_metadata = MetadataCommand::new()
.manifest_path(&manifest_path)
Expand Down Expand Up @@ -625,7 +625,7 @@ fn run() -> Result<()> {
.context("A pyproject.toml with a PEP 517 compliant `[build-system]` table is required to build a source distribution")?;

let cargo_toml = CargoToml::from_path(&manifest_path)?;
let metadata21 = Metadata21::from_cargo_toml(&cargo_toml, &manifest_dir)
let metadata21 = Metadata22::from_cargo_toml(&cargo_toml, &manifest_dir)
.context("Failed to parse Cargo.toml into python metadata")?;

let cargo_metadata = MetadataCommand::new()
Expand Down
26 changes: 13 additions & 13 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::str;
/// The metadata required to generate the .dist-info directory
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct WheelMetadata {
/// Python Package Metadata 2.1
pub metadata21: Metadata21,
/// Python Package Metadata 2.2
pub metadata22: Metadata22,
/// The `[console_scripts]` for the entry_points.txt
pub scripts: HashMap<String, String>,
/// The name of the module can be distinct from the package name, mostly
Expand All @@ -20,12 +20,12 @@ pub struct WheelMetadata {
pub module_name: String,
}

/// Python Package Metadata 2.1 as specified in
/// Python Package Metadata 2.2 as specified in
/// https://packaging.python.org/specifications/core-metadata/
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(rename_all = "kebab-case")]
#[allow(missing_docs)]
pub struct Metadata21 {
pub struct Metadata22 {
// Mandatory fields
pub metadata_version: String,
pub name: String,
Expand Down Expand Up @@ -75,14 +75,14 @@ fn path_to_content_type(path: &Path) -> String {
})
}

impl Metadata21 {
impl Metadata22 {
/// Uses a Cargo.toml to create the metadata for python packages
///
/// manifest_path must be the directory, not the file
pub fn from_cargo_toml(
cargo_toml: &CargoToml,
manifest_path: impl AsRef<Path>,
) -> Result<Metadata21> {
) -> Result<Metadata22> {
let authors = cargo_toml.package.authors.join(", ");

let classifiers = cargo_toml.classifiers();
Expand Down Expand Up @@ -123,8 +123,8 @@ impl Metadata21 {
})
.unwrap_or_else(|| cargo_toml.package.name.clone());

Ok(Metadata21 {
metadata_version: "2.1".to_owned(),
Ok(Metadata22 {
metadata_version: "2.2".to_owned(),

// Mapped from cargo metadata
name,
Expand Down Expand Up @@ -298,7 +298,7 @@ mod test {
let cargo_toml_struct: CargoToml = toml::from_str(&toml_with_path).unwrap();

let metadata =
Metadata21::from_cargo_toml(&cargo_toml_struct, &readme_md.path().parent().unwrap())
Metadata22::from_cargo_toml(&cargo_toml_struct, &readme_md.path().parent().unwrap())
.unwrap();

let actual = metadata.to_file_contents();
Expand Down Expand Up @@ -361,7 +361,7 @@ mod test {

let expected = indoc!(
r#"
Metadata-Version: 2.1
Metadata-Version: 2.2
Name: info-project
Version: 0.1.0
Classifiers: Programming Language :: Python
Expand Down Expand Up @@ -420,7 +420,7 @@ mod test {

let expected = indoc!(
r#"
Metadata-Version: 2.1
Metadata-Version: 2.2
Name: info-project
Version: 0.1.0
Classifiers: Programming Language :: Python
Expand Down Expand Up @@ -468,7 +468,7 @@ mod test {

let expected = indoc!(
r#"
Metadata-Version: 2.1
Metadata-Version: 2.2
Name: info
Version: 0.1.0
Classifiers: Programming Language :: Python
Expand All @@ -480,7 +480,7 @@ mod test {
);

let cargo_toml_struct: CargoToml = toml::from_str(&cargo_toml).unwrap();
let metadata = Metadata21::from_cargo_toml(&cargo_toml_struct, "").unwrap();
let metadata = Metadata22::from_cargo_toml(&cargo_toml_struct, "").unwrap();
let actual = metadata.to_file_contents();

assert_eq!(
Expand Down
12 changes: 6 additions & 6 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::build_context::ProjectLayout;
use crate::PythonInterpreter;
use crate::Target;
use crate::{BridgeModel, Metadata21};
use crate::{BridgeModel, Metadata22};
use anyhow::{anyhow, bail, Context, Result};
use flate2::write::GzEncoder;
use flate2::Compression;
Expand Down Expand Up @@ -122,7 +122,7 @@ impl PathWriter {
}

/// Writes the RECORD file after everything else has been written
pub fn write_record(self, metadata21: &Metadata21) -> Result<()> {
pub fn write_record(self, metadata21: &Metadata22) -> Result<()> {
let record_file = self
.base_path
.join(metadata21.get_dist_info_dir())
Expand Down Expand Up @@ -247,7 +247,7 @@ impl WheelWriter {
pub fn new(
tag: &str,
wheel_dir: &Path,
metadata21: &Metadata21,
metadata21: &Metadata22,
scripts: &HashMap<String, String>,
tags: &[String],
) -> Result<WheelWriter> {
Expand Down Expand Up @@ -340,7 +340,7 @@ impl ModuleWriter for SDistWriter {

impl SDistWriter {
/// Create a source distribution .tar.gz which can be subsequently expanded
pub fn new(wheel_dir: impl AsRef<Path>, metadata21: &Metadata21) -> Result<Self, io::Error> {
pub fn new(wheel_dir: impl AsRef<Path>, metadata21: &Metadata22) -> Result<Self, io::Error> {
let path = wheel_dir.as_ref().join(format!(
"{}-{}.tar.gz",
&metadata21.get_distribution_escaped(),
Expand Down Expand Up @@ -670,7 +670,7 @@ pub fn write_cffi_module(
pub fn write_bin(
writer: &mut impl ModuleWriter,
artifact: &Path,
metadata: &Metadata21,
metadata: &Metadata22,
bin_name: &OsStr,
) -> Result<()> {
let data_dir = PathBuf::from(format!(
Expand Down Expand Up @@ -725,7 +725,7 @@ pub fn write_python_part(
/// Creates the .dist-info directory and fills it with all metadata files except RECORD
pub fn write_dist_info(
writer: &mut impl ModuleWriter,
metadata21: &Metadata21,
metadata21: &Metadata22,
scripts: &HashMap<String, String, impl std::hash::BuildHasher>,
tags: &[String],
) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/read_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ mod test {
let metadata =
get_metadata_for_distribution(Path::new("test-data/pyo3_mixed-2.1.1.tar.gz")).unwrap();
let expected: Vec<_> = [
("Metadata-Version", "2.1"),
("Metadata-Version", "2.2"),
("Name", "pyo3-mixed"),
("Version", "2.1.1"),
("Summary", "Implements a dummy function combining rust and python"),
Expand Down
4 changes: 2 additions & 2 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::module_writer::ModuleWriter;
use crate::{Metadata21, SDistWriter};
use crate::{Metadata22, SDistWriter};
use anyhow::{bail, Context, Result};
use cargo_metadata::Metadata;
use fs_err as fs;
Expand Down Expand Up @@ -164,7 +164,7 @@ fn add_crate_to_source_distribution(
/// https://packaging.python.org/specifications/source-distribution-format/#source-distribution-file-format
pub fn source_distribution(
wheel_dir: impl AsRef<Path>,
metadata21: &Metadata21,
metadata21: &Metadata22,
manifest_path: impl AsRef<Path>,
cargo_metadata: &Metadata,
sdist_include: Option<&Vec<String>>,
Expand Down
Binary file modified test-data/pyo3_mixed-2.1.1.tar.gz
Binary file not shown.

0 comments on commit 9f0bd63

Please sign in to comment.