From 3c9f0b5649e1982f87b933c2018180e5bbe8b8dd Mon Sep 17 00:00:00 2001 From: luciorq Date: Thu, 14 Nov 2024 16:35:29 -0500 Subject: [PATCH] feat: create run_bin function --- DESCRIPTION | 6 ++-- NAMESPACE | 1 + NEWS.md | 2 ++ R/run_bin.R | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ man/run_bin.Rd | 62 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 R/run_bin.R create mode 100644 man/run_bin.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 8334744..b100331 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "luciorqueiroz@gmail.com", + person(given = "Lucio", + family = "Queiroz", role = c("aut", "cre", "cph"), + email = "luciorqueiroz@gmail.com", comment = c(ORCID = "0000-0002-6090-1834")), person(given = "Claudio", family = "Zanettini", diff --git a/NAMESPACE b/NAMESPACE index abc5141..2c3ece6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,3 +13,4 @@ export(micromamba_bin_path) export(parse_output) export(remove_env) export(run) +export(run_bin) diff --git a/NEWS.md b/NEWS.md index bd48191..5dc55f5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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". diff --git a/R/run_bin.R b/R/run_bin.R new file mode 100644 index 0000000..cc7d981 --- /dev/null +++ b/R/run_bin.R @@ -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)) +} diff --git a/man/run_bin.Rd b/man/run_bin.Rd new file mode 100644 index 0000000..57f0a9c --- /dev/null +++ b/man/run_bin.Rd @@ -0,0 +1,62 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/run_bin.R +\name{run_bin} +\alias{run_bin} +\title{Run a Binary from a Conda Environment Without Emvironment Activation} +\usage{ +run_bin( + cmd, + ..., + env_name = "condathis-env", + verbose = "silent", + error = c("cancel", "continue"), + stdout = "|", + stderr = "|" +) +} +\arguments{ +\item{cmd}{Character. The main command to be executed in the Conda environment.} + +\item{...}{Additional arguments to be passed to the command. These arguments will be passed directly to the command executed in the Conda environment. +File paths should not contain special characters or spaces.} + +\item{env_name}{Character. The name of the Conda environment where the tool will be run. Defaults to \code{"condathis-env"}. +If the specified environment does not exist, it will be created automatically using \code{create_env()}.} + +\item{verbose}{Character string specifying the verbosity level of the function's output. Acceptable values are: +\itemize{ +\item \strong{"silent"}: Suppress all output from internal command-line tools. Equivalent to \code{FALSE}. +\item \strong{"cmd"}: Print the internal command(s) passed to the command-line tool. +\item \strong{"output"}: Print the standard output and error from the command-line tool to the screen. Note that the order of the standard output and error lines may not be correct, as standard output is typically buffered. If the standard output and/or error is redirected to a file or they are ignored, they will not be echoed. +\item \strong{"full"}: Print both the internal command(s) (\code{"cmd"}) and their standard output and error (\code{"output"}). Equivalent to \code{TRUE}. +Logical values \code{FALSE} and \code{TRUE} are also accepted for backward compatibility but are \emph{soft-deprecated}. Please use \code{"silent"} and \code{"full"} respectively instead. +}} + +\item{error}{Character string. How to handle errors. Options are \code{"cancel"} or \code{"continue"}. Defaults to \code{"cancel"}.} + +\item{stdout}{Default: "|" keep stdout to the R object +returned by \code{run()}. +A character string can be used to define a file path to be used as standard output. e.g: "output.txt".} + +\item{stderr}{Default: "|" keep stderr to the R object +returned by \code{run()}. +A character string can be used to define a file path to be used as standard error. e.g: "error.txt".} +} +\value{ +An invisible list containing the results from \code{processx::run()}, including standard output and error. +} +\description{ +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 \code{\link[=run]{run()}} for the stable function to use. +} +\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") +} + +}