Skip to content

Commit

Permalink
Refactor pep517 sdist commands
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Dec 19, 2021
1 parent 34918b6 commit f793063
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 72 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub use crate::pyproject_toml::PyProjectToml;
pub use crate::python_interpreter::PythonInterpreter;
pub use crate::target::Target;
pub use auditwheel::PlatformTag;
pub use source_distribution::source_distribution;
#[cfg(feature = "upload")]
pub use {
crate::registry::Registry,
Expand Down
71 changes: 19 additions & 52 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
//! Run with --help for usage information

use anyhow::{bail, Context, Result};
use cargo_metadata::MetadataCommand;
use fs_err as fs;
use maturin::{
develop, init_project, new_project, source_distribution, write_dist_info, BridgeModel,
BuildOptions, CargoToml, GenerateProjectOptions, Metadata21, PathWriter, PlatformTag,
PyProjectToml, PythonInterpreter, Target,
develop, init_project, new_project, write_dist_info, BridgeModel, BuildOptions,
GenerateProjectOptions, PathWriter, PlatformTag, PythonInterpreter, Target,
};
#[cfg(feature = "upload")]
use maturin::{upload_ui, PublishOpt};
Expand Down Expand Up @@ -262,23 +259,15 @@ fn pep517(subcommand: Pep517Command) -> Result<()> {
sdist_directory,
manifest_path,
} => {
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)
.context("Failed to parse Cargo.toml into python metadata")?;
let cargo_metadata = MetadataCommand::new()
.manifest_path(&manifest_path)
.exec()
.context("Cargo metadata failed. Do you have cargo in your PATH?")?;

let path = source_distribution(
sdist_directory,
&metadata21,
&manifest_path,
&cargo_metadata,
None,
)
.context("Failed to build source distribution")?;
let build_options = BuildOptions {
manifest_path,
out: Some(sdist_directory),
..Default::default()
};
let build_context = build_options.into_build_context(false, false, false)?;
let (path, _) = build_context
.build_source_distribution()?
.context("Failed to build source distribution")?;
println!("{}", path.file_name().unwrap().to_str().unwrap());
}
};
Expand Down Expand Up @@ -376,37 +365,15 @@ fn run() -> Result<()> {
)?;
}
Opt::SDist { manifest_path, out } => {
let manifest_dir = manifest_path.parent().unwrap();

// Ensure the project has a compliant pyproject.toml
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)?;
let metadata21 = Metadata21::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)
.exec()
.context("Cargo metadata failed. Do you have cargo in your PATH?")?;

let wheel_dir = match out {
Some(ref dir) => dir.clone(),
None => PathBuf::from(&cargo_metadata.target_directory).join("wheels"),
let build_options = BuildOptions {
manifest_path,
out,
..Default::default()
};

fs::create_dir_all(&wheel_dir)
.context("Failed to create the target directory for the source distribution")?;

source_distribution(
&wheel_dir,
&metadata21,
&manifest_path,
&cargo_metadata,
pyproject.sdist_include(),
)
.context("Failed to build source distribution")?;
let build_context = build_options.into_build_context(false, false, false)?;
build_context
.build_source_distribution()?
.context("Failed to build source distribution")?;
}
Opt::Pep517(subcommand) => pep517(subcommand)?,
Opt::InitProject { path, options } => init_project(path, options)?,
Expand Down
30 changes: 11 additions & 19 deletions tests/common/other.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use anyhow::{Context, Result};
use cargo_metadata::MetadataCommand;
use flate2::read::GzDecoder;
use maturin::BuildOptions;
use maturin::{source_distribution, CargoToml, Metadata21};
use std::collections::HashSet;
use std::iter::FromIterator;
use std::path::Path;
Expand Down Expand Up @@ -98,7 +96,7 @@ pub fn test_workspace_cargo_lock() -> Result<()> {
"linux",
])?;

let build_context = options.into_build_context(false, cfg!(feature = "faster-tests"), false)?;
let build_context = options.into_build_context(false, false, false)?;
let source_distribution = build_context.build_source_distribution()?;
assert!(source_distribution.is_some());

Expand All @@ -111,23 +109,17 @@ pub fn test_source_distribution(
) -> Result<()> {
let manifest_dir = package.as_ref();
let manifest_path = manifest_dir.join("Cargo.toml");
let cargo_toml = CargoToml::from_path(&manifest_path)?;
let metadata21 = Metadata21::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)
.exec()
.context("Cargo metadata failed. Do you have cargo in your PATH?")?;

let sdist_directory = tempfile::tempdir()?;
let path = source_distribution(
&sdist_directory,
&metadata21,
&manifest_path,
&cargo_metadata,
None,
)
.context("Failed to build source distribution")?;
let build_options = BuildOptions {
manifest_path,
out: Some(sdist_directory.into_path()),
..Default::default()
};

let build_context = build_options.into_build_context(false, false, false)?;
let (path, _) = build_context
.build_source_distribution()?
.context("Failed to build source distribution")?;

let tar_gz = fs_err::File::open(path)?;
let tar = GzDecoder::new(tar_gz);
Expand Down

0 comments on commit f793063

Please sign in to comment.