diff --git a/src/build_context.rs b/src/build_context.rs index 11bad5b40..09b83a7f0 100644 --- a/src/build_context.rs +++ b/src/build_context.rs @@ -6,8 +6,9 @@ use crate::compile::warn_missing_py_init; 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::{get_pyproject_toml, source_distribution}; +use crate::source_distribution::source_distribution; use crate::Metadata21; +use crate::PyProjectToml; use crate::PythonInterpreter; use crate::Target; use anyhow::{anyhow, bail, Context, Result}; @@ -153,7 +154,7 @@ impl BuildContext { fs::create_dir_all(&self.out) .context("Failed to create the target directory for the source distribution")?; - match get_pyproject_toml(self.manifest_path.parent().unwrap()) { + match PyProjectToml::new(self.manifest_path.parent().unwrap()) { Ok(pyproject) => { let sdist_path = source_distribution( &self.out, diff --git a/src/lib.rs b/src/lib.rs index 0ba6226ac..e83b2c016 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,13 +36,14 @@ pub use crate::metadata::{Metadata21, WheelMetadata}; pub use crate::module_writer::{ write_dist_info, ModuleWriter, PathWriter, SDistWriter, WheelWriter, }; +pub use crate::pyproject_toml::PyProjectToml; pub use crate::python_interpreter::PythonInterpreter; pub use crate::read_distribution::{ get_metadata_for_distribution, get_supported_version_for_distribution, }; pub use crate::target::Target; pub use auditwheel::Manylinux; -pub use source_distribution::{get_pyproject_toml, source_distribution}; +pub use source_distribution::source_distribution; #[cfg(feature = "upload")] pub use { crate::registry::Registry, @@ -57,6 +58,7 @@ mod compile; mod develop; mod metadata; mod module_writer; +mod pyproject_toml; mod python_interpreter; mod read_distribution; #[cfg(feature = "upload")] diff --git a/src/main.rs b/src/main.rs index 1418ddc70..a94e64bd7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,10 +15,9 @@ use human_panic::setup_panic; #[cfg(feature = "password-storage")] use keyring::{Keyring, KeyringError}; use maturin::{ - develop, get_metadata_for_distribution, get_pyproject_toml, - get_supported_version_for_distribution, source_distribution, write_dist_info, BridgeModel, - BuildOptions, BuiltWheelMetadata, CargoToml, Manylinux, Metadata21, PathWriter, - PythonInterpreter, Target, + develop, get_metadata_for_distribution, get_supported_version_for_distribution, + source_distribution, write_dist_info, BridgeModel, BuildOptions, BuiltWheelMetadata, CargoToml, + Manylinux, Metadata21, PathWriter, PyProjectToml, PythonInterpreter, Target, }; use std::env; use std::path::PathBuf; @@ -574,7 +573,7 @@ fn run() -> Result<()> { let manifest_dir = manifest_path.parent().unwrap(); // Ensure the project has a compliant pyproject.toml - let pyproject = get_pyproject_toml(&manifest_dir) + let pyproject = PyProjectToml::new(&manifest_dir) .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)?; diff --git a/src/pyproject_toml.rs b/src/pyproject_toml.rs new file mode 100644 index 000000000..4a38169a0 --- /dev/null +++ b/src/pyproject_toml.rs @@ -0,0 +1,56 @@ +use anyhow::{format_err, Context, Result}; +use serde::{Deserialize, Serialize}; +use std::fs; +use std::path::Path; + +/// The `[build-system]` section of a pyproject.toml as specified in PEP 517 +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "kebab-case")] +pub struct BuildSystem { + requires: Vec, + build_backend: String, +} + +/// The `[tool]` section of a pyproject.toml +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "kebab-case")] +pub struct Tool { + maturin: Option, +} + +/// The `[tool.maturin]` section of a pyproject.toml +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "kebab-case")] +pub struct ToolMaturin { + sdist_include: Option>, +} + +/// A pyproject.toml as specified in PEP 517 +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "kebab-case")] +pub struct PyProjectToml { + build_system: BuildSystem, + tool: Option, +} + +impl PyProjectToml { + /// Returns the contents of a pyproject.toml with a `[build-system]` entry or an error + /// + /// Does no specific error handling because it's only used to check whether or not to build + /// source distributions + pub fn new(project_root: impl AsRef) -> Result { + let path = project_root.as_ref().join("pyproject.toml"); + let contents = fs::read_to_string(&path).context(format!( + "Couldn't find pyproject.toml at {}", + path.display() + ))?; + let cargo_toml = toml::from_str(&contents) + .map_err(|err| format_err!("pyproject.toml is not PEP 517 compliant: {}", err))?; + Ok(cargo_toml) + } + + /// Returns the value of `[maturin.sdist-include]` in pyproject.toml + pub fn sdist_include(&self) -> Option<&Vec> { + self.tool.as_ref()?.maturin.as_ref()?.sdist_include.as_ref() + } +} diff --git a/src/source_distribution.rs b/src/source_distribution.rs index 11ec4fc27..b2b3d8e5d 100644 --- a/src/source_distribution.rs +++ b/src/source_distribution.rs @@ -1,10 +1,9 @@ use crate::module_writer::ModuleWriter; use crate::{Metadata21, SDistWriter}; -use anyhow::{bail, format_err, Context, Result}; +use anyhow::{bail, Context, Result}; use cargo_metadata::Metadata; use fs_err as fs; use regex::Regex; -use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::process::Command; @@ -246,54 +245,3 @@ pub fn source_distribution( Ok(source_distribution_path) } - -/// The `[build-system]` section of a pyproject.toml as specified in PEP 517 -#[derive(Serialize, Deserialize, Debug, Clone)] -#[serde(rename_all = "kebab-case")] -pub struct BuildSystem { - requires: Vec, - build_backend: String, -} - -/// The `[tool]` section of a pyproject.toml -#[derive(Serialize, Deserialize, Debug, Clone)] -#[serde(rename_all = "kebab-case")] -pub struct Tool { - maturin: Option, -} - -/// The `[tool.maturin]` section of a pyproject.toml -#[derive(Serialize, Deserialize, Debug, Clone)] -#[serde(rename_all = "kebab-case")] -pub struct ToolMaturin { - sdist_include: Option>, -} - -/// A pyproject.toml as specified in PEP 517 -#[derive(Serialize, Deserialize, Debug, Clone)] -#[serde(rename_all = "kebab-case")] -pub struct PyProjectToml { - build_system: BuildSystem, - tool: Option, -} - -impl PyProjectToml { - pub fn sdist_include(&self) -> Option<&Vec> { - self.tool.as_ref()?.maturin.as_ref()?.sdist_include.as_ref() - } -} - -/// Returns the contents of a pyproject.toml with a `[build-system]` entry or an error -/// -/// Does no specific error handling because it's only used to check whether or not to build -/// source distributions -pub fn get_pyproject_toml(project_root: impl AsRef) -> Result { - let path = project_root.as_ref().join("pyproject.toml"); - let contents = fs::read_to_string(&path).context(format!( - "Couldn't find pyproject.toml at {}", - path.display() - ))?; - let cargo_toml = toml::from_str(&contents) - .map_err(|err| format_err!("pyproject.toml is not PEP 517 compliant: {}", err))?; - Ok(cargo_toml) -}