Skip to content

Commit

Permalink
feat: create run_bin function
Browse files Browse the repository at this point in the history
  • Loading branch information
luciorq committed Nov 14, 2024
1 parent 32db5ac commit 3c9f0b5
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 2 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Package: condathis
Title: Run Any CLI Tool on a Conda Environment
Version: 0.0.6.9007
Version: 0.0.6.9008
Authors@R: c(
person("Lucio", "Queiroz", , "[email protected]",
person(given = "Lucio",
family = "Queiroz",
role = c("aut", "cre", "cph"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-6090-1834")),
person(given = "Claudio",
family = "Zanettini",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export(micromamba_bin_path)
export(parse_output)
export(remove_env)
export(run)
export(run_bin)
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

* New `parse_output()` parse lines output streams from `run()` results into character vectors.

* New `run_bin()` runs binary installed in a conda environment without using `micromamba run`.

## Minor improvements and fixes

* Internal `micromamba` version bump to "2.0.2-2".
Expand Down
75 changes: 75 additions & 0 deletions R/run_bin.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#' Run a Binary from a Conda Environment Without Emvironment Activation
#'
#' Executes a binary command from a specified Conda environment without activating the environment or using its environment variables. This function temporarily clears Conda and Mamba-related environment variables to prevent interference, ensuring that the command runs in a clean environment.
#' Usually this is not what the user wants, check [run()] for the stable function to use.
#'
#' @inheritParams run
#'
#' @return An invisible list containing the results from `processx::run()`, including standard output and error.
#'
#' @examples
#' \dontrun{
#' # Example assumes that 'my_env' exists and contains 'python'
#' # Run 'python' with a script in 'my_env' environment
#' run_bin("python", "script.py", env_name = "my_env", verbose = "silent")
#'
#' # Run 'ls' command with additional arguments
#' run_bin("ls", "-la", env_name = "my_env")
#' }
#'
#' @export
run_bin <- function(
cmd,
...,
env_name = "condathis-env",
verbose = "silent",
error = c("cancel", "continue"),
stdout = "|",
stderr = "|") {
error <- rlang::arg_match(error)
if (isTRUE(identical(error, "cancel"))) {
error_var <- TRUE
} else {
error_var <- FALSE
}

verbose_list <- parse_strategy_verbose(strategy = verbose)
verbose_cmd <- verbose_list$cmd
verbose_output <- verbose_list$output

env_dir <- get_env_dir(env_name = env_name)
cmd_path <- fs::path(env_dir, "bin", cmd)

withr::local_envvar(
.new = list(
CONDA_SHLVL = 0,
MAMBA_SHLVL = 0,
CONDA_ENVS_PATH = "",
CONDA_ROOT_PREFIX = "",
CONDA_PREFIX = "",
MAMBA_ENVS_PATH = "",
MAMBA_ROOT_PREFIX = "",
MAMBA_PREFIX = "",
CONDARC = "",
MAMBARC = "",
CONDA_PROMPT_MODIFIER = "",
MAMBA_PROMPT_MODIFIER = "",
CONDA_DEFAULT_ENV = "",
MAMBA_DEFAULT_ENV = "",
R_HOME = ""
)
)
px_res <- processx::run(
command = fs::path_real(cmd_path),
args = c(
...
),
spinner = TRUE,
echo_cmd = verbose_cmd,
echo = verbose_output,
stdout = stdout,
stderr = stderr,
error_on_status = error_var
)
return(invisible(px_res))
}
62 changes: 62 additions & 0 deletions man/run_bin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3c9f0b5

Please sign in to comment.