Skip to content

Commit

Permalink
Merge pull request #749 from messense/sdist-lock
Browse files Browse the repository at this point in the history
Add `Cargo.lock` to sdist when `--locked` or `--frozen` specified
  • Loading branch information
messense authored Dec 19, 2021
2 parents 34918b6 + a03f578 commit a05b713
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 72 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Add support for x86_64 Haiku in [#735](https://github.com/PyO3/maturin/pull/735)
* Fix undefined auditwheel policy panic in [#740](https://github.com/PyO3/maturin/pull/740)
* Fix sdist upload for packages where the pkgname contains multiple underscores in [#741](https://github.com/PyO3/maturin/pull/741)
* Add `Cargo.lock` to sdist when `--locked` or `--frozen` specified in [#749](https://github.com/PyO3/maturin/pull/749)

## [0.12.4] - 2021-12-06

Expand Down
5 changes: 5 additions & 0 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ impl BuildContext {
fs::create_dir_all(&self.out)
.context("Failed to create the target directory for the source distribution")?;

let include_cargo_lock = self
.cargo_extra_args
.iter()
.any(|arg| arg == "--locked" || arg == "--frozen");
match PyProjectToml::new(self.manifest_path.parent().unwrap()) {
Ok(pyproject) => {
let sdist_path = source_distribution(
Expand All @@ -238,6 +242,7 @@ impl BuildContext {
&self.manifest_path,
&self.cargo_metadata,
pyproject.sdist_include(),
include_cargo_lock,
)
.context("Failed to build source distribution")?;
Ok(Some((sdist_path, "source".to_string())))
Expand Down
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
6 changes: 6 additions & 0 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ pub fn source_distribution(
manifest_path: impl AsRef<Path>,
cargo_metadata: &Metadata,
sdist_include: Option<&Vec<String>>,
include_cargo_lock: bool,
) -> Result<PathBuf> {
let known_path_deps = find_path_deps(cargo_metadata)?;

Expand Down Expand Up @@ -271,6 +272,11 @@ pub fn source_distribution(
)?;

let manifest_dir = manifest_path.as_ref().parent().unwrap();
if include_cargo_lock {
let cargo_lock_path = manifest_dir.join("Cargo.lock");
let target = root_dir.join("Cargo.lock");
writer.add_file(&target, &cargo_lock_path)?;
}

if let Some(include_targets) = sdist_include {
for pattern in include_targets {
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 a05b713

Please sign in to comment.