Skip to content

Commit

Permalink
Add only .pth file to editable wheels
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Oct 14, 2021
1 parent b27d47d commit 7cd18f0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ impl BuildContext {
None,
&self.target,
false,
self.editable,
)
.context("Failed to add the files to the wheel")?;

Expand Down Expand Up @@ -330,6 +331,7 @@ impl BuildContext {
Some(python_interpreter),
&self.target,
false,
self.editable,
)
.context("Failed to add the files to the wheel")?;

Expand Down Expand Up @@ -421,6 +423,7 @@ impl BuildContext {
artifact,
&self.interpreter[0].executable,
false,
self.editable,
)?;

self.add_pth(&mut writer)?;
Expand Down Expand Up @@ -463,8 +466,10 @@ impl BuildContext {
ref extension_name,
..
} => {
write_python_part(&mut writer, python_module, extension_name)
.context("Failed to add the python module to the package")?;
if !self.editable {
write_python_part(&mut writer, python_module, extension_name)
.context("Failed to add the python module to the package")?;
}
}
ProjectLayout::PureRust { .. } => {}
}
Expand Down
3 changes: 3 additions & 0 deletions src/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub fn develop(
&artifact,
&interpreter.executable,
true,
false,
)?;
}
BridgeModel::Bindings(_) => {
Expand All @@ -164,6 +165,7 @@ pub fn develop(
Some(&interpreter),
&target,
true,
false,
)?;
}
BridgeModel::BindingsAbi3(_, _) => {
Expand All @@ -183,6 +185,7 @@ pub fn develop(
None,
&target,
true,
false,
)?;
}
}
Expand Down
38 changes: 26 additions & 12 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ fn handle_cffi_call_result(
}

/// Copies the shared library into the module, which is the only extra file needed with bindings
#[allow(clippy::too_many_arguments)]
pub fn write_bindings_module(
writer: &mut impl ModuleWriter,
project_layout: &ProjectLayout,
Expand All @@ -600,6 +601,7 @@ pub fn write_bindings_module(
python_interpreter: Option<&PythonInterpreter>,
target: &Target,
develop: bool,
editable: bool,
) -> Result<()> {
let ext_name = project_layout.extension_name();
let so_filename = match python_interpreter {
Expand All @@ -621,10 +623,12 @@ pub fn write_bindings_module(
ref rust_module,
..
} => {
write_python_part(writer, python_module, &module_name)
.context("Failed to add the python module to the package")?;
if !editable {
write_python_part(writer, python_module, &module_name)
.context("Failed to add the python module to the package")?;
}

if develop {
if develop || editable {
let target = rust_module.join(&so_filename);
fs::copy(&artifact, &target).context(format!(
"Failed to copy {} to {}",
Expand All @@ -633,8 +637,10 @@ pub fn write_bindings_module(
))?;
}

let relative = rust_module.strip_prefix(python_module.parent().unwrap())?;
writer.add_file_with_permissions(relative.join(&so_filename), &artifact, 0o755)?;
if !editable {
let relative = rust_module.strip_prefix(python_module.parent().unwrap())?;
writer.add_file_with_permissions(relative.join(&so_filename), &artifact, 0o755)?;
}
}
ProjectLayout::PureRust {
ref rust_module, ..
Expand Down Expand Up @@ -667,6 +673,7 @@ pub fn write_bindings_module(
}

/// Creates the cffi module with the shared library, the cffi declarations and the cffi loader
#[allow(clippy::too_many_arguments)]
pub fn write_cffi_module(
writer: &mut impl ModuleWriter,
project_layout: &ProjectLayout,
Expand All @@ -675,6 +682,7 @@ pub fn write_cffi_module(
artifact: &Path,
python: &Path,
develop: bool,
editable: bool,
) -> Result<()> {
let cffi_declarations = generate_cffi_declarations(crate_dir, python)?;

Expand All @@ -686,10 +694,12 @@ pub fn write_cffi_module(
ref rust_module,
ref extension_name,
} => {
write_python_part(writer, python_module, &module_name)
.context("Failed to add the python module to the package")?;
if !editable {
write_python_part(writer, python_module, &module_name)
.context("Failed to add the python module to the package")?;
}

if develop {
if develop || editable {
let base_path = python_module.join(&module_name);
fs::create_dir_all(&base_path)?;
let target = base_path.join("native.so");
Expand All @@ -705,7 +715,9 @@ pub fn write_cffi_module(

let relative = rust_module.strip_prefix(python_module.parent().unwrap())?;
module = relative.join(extension_name);
writer.add_directory(&module)?;
if !editable {
writer.add_directory(&module)?;
}
}
ProjectLayout::PureRust {
ref rust_module, ..
Expand All @@ -724,9 +736,11 @@ pub fn write_cffi_module(
}
};

writer.add_bytes(&module.join("__init__.py"), cffi_init_file().as_bytes())?;
writer.add_bytes(&module.join("ffi.py"), cffi_declarations.as_bytes())?;
writer.add_file_with_permissions(&module.join("native.so"), &artifact, 0o755)?;
if !editable {
writer.add_bytes(&module.join("__init__.py"), cffi_init_file().as_bytes())?;
writer.add_bytes(&module.join("ffi.py"), cffi_declarations.as_bytes())?;
writer.add_file_with_permissions(&module.join("native.so"), &artifact, 0o755)?;
}

Ok(())
}
Expand Down

0 comments on commit 7cd18f0

Please sign in to comment.