Skip to content

Commit

Permalink
Add wheel files check
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrobbel committed Nov 8, 2022
1 parent 1855d80 commit 587054e
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 26 deletions.
12 changes: 2 additions & 10 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down Expand Up @@ -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")?;
Expand Down Expand Up @@ -706,7 +704,6 @@ impl BuildContext {
&artifact.path,
&self.interpreter[0].executable,
self.editable,
&self.metadata21,
self.pyproject_toml.as_ref(),
)?;

Expand Down Expand Up @@ -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")?;
}
}

Expand Down
17 changes: 5 additions & 12 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)?;
Expand All @@ -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")?;
}

Expand Down Expand Up @@ -891,7 +889,6 @@ if __name__ == '__main__':
pub fn write_python_part(
writer: &mut impl ModuleWriter,
python_module: impl AsRef<Path>,
metadata21: &Metadata21,
pyproject_toml: Option<&PyProjectToml>,
) -> Result<()> {
let python_module = python_module.as_ref();
Expand All @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions test-crates/pyo3-mixed-include-exclude/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
44 changes: 44 additions & 0 deletions tests/common/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -169,6 +171,48 @@ pub fn test_source_distribution(
Ok(())
}

pub fn check_wheel_files(
package: impl AsRef<Path>,
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::<BTreeSet<_>>(),
expected_files.into_iter().collect::<BTreeSet<_>>()
);
Ok(())
}

pub fn abi3_python_interpreter_args() -> Result<()> {
// Case 1: maturin build without `-i`, should work
let options = BuildOptions::try_parse_from(vec![
Expand Down
23 changes: 21 additions & 2 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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(
Expand Down

0 comments on commit 587054e

Please sign in to comment.