From 587054ed3355636ccf2c345b3840b741d78ba349 Mon Sep 17 00:00:00 2001 From: Matthijs Brobbel Date: Tue, 8 Nov 2022 21:05:20 +0100 Subject: [PATCH] Add wheel files check --- src/build_context.rs | 12 +---- src/module_writer.rs | 17 +++---- .../.gitignore | 0 .../exclude_this_file | 0 .../include_this_file | 0 .../pyo3-mixed-include-exclude/pyproject.toml | 13 +++++- tests/common/other.rs | 44 +++++++++++++++++++ tests/run.rs | 23 +++++++++- 8 files changed, 83 insertions(+), 26 deletions(-) rename test-crates/pyo3-mixed-include-exclude/{ => pyo3_mixed_include_exclude}/.gitignore (100%) rename test-crates/pyo3-mixed-include-exclude/{ => pyo3_mixed_include_exclude}/exclude_this_file (100%) rename test-crates/pyo3-mixed-include-exclude/{ => pyo3_mixed_include_exclude}/include_this_file (100%) diff --git a/src/build_context.rs b/src/build_context.rs index a95f56a31..6866dc4d7 100644 --- a/src/build_context.rs +++ b/src/build_context.rs @@ -501,7 +501,6 @@ impl BuildContext { None, &self.target, self.editable, - &self.metadata21, self.pyproject_toml.as_ref(), ) .context("Failed to add the files to the wheel")?; @@ -580,7 +579,6 @@ impl BuildContext { Some(python_interpreter), &self.target, self.editable, - &self.metadata21, self.pyproject_toml.as_ref(), ) .context("Failed to add the files to the wheel")?; @@ -706,7 +704,6 @@ impl BuildContext { &artifact.path, &self.interpreter[0].executable, self.editable, - &self.metadata21, self.pyproject_toml.as_ref(), )?; @@ -814,13 +811,8 @@ impl BuildContext { bail!("Sorry, adding python code to a wasm binary is currently not supported") } if !self.editable { - write_python_part( - &mut writer, - python_module, - &metadata21, - self.pyproject_toml.as_ref(), - ) - .context("Failed to add the python module to the package")?; + write_python_part(&mut writer, python_module, self.pyproject_toml.as_ref()) + .context("Failed to add the python module to the package")?; } } diff --git a/src/module_writer.rs b/src/module_writer.rs index 983d1c25b..1079dc59c 100644 --- a/src/module_writer.rs +++ b/src/module_writer.rs @@ -673,7 +673,6 @@ pub fn write_bindings_module( python_interpreter: Option<&PythonInterpreter>, target: &Target, editable: bool, - metadata: &Metadata21, pyproject_toml: Option<&PyProjectToml>, ) -> Result<()> { let ext_name = &project_layout.extension_name; @@ -705,7 +704,7 @@ pub fn write_bindings_module( target.display() ))?; } else { - write_python_part(writer, python_module, metadata, pyproject_toml) + write_python_part(writer, python_module, pyproject_toml) .context("Failed to add the python module to the package")?; let relative = project_layout @@ -755,7 +754,6 @@ pub fn write_cffi_module( artifact: &Path, python: &Path, editable: bool, - metadata21: &Metadata21, pyproject_toml: Option<&PyProjectToml>, ) -> Result<()> { let cffi_declarations = generate_cffi_declarations(crate_dir, target_dir, python)?; @@ -764,7 +762,7 @@ pub fn write_cffi_module( if let Some(python_module) = &project_layout.python_module { if !editable { - write_python_part(writer, python_module, metadata21, pyproject_toml) + write_python_part(writer, python_module, pyproject_toml) .context("Failed to add the python module to the package")?; } @@ -891,7 +889,6 @@ if __name__ == '__main__': pub fn write_python_part( writer: &mut impl ModuleWriter, python_module: impl AsRef, - metadata21: &Metadata21, pyproject_toml: Option<&PyProjectToml>, ) -> Result<()> { let python_module = python_module.as_ref(); @@ -918,22 +915,18 @@ pub fn write_python_part( // Include additional files if let Some(pyproject) = pyproject_toml { - let root_dir = PathBuf::from(format!( - "{}-{}", - &metadata21.get_distribution_escaped(), - &metadata21.get_version_escaped() - )); + let pyproject_dir = python_module.parent().unwrap(); if let Some(glob_patterns) = pyproject.include() { for pattern in glob_patterns .iter() .filter_map(|glob_pattern| glob_pattern.targets(Format::Sdist)) { println!("📦 Including files matching \"{}\"", pattern); - for source in glob::glob(&python_module.join(pattern).to_string_lossy()) + for source in glob::glob(&pyproject_dir.join(pattern).to_string_lossy()) .expect("No files found for pattern") .filter_map(Result::ok) { - let target = root_dir.join(source.strip_prefix(python_module).unwrap()); + let target = source.strip_prefix(pyproject_dir)?.to_path_buf(); if source.is_dir() { writer.add_directory(target)?; } else { diff --git a/test-crates/pyo3-mixed-include-exclude/.gitignore b/test-crates/pyo3-mixed-include-exclude/pyo3_mixed_include_exclude/.gitignore similarity index 100% rename from test-crates/pyo3-mixed-include-exclude/.gitignore rename to test-crates/pyo3-mixed-include-exclude/pyo3_mixed_include_exclude/.gitignore diff --git a/test-crates/pyo3-mixed-include-exclude/exclude_this_file b/test-crates/pyo3-mixed-include-exclude/pyo3_mixed_include_exclude/exclude_this_file similarity index 100% rename from test-crates/pyo3-mixed-include-exclude/exclude_this_file rename to test-crates/pyo3-mixed-include-exclude/pyo3_mixed_include_exclude/exclude_this_file diff --git a/test-crates/pyo3-mixed-include-exclude/include_this_file b/test-crates/pyo3-mixed-include-exclude/pyo3_mixed_include_exclude/include_this_file similarity index 100% rename from test-crates/pyo3-mixed-include-exclude/include_this_file rename to test-crates/pyo3-mixed-include-exclude/pyo3_mixed_include_exclude/include_this_file diff --git a/test-crates/pyo3-mixed-include-exclude/pyproject.toml b/test-crates/pyo3-mixed-include-exclude/pyproject.toml index df4db10d8..9c8bc0307 100644 --- a/test-crates/pyo3-mixed-include-exclude/pyproject.toml +++ b/test-crates/pyo3-mixed-include-exclude/pyproject.toml @@ -12,5 +12,14 @@ dependencies = ["boltons"] get_42 = "pyo3_mixed_include_exclude:get_42" [tool.maturin] -include = ["include_this_file", "missing"] -exclude = ["exclude_this_file", "tests/**/*", ".gitignore", "unused"] +include = [ + "pyo3_mixed_include_exclude/include_this_file", + "missing", + "README.md", +] +exclude = [ + "pyo3_mixed_include_exclude/exclude_this_file", + "pyo3_mixed_include_exclude/.gitignore", + "tests/**/*", + "unused", +] diff --git a/tests/common/other.rs b/tests/common/other.rs index cbdb7dc01..322316b63 100644 --- a/tests/common/other.rs +++ b/tests/common/other.rs @@ -4,10 +4,12 @@ use flate2::read::GzDecoder; use maturin::{BuildOptions, CargoOptions}; use pretty_assertions::assert_eq; use std::collections::BTreeSet; +use std::fs::File; use std::io::Read; use std::iter::FromIterator; use std::path::{Path, PathBuf}; use tar::Archive; +use zip::ZipArchive; /// Tries to compile a sample crate (pyo3-pure) for musl, /// given that rustup and the the musl target are installed @@ -169,6 +171,48 @@ pub fn test_source_distribution( Ok(()) } +pub fn check_wheel_files( + package: impl AsRef, + expected_files: Vec<&str>, + unique_name: &str, +) -> Result<()> { + let manifest_path = package.as_ref().join("Cargo.toml"); + let wheel_directory = Path::new("test-crates").join("wheels").join(unique_name); + + let build_options = BuildOptions { + out: Some(wheel_directory), + cargo: CargoOptions { + manifest_path: Some(manifest_path), + quiet: true, + target_dir: Some(PathBuf::from( + "test-crates/targets/test_workspace_cargo_lock", + )), + ..Default::default() + }, + ..Default::default() + }; + + let build_context = build_options.into_build_context(false, false, false)?; + let wheels = build_context + .build_wheels() + .context("Failed to build wheels")?; + assert!(!wheels.is_empty()); + let (wheel_path, _) = &wheels[0]; + + let wheel = ZipArchive::new(File::open(wheel_path)?)?; + let drop_platform_specific_files = |file: &&str| -> bool { + !matches!(Path::new(file).extension(), Some(ext) if ext == "pyc" || ext == "pyd" || ext == "so") + }; + assert_eq!( + wheel + .file_names() + .filter(drop_platform_specific_files) + .collect::>(), + expected_files.into_iter().collect::>() + ); + Ok(()) +} + pub fn abi3_python_interpreter_args() -> Result<()> { // Case 1: maturin build without `-i`, should work let options = BuildOptions::try_parse_from(vec![ diff --git a/tests/run.rs b/tests/run.rs index 04964b2a7..98e6598d9 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -504,9 +504,9 @@ fn pyo3_mixed_include_exclude_sdist() { "pyo3_mixed_include_exclude-2.1.3/PKG-INFO", "pyo3_mixed_include_exclude-2.1.3/README.md", "pyo3_mixed_include_exclude-2.1.3/check_installed/check_installed.py", - "pyo3_mixed_include_exclude-2.1.3/include_this_file", // included - // "pyo3_mixed_include_exclude-2.1.3/exclude_this_file, excluded + // "pyo3_mixed_include_exclude-2.1.3/pyo3_mixed_include_exclude/exclude_this_file, excluded "pyo3_mixed_include_exclude-2.1.3/pyo3_mixed_include_exclude/__init__.py", + "pyo3_mixed_include_exclude-2.1.3/pyo3_mixed_include_exclude/include_this_file", // included "pyo3_mixed_include_exclude-2.1.3/pyo3_mixed_include_exclude/python_module/__init__.py", "pyo3_mixed_include_exclude-2.1.3/pyo3_mixed_include_exclude/python_module/double.py", "pyo3_mixed_include_exclude-2.1.3/pyproject.toml", @@ -519,6 +519,25 @@ fn pyo3_mixed_include_exclude_sdist() { )) } +#[test] +fn pyo3_mixed_include_exclude_wheel_files() { + handle_result(other::check_wheel_files( + "test-crates/pyo3-mixed-include-exclude", + vec![ + "pyo3_mixed_include_exclude-2.1.3.dist-info/METADATA", + "pyo3_mixed_include_exclude-2.1.3.dist-info/RECORD", + "pyo3_mixed_include_exclude-2.1.3.dist-info/WHEEL", + "pyo3_mixed_include_exclude-2.1.3.dist-info/entry_points.txt", + "pyo3_mixed_include_exclude/__init__.py", + "pyo3_mixed_include_exclude/include_this_file", + "pyo3_mixed_include_exclude/python_module/__init__.py", + "pyo3_mixed_include_exclude/python_module/double.py", + "README.md", + ], + "wheel-files-pyo3-mixed-include-exclude", + )) +} + #[test] fn workspace_with_path_dep_sdist() { handle_result(other::test_source_distribution(