Skip to content

Commit

Permalink
Add new pyproject_toml module
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Apr 17, 2021
1 parent 12ac30f commit d91ca53
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 61 deletions.
5 changes: 3 additions & 2 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")]
Expand Down
9 changes: 4 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?;
Expand Down
56 changes: 56 additions & 0 deletions src/pyproject_toml.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
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<ToolMaturin>,
}

/// The `[tool.maturin]` section of a pyproject.toml
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct ToolMaturin {
sdist_include: Option<Vec<String>>,
}

/// 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<Tool>,
}

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<Path>) -> Result<PyProjectToml> {
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<String>> {
self.tool.as_ref()?.maturin.as_ref()?.sdist_include.as_ref()
}
}
54 changes: 1 addition & 53 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String>,
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<ToolMaturin>,
}

/// The `[tool.maturin]` section of a pyproject.toml
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct ToolMaturin {
sdist_include: Option<Vec<String>>,
}

/// 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<Tool>,
}

impl PyProjectToml {
pub fn sdist_include(&self) -> Option<&Vec<String>> {
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<Path>) -> Result<PyProjectToml> {
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)
}

0 comments on commit d91ca53

Please sign in to comment.