From 6b9e52e085bbf1c62a2cabb52b85fc5c084be4d0 Mon Sep 17 00:00:00 2001 From: kamangir Date: Fri, 10 Jan 2025 22:16:30 -0800 Subject: [PATCH 1/2] =?UTF-8?q?start=20of=20conda-refactors-2025-01-10-Q7A?= =?UTF-8?q?oMc=20=F0=9F=AA=84=20-=20kamangir/bolt#746?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- abcli/.abcli/plugins/conda.sh | 63 ------------------- abcli/.abcli/plugins/conda/exists.sh | 12 ++++ abcli/.abcli/plugins/conda/list.sh | 7 +++ abcli/.abcli/plugins/conda/rm.sh | 14 +++++ abcli/.abcli/tests/help.sh | 6 ++ abcli/__init__.py | 2 +- abcli/help/conda.py | 90 ++++++++++++++++++++++++++++ abcli/help/functions.py | 2 + 9 files changed, 133 insertions(+), 65 deletions(-) create mode 100644 abcli/.abcli/plugins/conda/exists.sh create mode 100644 abcli/.abcli/plugins/conda/list.sh create mode 100644 abcli/.abcli/plugins/conda/rm.sh create mode 100644 abcli/help/conda.py diff --git a/README.md b/README.md index ebfe910b..db9883dd 100644 --- a/README.md +++ b/README.md @@ -63,4 +63,4 @@ create a copy of [`sample.env`](./abcli/sample.env) as `.env` and fill in the se [![pylint](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pylint.yml/badge.svg)](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pylint.yml) [![pytest](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pytest.yml/badge.svg)](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pytest.yml) [![bashtest](https://github.com/kamangir/awesome-bash-cli/actions/workflows/bashtest.yml/badge.svg)](https://github.com/kamangir/awesome-bash-cli/actions/workflows/bashtest.yml) [![PyPI version](https://img.shields.io/pypi/v/abcli.svg)](https://pypi.org/project/abcli/) [![PyPI - Downloads](https://img.shields.io/pypi/dd/abcli)](https://pypistats.org/packages/abcli) -built by 🌀 [`blue_options-4.177.1`](https://github.com/kamangir/awesome-bash-cli), based on 🪄 [`abcli-9.458.1`](https://github.com/kamangir/awesome-bash-cli). +built by 🌀 [`blue_options-4.180.1`](https://github.com/kamangir/awesome-bash-cli), based on 🪄 [`abcli-9.459.1`](https://github.com/kamangir/awesome-bash-cli). diff --git a/abcli/.abcli/plugins/conda.sh b/abcli/.abcli/plugins/conda.sh index ab714da6..a131c55c 100644 --- a/abcli/.abcli/plugins/conda.sh +++ b/abcli/.abcli/plugins/conda.sh @@ -3,69 +3,6 @@ function abcli_conda() { local task=$(abcli_unpack_keyword $1) - if [ "$task" == "help" ]; then - abcli_conda create "$@" - abcli_conda exists "$@" - abcli_conda list "$@" - abcli_conda remove "$@" - return - fi - - if [[ "$2" == "help" ]]; then - local options - case $task in - create) - options="${EOP}clone=,~install_plugin,${EOPE}name=${EOP},repo=,~recreate${EOP}" - abcli_show_usage "@conda create$ABCUL$options" \ - "create conda environment." - ;; - exists) - abcli_show_usage "@conda exists$ABCUL[]" \ - "does conda environment exist? true|false." - ;; - list) - abcli_show_usage "@conda list" \ - "show list of conda environments." - ;; - remove | rm) - abcli_show_usage "@conda remove|rm$ABCUL[]" \ - "remove conda environment." - ;; - *) - abcli_log_error "@conda: $task: command not found." - return 1 - ;; - esac - - return - fi - - if [[ ",remove,rm," == *",$task,"* ]]; then - local environment_name=$(abcli_clarify_input $2 abcli) - - conda activate base - conda remove -y --name $environment_name --all - - return - fi - - if [ "$task" == "exists" ]; then - local environment_name=$(abcli_clarify_input $2 abcli) - - if conda info --envs | grep -q "^$environment_name "; then - echo 1 - else - echo 0 - fi - - return - fi - - if [ "$task" == "list" ]; then - conda info --envs "${@:2}" - return - fi - local function_name="abcli_conda_$task" if [[ $(type -t $function_name) == "function" ]]; then $function_name "${@:2}" diff --git a/abcli/.abcli/plugins/conda/exists.sh b/abcli/.abcli/plugins/conda/exists.sh new file mode 100644 index 00000000..eb400d3d --- /dev/null +++ b/abcli/.abcli/plugins/conda/exists.sh @@ -0,0 +1,12 @@ +#! /usr/bin/env bash + +function abcli_conda_exists() { + local options=$1 + local environment_name=$(abcli_option "$options" name abcli) + + if conda info --envs | grep -q "^$environment_name "; then + echo 1 + else + echo 0 + fi +} diff --git a/abcli/.abcli/plugins/conda/list.sh b/abcli/.abcli/plugins/conda/list.sh new file mode 100644 index 00000000..d347f7cd --- /dev/null +++ b/abcli/.abcli/plugins/conda/list.sh @@ -0,0 +1,7 @@ +#! /usr/bin/env bash + +function abcli_conda_list() { + abcli_eval ,$1 \ + conda info \ + --envs "${@:2}" +} diff --git a/abcli/.abcli/plugins/conda/rm.sh b/abcli/.abcli/plugins/conda/rm.sh new file mode 100644 index 00000000..2f291e2d --- /dev/null +++ b/abcli/.abcli/plugins/conda/rm.sh @@ -0,0 +1,14 @@ +#! /usr/bin/env bash + +function abcli_conda_rm() { + local options=$1 + local environment_name=$(abcli_option "$options" name abcli) + + conda activate base + [[ $? -ne 0 ]] && return 1 + + abcli_eval ,$options \ + conda remove -y \ + --name $environment_name \ + --all +} diff --git a/abcli/.abcli/tests/help.sh b/abcli/.abcli/tests/help.sh index ca63055e..b2c49e07 100644 --- a/abcli/.abcli/tests/help.sh +++ b/abcli/.abcli/tests/help.sh @@ -19,6 +19,12 @@ function test_abcli_help() { \ "@cat" \ \ + "@conda" \ + "@conda create" \ + "@conda exists" \ + "@conda list" \ + "@conda rm" \ + \ "@docker browse " \ "@docker build " \ "@docker clear " \ diff --git a/abcli/__init__.py b/abcli/__init__.py index fe5efb4b..68ccb85e 100644 --- a/abcli/__init__.py +++ b/abcli/__init__.py @@ -6,7 +6,7 @@ DESCRIPTION = f"{ICON} A language to speak AI." -VERSION = "9.458.1" +VERSION = "9.459.1" REPO_NAME = "awesome-bash-cli" diff --git a/abcli/help/conda.py b/abcli/help/conda.py new file mode 100644 index 00000000..6b3df73a --- /dev/null +++ b/abcli/help/conda.py @@ -0,0 +1,90 @@ +from typing import List + +from blue_options.terminal import show_usage, xtra + + +def help_create( + tokens: List[str], + mono: bool, +) -> str: + options = "".join( + [ + xtra("clone=,~install_plugin,", mono=mono), + "name=", + xtra(",repo=,~recreate", mono=mono), + ] + ) + + return show_usage( + [ + "@conda", + "create", + f"[{options}]", + ], + "create conda environment.", + mono=mono, + ) + + +def help_exists( + tokens: List[str], + mono: bool, +) -> str: + options = "name=" + + return show_usage( + [ + "@conda", + "exists", + f"[{options}]", + ], + "does conda environment exist? true | false.", + mono=mono, + ) + + +def help_list( + tokens: List[str], + mono: bool, +) -> str: + options = xtra("dryrun", mono=mono) + + return show_usage( + [ + "@conda", + "list", + f"[{options}]", + ], + "show list of conda environments.", + mono=mono, + ) + + +def help_rm( + tokens: List[str], + mono: bool, +) -> str: + options = "".join( + [ + xtra("dryrun,", mono=mono), + "name=", + ] + ) + + return show_usage( + [ + "@conda", + "rm", + f"[{options}]", + ], + "rm conda environment.", + mono=mono, + ) + + +help_functions = { + "create": help_create, + "exists": help_exists, + "list": help_list, + "rm": help_rm, +} diff --git a/abcli/help/functions.py b/abcli/help/functions.py index 5047a4b7..2f4e384a 100644 --- a/abcli/help/functions.py +++ b/abcli/help/functions.py @@ -3,6 +3,7 @@ from abcli.help.aws_batch import help_functions as help_aws_batch from abcli.help.blueness import help_blueness from abcli.help.browse import help_browse +from abcli.help.conda import help_functions as help_conda from abcli.help.cp import help_cp from abcli.help.docker import help_functions as help_docker from abcli.help.download import help_download @@ -50,6 +51,7 @@ "blueness": help_blueness, "browse": help_browse, "cat": help_cat, + "conda": help_conda, "cp": help_cp, "docker": help_docker, "download": help_download, From d9f6d83691e3f30e630e3e21305fba21625abaf4 Mon Sep 17 00:00:00 2001 From: kamangir Date: Fri, 10 Jan 2025 22:41:17 -0800 Subject: [PATCH 2/2] @conda rm refactor - kamangir/bolt#746 --- README.md | 2 +- abcli/.abcli/plugins/conda/rm.sh | 6 ++++++ abcli/__init__.py | 2 +- abcli/help/conda.py | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index db9883dd..dc027e6b 100644 --- a/README.md +++ b/README.md @@ -63,4 +63,4 @@ create a copy of [`sample.env`](./abcli/sample.env) as `.env` and fill in the se [![pylint](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pylint.yml/badge.svg)](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pylint.yml) [![pytest](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pytest.yml/badge.svg)](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pytest.yml) [![bashtest](https://github.com/kamangir/awesome-bash-cli/actions/workflows/bashtest.yml/badge.svg)](https://github.com/kamangir/awesome-bash-cli/actions/workflows/bashtest.yml) [![PyPI version](https://img.shields.io/pypi/v/abcli.svg)](https://pypi.org/project/abcli/) [![PyPI - Downloads](https://img.shields.io/pypi/dd/abcli)](https://pypistats.org/packages/abcli) -built by 🌀 [`blue_options-4.180.1`](https://github.com/kamangir/awesome-bash-cli), based on 🪄 [`abcli-9.459.1`](https://github.com/kamangir/awesome-bash-cli). +built by 🌀 [`blue_options-4.180.1`](https://github.com/kamangir/awesome-bash-cli), based on 🪄 [`abcli-9.460.1`](https://github.com/kamangir/awesome-bash-cli). diff --git a/abcli/.abcli/plugins/conda/rm.sh b/abcli/.abcli/plugins/conda/rm.sh index 2f291e2d..6c87a7e9 100644 --- a/abcli/.abcli/plugins/conda/rm.sh +++ b/abcli/.abcli/plugins/conda/rm.sh @@ -4,6 +4,12 @@ function abcli_conda_rm() { local options=$1 local environment_name=$(abcli_option "$options" name abcli) + local exists=$(abcli_conda_exists $environment_name) + if [[ "$exists" == 0 ]]; then + abcli_log_warning "@conda: $environment_name does not exist." + return 0 + fi + conda activate base [[ $? -ne 0 ]] && return 1 diff --git a/abcli/__init__.py b/abcli/__init__.py index 68ccb85e..9c1df5a3 100644 --- a/abcli/__init__.py +++ b/abcli/__init__.py @@ -6,7 +6,7 @@ DESCRIPTION = f"{ICON} A language to speak AI." -VERSION = "9.459.1" +VERSION = "9.460.1" REPO_NAME = "awesome-bash-cli" diff --git a/abcli/help/conda.py b/abcli/help/conda.py index 6b3df73a..fde0104f 100644 --- a/abcli/help/conda.py +++ b/abcli/help/conda.py @@ -38,7 +38,7 @@ def help_exists( "exists", f"[{options}]", ], - "does conda environment exist? true | false.", + "does conda environment exist?", mono=mono, )