Skip to content

Commit

Permalink
Add support for passing environment variables (#82)
Browse files Browse the repository at this point in the history
When including an `env` key in the `conda` and `pip` config sections,
ozy will now include those environment variables in the environment when
executing the installer process.

Co-authored-by: David Harks <[email protected]>
  • Loading branch information
dwink and David Harks authored Sep 27, 2024
1 parent 6141b58 commit 9601cf3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
22 changes: 21 additions & 1 deletion rozy/src/installers/conda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ pub struct Conda {
channels: Vec<String>,
conda_bin: String,
pyinstaller: bool,
env: Vec<(String, String)>,
}

pub fn conda_install(
conda_bin: &str,
channels: &[String],
to_dir: &std::path::Path,
to_install: &[String],
extra_env: &[(String, String)],
) -> Result<()> {
if let Some(parent) = to_dir.parent() {
std::fs::create_dir_all(parent)?;
Expand All @@ -26,7 +28,10 @@ pub fn conda_install(
delete_if_exists(to_dir)?;

let conda_cache_dir = tempdir()?;
let env = vec![("CONDA_PKGS_DIRS", conda_cache_dir.path().to_str().unwrap())];
let mut env = vec![("CONDA_PKGS_DIRS", conda_cache_dir.path().to_str().unwrap())];
for pair in extra_env {
env.push((pair.0.as_str(), pair.1.as_str()));
}

let mut args = vec!["create", "-y", "-p", to_dir.to_str().unwrap()];
for arg in channels.iter() {
Expand Down Expand Up @@ -68,12 +73,26 @@ impl Conda {
_ => false,
};

let env = match app_config.get("env") {
Some(serde_yaml::Value::Mapping(env_map)) => env_map
.iter()
.map(|(a, b)| {
(
a.as_str().unwrap().to_owned(),
b.as_str().unwrap().to_owned(),
)
})
.collect(),
_ => vec![],
};

Ok(Conda {
package,
version: version.to_string(),
channels,
conda_bin,
pyinstaller,
env,
})
}
}
Expand All @@ -91,6 +110,7 @@ impl Installer for Conda {
&self.channels,
to_dir,
&[versioned_package],
&self.env,
)?;
Ok(())
}
Expand Down
16 changes: 16 additions & 0 deletions rozy/src/installers/pip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Pip {
package: String,
version: String,
channels: Vec<String>,
env: Vec<(String, String)>,
}

impl Pip {
Expand All @@ -20,10 +21,24 @@ impl Pip {
_ => vec![],
};

let env = match app_config.get("env") {
Some(serde_yaml::Value::Mapping(env_map)) => env_map
.iter()
.map(|(a, b)| {
(
a.as_str().unwrap().to_owned(),
b.as_str().unwrap().to_owned(),
)
})
.collect(),
_ => vec![],
};

Ok(Pip {
package,
version: version.to_string(),
channels,
env,
})
}
}
Expand All @@ -38,6 +53,7 @@ impl Installer for Pip {
&self.channels,
to_dir,
&[String::from("pip")],
&self.env,
)?;

let pip_command = to_dir.join("bin").join("pip");
Expand Down
2 changes: 2 additions & 0 deletions rozy/test_resource/unittest.ozy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ apps:
channels:
- channel_a
- channel_b
env:
MY_ENV_VAR: value
file_app:
version: 6.6.0
type: single_file
Expand Down

0 comments on commit 9601cf3

Please sign in to comment.