Skip to content

Commit

Permalink
Allow optional dependencies for installation (#85)
Browse files Browse the repository at this point in the history
Co-authored-by: Ofek Lev <[email protected]>
  • Loading branch information
trappitsch and ofek authored Mar 1, 2024
1 parent ff0225e commit 5b32fd8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ passthrough = [
"PYAPP_PIP_EXTRA_ARGS",
"PYAPP_PIP_VERSION",
"PYAPP_PROJECT_DEPENDENCY_FILE",
"PYAPP_PROJECT_FEATURES",
"PYAPP_PROJECT_NAME",
"PYAPP_PROJECT_PATH",
"PYAPP_PROJECT_VERSION",
Expand Down
6 changes: 6 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,11 @@ fn set_pip_version() {
set_runtime_variable(variable, env::var(variable).unwrap_or("latest".to_string()));
}

fn set_pip_project_features() {
let variable = "PYAPP_PROJECT_FEATURES";
set_runtime_variable(variable, env::var(variable).unwrap_or_default());
}

fn set_pip_extra_args() {
let variable = "PYAPP_PIP_EXTRA_ARGS";
set_runtime_variable(variable, env::var(variable).unwrap_or_default());
Expand Down Expand Up @@ -919,6 +924,7 @@ fn main() {
set_upgrade_virtualenv();
set_pip_external();
set_pip_version();
set_pip_project_features();
set_pip_extra_args();
set_pip_allow_config();
set_skip_install();
Expand Down
6 changes: 6 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ You may embed the project with the `PYAPP_PROJECT_PATH` option which should be a
!!! note
The project name and version is automatically derived from the metadata files inside.

## Features (extras) ## {: #project-features }

You may set the `PYAPP_PROJECT_FEATURES` option to select [optional dependency groups](https://packaging.python.org/en/latest/specifications/dependency-specifiers/#extras) that would usually be passed to installers within square brackets after the package name e.g. `pkg[foo,bar]`. In that example, you would set `PYAPP_PROJECT_FEATURES` to `foo,bar`.

This also works when [embedding the project](#project-embedding).

## Execution mode

The following options are mutually exclusive:
Expand Down
4 changes: 4 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ pub fn exec_notebook_path() -> PathBuf {
.join(env!("PYAPP__EXEC_NOTEBOOK_NAME"))
}

pub fn pip_project_features() -> String {
env!("PYAPP_PROJECT_FEATURES").into()
}

pub fn pip_extra_args() -> String {
env!("PYAPP_PIP_EXTRA_ARGS").into()
}
Expand Down
18 changes: 16 additions & 2 deletions src/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ fn install_project() -> Result<()> {
)
})?;

command.arg(temp_path.to_string_lossy().as_ref());
command.arg(apply_project_features(
temp_path.to_string_lossy().as_ref().to_string(),
));

let wait_message = if binary_only && file_name.ends_with(".whl") {
format!("Unpacking {}", install_target)
Expand All @@ -276,7 +278,11 @@ fn install_project() -> Result<()> {

let dependency_file = app::project_dependency_file();
if dependency_file.is_empty() {
command.arg(format!("{}=={}", app::project_name(), app::project_version()).as_str());
command.arg(format!(
"{}=={}",
apply_project_features(app::project_name()),
app::project_version()
));
pip_install(command, wait_message)
} else {
pip_install_dependency_file(&dependency_file, command, wait_message)
Expand Down Expand Up @@ -391,3 +397,11 @@ fn check_setup_status(status: ExitStatus, output: String) -> Result<()> {

Ok(())
}

fn apply_project_features(install_target: String) -> String {
if app::pip_project_features().is_empty() {
install_target
} else {
format!("{install_target}[{}]", app::pip_project_features())
}
}

0 comments on commit 5b32fd8

Please sign in to comment.