Skip to content

Commit

Permalink
Refactor source distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Sep 12, 2022
1 parent d7d9d52 commit 01e8160
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
13 changes: 2 additions & 11 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,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_options.locked || self.cargo_options.frozen;
match self.pyproject_toml.as_ref() {
Some(pyproject) => {
let sdist_path = source_distribution(
&self.out,
&self.metadata21,
&self.manifest_path,
&self.cargo_metadata,
pyproject.sdist_include(),
include_cargo_lock,
self.project_layout.data.as_deref(),
)
.context("Failed to build source distribution")?;
let sdist_path = source_distribution(self, pyproject)
.context("Failed to build source distribution")?;
Ok(Some((sdist_path, "source".to_string())))
}
None => Ok(None),
Expand Down
26 changes: 13 additions & 13 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::module_writer::{add_data, ModuleWriter};
use crate::{Metadata21, SDistWriter};
use crate::{BuildContext, PyProjectToml, SDistWriter};
use anyhow::{bail, Context, Result};
use cargo_metadata::Metadata;
use fs_err as fs;
Expand Down Expand Up @@ -238,17 +238,15 @@ fn find_path_deps(cargo_metadata: &Metadata) -> Result<HashMap<String, PathBuf>>
/// and in
/// https://packaging.python.org/specifications/source-distribution-format/#source-distribution-file-format
pub fn source_distribution(
wheel_dir: impl AsRef<Path>,
metadata21: &Metadata21,
manifest_path: impl AsRef<Path>,
cargo_metadata: &Metadata,
sdist_include: Option<&Vec<String>>,
include_cargo_lock: bool,
data: Option<&Path>,
build_context: &BuildContext,
pyproject: &PyProjectToml,
) -> Result<PathBuf> {
let known_path_deps = find_path_deps(cargo_metadata)?;
let metadata21 = &build_context.metadata21;
let manifest_path = &build_context.manifest_path;

let known_path_deps = find_path_deps(&build_context.cargo_metadata)?;

let mut writer = SDistWriter::new(wheel_dir, metadata21)?;
let mut writer = SDistWriter::new(&build_context.out, metadata21)?;
let root_dir = PathBuf::from(format!(
"{}-{}",
&metadata21.get_distribution_escaped(),
Expand Down Expand Up @@ -280,14 +278,16 @@ pub fn source_distribution(
true,
)?;

let manifest_dir = manifest_path.as_ref().parent().unwrap();
let manifest_dir = manifest_path.parent().unwrap();
let include_cargo_lock =
build_context.cargo_options.locked || build_context.cargo_options.frozen;
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 {
if let Some(include_targets) = pyproject.sdist_include() {
for pattern in include_targets {
println!("📦 Including files matching \"{}\"", pattern);
for source in glob::glob(&manifest_dir.join(pattern).to_string_lossy())
Expand All @@ -305,7 +305,7 @@ pub fn source_distribution(
metadata21.to_file_contents()?.as_bytes(),
)?;

add_data(&mut writer, data)?;
add_data(&mut writer, build_context.project_layout.data.as_deref())?;
let source_distribution_path = writer.finish()?;

println!(
Expand Down

0 comments on commit 01e8160

Please sign in to comment.