Last active
April 18, 2021 20:58
-
-
Save alex65536/e69adc1cb814b735d2eb775e2ac61097 to your computer and use it in GitHub Desktop.
Revisions
-
alex65536 revised this gist
Apr 18, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
Empty file. -
alex65536 revised this gist
Apr 18, 2021 . 1 changed file with 10 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,21 +4,18 @@ # # Copyright 2020-2021 Alexander Kernozhitsky <[email protected]> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. VENV_DIR=~/venvs -
alex65536 revised this gist
Apr 18, 2021 . No changes.There are no files selected for viewing
-
alex65536 created this gist
Apr 18, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,172 @@ #!/bin/bash # # A small shell script to handle all your virtualenvs # # Copyright 2020-2021 Alexander Kernozhitsky <[email protected]> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # VENV_DIR=~/venvs validate_name() { [[ "$(grep -cEZ '^[a-zA-Z0-9-]+$' <<<"$NAME")" == 1 ]] } ensure_name() { local NAME="$1" local DIR="$VENV_DIR/$NAME" if ! validate_name "$NAME"; then echo "Error: Env name $NAME contains invalid chars" exit 1 fi if ! [[ -d "$DIR" ]]; then echo "Error: Env $NAME does not exist" exit 1 fi } init() { if [[ "$#" != 1 ]]; then echo "Usage: $0 init NAME" exit 1 fi local NAME="$1" if ! validate_name "$NAME"; then echo "Error: Env name $NAME contains invalid chars" exit 1 fi local DIR="$VENV_DIR/$NAME" if [[ -d "$DIR" ]]; then echo "Error: Env $NAME already exists" exit 1 fi python3 -m venv --upgrade-deps "$DIR" } start() { if [[ "$#" != 1 ]]; then echo "Usage: $0 start NAME" exit 1 fi local NAME="$1" local DIR="$VENV_DIR/$NAME" ensure_name "$NAME" bash --rcfile <(echo ". $HOME/.bashrc; . $DIR/bin/activate;") } delete() { if [[ "$#" != 1 ]]; then echo "Usage: $0 delete NAME" exit 1 fi local NAME="$1" local DIR="$VENV_DIR/$NAME" ensure_name "$NAME" while :; do echo -n "Are you sure to delete $DIR? [y/n] " read -r ANS case "$ANS" in y|Y) break ;; n|N) exit 1 ;; *) echo "Invalid option $ANS" ;; esac done rm -rf "$DIR" } list() { if [[ "$#" != 0 ]]; then echo "Usage: $0 info" exit 1 fi ls -1 "$VENV_DIR" } info() { if [[ "$#" != 0 ]]; then echo "Usage: $0 info" exit 1 fi echo "Penv: a tiny Python venv manager" echo "Copyright (c) 2020-2021 Alexander Kernozhitsky" echo "Version: 0.1.1-beta" } py() { if [[ "$#" != 1 ]]; then echo "Usage: $0 py NAME" exit 1 fi local NAME="$1" local DIR="$VENV_DIR/$NAME" ensure_name "$NAME" bash -c ' . "$0"/bin/activate python3 ' "$DIR" } ipython() { if [[ "$#" != 1 ]]; then echo "Usage: $0 ipython NAME" exit 1 fi local NAME="$1" local DIR="$VENV_DIR/$NAME" ensure_name "$NAME" bash -c ' . "$0"/bin/activate grep -E "^ipython\s" <<<"$(pip list)" >/dev/null || pip install ipython ipython3 ' "$DIR" } notebook() { if [[ "$#" != 1 ]]; then echo "Usage: $0 notebook NAME" exit 1 fi local NAME="$1" local DIR="$VENV_DIR/$NAME" ensure_name "$NAME" bash -c ' . "$0"/bin/activate grep -E "^notebook\s" <<<"$(pip list)" >/dev/null || pip install notebook jupyter-notebook ' "$DIR" } unknown() { echo "Unknown command '$CMD'" echo "Usage: $0 init|start|delete|list|info|py|ipython|notebook" exit 1 } CMD="$1" shift case "$CMD" in "init") init "$@" ;; "start") start "$@" ;; "delete") delete "$@" ;; "list") list "$@" ;; "info") info "$@" ;; "py") py "$@" ;; "ipython") ipython "$@" ;; "notebook") notebook "$@" ;; *) unknown "$CMD" ;; esac