From a9cfed93611bd2fb929c7b0a45326e1c44e35b6c Mon Sep 17 00:00:00 2001 From: kamangir Date: Sat, 9 Nov 2024 20:15:43 -0800 Subject: [PATCH] @seed -> @options - kamangir/bolt#746 --- README.md | 2 +- abcli/.abcli/plugins/seed.sh | 261 ----------------------------------- abcli/.abcli/tests/help.sh | 4 + abcli/__init__.py | 2 +- abcli/help/functions.py | 2 + abcli/help/seed.py | 57 ++++++++ 6 files changed, 65 insertions(+), 263 deletions(-) delete mode 100644 abcli/.abcli/plugins/seed.sh create mode 100644 abcli/help/seed.py diff --git a/README.md b/README.md index dc1627be..33453493 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.153.1`](https://github.com/kamangir/awesome-bash-cli), based on 🪄 [`abcli-9.412.1`](https://github.com/kamangir/awesome-bash-cli). +built by 🌀 [`blue_options-4.154.1`](https://github.com/kamangir/awesome-bash-cli), based on 🪄 [`abcli-9.413.1`](https://github.com/kamangir/awesome-bash-cli). diff --git a/abcli/.abcli/plugins/seed.sh b/abcli/.abcli/plugins/seed.sh deleted file mode 100644 index 21d1a5cd..00000000 --- a/abcli/.abcli/plugins/seed.sh +++ /dev/null @@ -1,261 +0,0 @@ -#! /usr/bin/env bash - -function abcli_seed() { - local task=$(abcli_unpack_keyword $1) - - local list_of_seed_targets="cloudshell|docker|ec2|jetson|headless_rpi|mac|rpi|sagemaker|sagemaker-system" - - if [ "$task" == "help" ]; then - local options="clipboard|filename=|key|screen,env=,eval,plugin=,~log" - abcli_show_usage "@seed$ABCUL[|$list_of_seed_targets]$ABCUL[$options]$ABCUL[$abcli_scripts_options]$ABCUL" \ - "generate and output a seed 🌱." - abcli_show_usage "@seed eject" \ - "eject seed 🌱." - abcli_show_usage "@seed list" \ - "list seed 🌱 targets." - return - fi - - if [ "$task" == "list" ]; then - local list_of_targets=$(declare -F | awk '{print $NF}' | grep 'abcli_seed_' | sed 's/abcli_seed_//' | tr '\n' '|') - list_of_targets="$list_of_targets|$list_of_seed_targets" - abcli_log_list "$list_of_targets" \ - --before "" \ - --delim \| \ - --after "target(s)" - return - fi - - local base64="base64" - # https://superuser.com/a/1225139 - [[ "$abcli_is_ubuntu" == true ]] && base64="base64 -w 0" - - # internal function. - if [ "$task" == "add_file" ]; then - local source_filename=$2 - - local destination_filename=$3 - - local var_name=_abcli_seed_$(echo $source_filename | tr / _ | tr . _ | tr - _ | tr \~ _ | tr \$ _) - - local seed="$var_name=\"$(cat $source_filename | $base64)\"$delim" - seed="${seed}echo \$$var_name | base64 --decode > $var_name$delim" - seed="$seed${sudo_prefix}mv -v $var_name $destination_filename" - - echo $seed - return - fi - - if [ "$task" == "eject" ]; then - if [[ "$abcli_is_jetson" == true ]]; then - sudo eject /media/abcli/SEED - else - sudo diskutil umount /Volumes/seed - fi - return - fi - - local seed="" - - local target=$(abcli_clarify_input $1 ec2) - - local options=$2 - local do_log=$(abcli_option_int "$options" log 1) - local do_eval=$(abcli_option_int "$options" eval 0) - local output=$(abcli_option_choice "$options" clipboard,key,screen clipboard) - [[ "$abcli_is_sagemaker" == true ]] && - output=screen - - local delim="\n" - local delim_section="\n\n" - if [ "$output" == "clipboard" ]; then - delim="; " - delim_section="; " - fi - - local env_name="" - [[ "$target" == "ec2" ]] && - env_name="worker" - env_name=$(abcli_option "$options" env $env_name) - - local sudo_prefix="sudo " - [[ "$target" == "sagemaker" ]] && - sudo_prefix="" - - if [ "$output" == "key" ]; then - local seed_path="/Volumes/seed" - [[ "$abcli_is_jetson" == true ]] && - seed_path="/media/abcli/SEED" - - if [ ! -d "$seed_path" ]; then - abcli_log_error "-abcli: seed: usb key not found." - return 1 - fi - - mkdir -p $seed_path/abcli/ - fi - - [[ "$do_log" == 1 ]] && - abcli_log "$abcli_fullname seed 🌱 -$output-> $target" - - local seed="#! /bin/bash$delim" - [[ "$output" == "clipboard" ]] && seed="" - - seed="${seed}echo \"$abcli_fullname seed for $target\"$delim_section" - - if [[ "|$list_of_seed_targets|" != *"|$target|"* ]]; then - # expected to append to/update $seed - abcli_seed_${target} "${@:2}" - else - if [ "$target" == docker ]; then - seed="${seed}source /root/git/awesome-bash-cli/abcli/.abcli/abcli.sh$delim" - else - if [[ "$target" != sagemaker ]]; then - if [ -d "$HOME/.kaggle" ]; then - seed="${seed}mkdir -p \$HOME/.kaggle$delim" - seed="$seed$(abcli_seed add_file $HOME/.kaggle/kaggle.json \$HOME/.kaggle/kaggle.json)$delim" - seed="${seed}chmod 600 \$HOME/.kaggle/kaggle.json$delim_section" - else - abcli_log_warning "-abcli: seed: kaggle.json not found." - fi - fi - - if [[ "$target" != sagemaker* ]] && [[ "$target" != cloudshell ]]; then - seed="$seed${sudo_prefix}rm -rf ~/.aws$delim" - seed="$seed${sudo_prefix}mkdir ~/.aws$delim_section" - seed="$seed$(abcli_seed add_file $HOME/.aws/config \$HOME/.aws/config)$delim" - seed="$seed$(abcli_seed add_file $HOME/.aws/credentials \$HOME/.aws/credentials)$delim_section" - fi - - if [[ "|cloudshell|sagemaker|" != *"|$target|"* ]]; then - seed="${seed}${sudo_prefix}mkdir -p ~/.ssh$delim_section" - seed="$seed"'eval "$(ssh-agent -s)"'"$delim_section" - seed="$seed$(abcli_seed add_file $HOME/.ssh/$abcli_git_ssh_key_name \$HOME/.ssh/$abcli_git_ssh_key_name)$delim" - seed="${seed}chmod 600 ~/.ssh/$abcli_git_ssh_key_name$delim" - seed="${seed}ssh-add -k ~/.ssh/$abcli_git_ssh_key_name$delim_section" - fi - - if [[ "$target" == "sagemaker-system" ]]; then - git_seed - - # https://chat.openai.com/c/8bdce889-a9fa-41c2-839f-f75c14d48e52 - seed="${seed}conda install -y unzip$delim_section" - fi - - if [[ "$target" == "sagemaker" ]]; then - seed="${seed}apt-get update$delim" - seed="${seed}apt install -y libgl1-mesa-glx$delim_section" - fi - - if [[ "$target" == *"rpi" ]]; then - seed="${seed}ssh-keyscan github.com | sudo tee -a ~/.ssh/known_hosts$delim_section" - fi - - if [[ "$target" != sagemaker ]] && [[ "$target" != cloudshell ]]; then - seed="${seed}"'ssh -T git@github.com'"$delim_section" - fi - - if [[ "$target" == *"rpi" ]]; then - # https://serverfault.com/a/1093530 - # https://packages.ubuntu.com/bionic/all/ca-certificates/download - certificate_name="ca-certificates_20211016ubuntu0.18.04.1_all" - seed="${seed}wget --no-check-certificate http://security.ubuntu.com/ubuntu/pool/main/c/ca-certificates/$certificate_name.deb$delim" - seed="$seed${sudo_prefix}sudo dpkg -i $certificate_name.deb$delim_section" - - seed="$seed${sudo_prefix}apt-get update --allow-releaseinfo-change$delim" - seed="$seed${sudo_prefix}apt-get install -y ca-certificates libgnutls30$delim" - seed="$seed${sudo_prefix}apt-get --yes --force-yes install git$delim_section" - fi - - local repo_address="git@github.com:kamangir/awesome-bash-cli.git" - [[ "$target" == sagemaker-system ]] && - repo_address="https://github.com/kamangir/awesome-bash-cli" - - if [[ "$target" == sagemaker ]]; then - seed="${seed}pip install --upgrade pip$delim_section" - seed="${seed}cd git/awesome-bash-cli${delim}" - else - seed="${seed}cd; mkdir -p git; cd git$delim" - seed="${seed}git clone $repo_address$delim" - seed="${seed}cd awesome-bash-cli${delim}" - seed="${seed}git checkout $abcli_git_branch; git pull$delim_section" - fi - - seed="$seed$(abcli_seed \ - add_file \ - $HOME/git/awesome-bash-cli/.env \ - \$HOME/git/awesome-bash-cli/.env)$delim_section" - - if [ "$target" == "headless_rpi" ]; then - seed="${seed}touch ~/storage/temp/ignore/headless$delim_section" - fi - - if [ "$target" == "rpi" ]; then - seed="${seed}python3 -m pip install --upgrade pip$delim" - seed="${seed}pip3 install -e .$delim" - seed="${seed}sudo python3 -m pip install --upgrade pip$delim" - seed="${seed}sudo pip3 install -e .$delim_section" - elif [ "$target" == "headless_rpi" ]; then - seed="${seed}sudo apt-get --yes --force-yes install python3-pip$delim" - seed="${seed}pip3 install -e .$delim" - seed="${seed}sudo pip3 install -e .$delim_section" - else - seed="${seed}pip3 install -e .$delim_section" - fi - - seed="${seed}source ./abcli/.abcli/abcli.sh$delim_section" - - if [ "$target" == "ec2" ]; then - seed="${seed}source ~/.bash_profile$delim_section" - elif [ "$target" == "rpi" ]; then - seed="${seed}source ~/.bashrc$delim_section" - fi - - if [ ! -z "$env_name" ]; then - seed="${seed}abcli_env dot copy $env_name$delim" - seed="${seed}abcli init$delim_section" - fi - - if [[ "$target" == sagemaker ]]; then - local plugin_name=$(abcli_option "$options" plugin) - - [[ ! -z "$plugin_name" ]] && - seed="${seed}abcli_conda create name=$plugin_name,~recreate$delim" - fi - fi - fi - - [[ "$do_eval" == 1 ]] && - seed="${seed}abcli_eval ${@:3}$delim_section" - - [[ "$target" == sagemaker* ]] && - abcli_log_warning "run \"bash\" before pasting the seed." - - if [ "$output" == "clipboard" ]; then - if [ "$abcli_is_mac" == true ]; then - echo $seed | pbcopy - elif [ "$abcli_is_ubuntu" == true ]; then - echo $seed | xclip -sel clip - fi - - [[ "$do_log" == 1 ]] && - abcli_log "📋 paste the seed 🌱 in the $target terminal." - elif [ "$output" == "key" ] || [ "$output" == "filename" ]; then - filename=$(abcli_option "$options" filename $abcli_object_path/seed) - [[ "$output" == "key" ]] && - filename="$seed_path/abcli/$target" - - echo -en $seed >$filename.sh - chmod +x $filename.sh - - echo "{\"version\":\"$abcli_version\"}" >$filename.json - - [[ "$do_log" == 1 ]] && - abcli_log "seed 🌱 -> $filename." - elif [ "$output" == "screen" ]; then - printf "$GREEN$seed$NC\n" - else - abcli_log_error "this should not happen - output: $output". - return 1 - fi -} diff --git a/abcli/.abcli/tests/help.sh b/abcli/.abcli/tests/help.sh index 6882294f..eea9ebe1 100644 --- a/abcli/.abcli/tests/help.sh +++ b/abcli/.abcli/tests/help.sh @@ -77,6 +77,10 @@ function test_abcli_help() { \ "@repeat" \ \ + "@seed" \ + "@seed eject" \ + "@seed list" \ + \ "@sleep" \ \ "@terraform" \ diff --git a/abcli/__init__.py b/abcli/__init__.py index 484e1e61..ff6feb5c 100644 --- a/abcli/__init__.py +++ b/abcli/__init__.py @@ -6,7 +6,7 @@ DESCRIPTION = f"{ICON} a language to speak AI." -VERSION = "9.412.1" +VERSION = "9.413.1" REPO_NAME = "awesome-bash-cli" diff --git a/abcli/help/functions.py b/abcli/help/functions.py index 533c8772..0d98b7e1 100644 --- a/abcli/help/functions.py +++ b/abcli/help/functions.py @@ -22,6 +22,7 @@ from abcli.help.pytest import help_pytest from abcli.help.repeat import help_repeat from abcli.help.sagemaker import help_functions as help_sagemaker +from abcli.help.seed import help_functions as help_seed from abcli.help.sleep import help_sleep from abcli.help.terraform import help_functions as help_terraform from abcli.help.upload import help_upload @@ -52,6 +53,7 @@ "plugins": help_plugins, "repeat": help_repeat, "sagemaker": help_sagemaker, + "seed": help_seed, "sleep": help_sleep, "terraform": help_terraform, "upload": help_upload, diff --git a/abcli/help/seed.py b/abcli/help/seed.py new file mode 100644 index 00000000..46a3f87f --- /dev/null +++ b/abcli/help/seed.py @@ -0,0 +1,57 @@ +from typing import List + +from blue_options.terminal import show_usage, xtra + + +def help_( + tokens: List[str], + mono: bool, +) -> str: + options = "clipboard|filename=|key|screen,env=,eval,plugin=,~log" + + return show_usage( + [ + "@seed", + "", + f"[{options}]", + "[cat,dryrun]", + "", + ], + "generate and output a seed 🌱.", + mono=mono, + ) + + +def help_eject( + tokens: List[str], + mono: bool, +) -> str: + return show_usage( + [ + "@seed", + "eject", + ], + "eject seed 🌱.", + mono=mono, + ) + + +def help_list( + tokens: List[str], + mono: bool, +) -> str: + return show_usage( + [ + "@seed", + "list", + ], + "list seed 🌱 targets.", + mono=mono, + ) + + +help_functions = { + "": help_, + "eject": help_eject, + "list": help_list, +}