-
-
Save Athesto/979b93ab8f3ee13842252dfcd91927f2 to your computer and use it in GitHub Desktop.
venv is a python's virtual env manager.
This file contains 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 characters
#!/usr/bin/env bash | |
# VERSION=0.2.3 | |
# shellcheck disable=SC1090 | |
# VARIABLES ---------------------------------- | |
INSTALLATION_DIR="$HOME/.venv" | |
# -------------------------------------------- | |
function main() | |
{ | |
env_name="${2-main}" | |
env_dir="${INSTALLATION_DIR}/${env_name}" | |
output_status=0 | |
case $1 in | |
'create') create_env "$@" ;; | |
'destroy') destroy_env "$@" ;; | |
'list') list_env ;; | |
'restore') restore_env "$@" ;; | |
'stop') deactivate ;; | |
'start') start_env ;; | |
'--help' | '-h') call_help ;; | |
'--version' | '-v') version ;; | |
*) | |
echo "Uknown command: $1" 1>&2 | |
call_help 1>&2 | |
output_status=3 | |
;; | |
esac | |
(( output_status == 1 )) && output_status=0; | |
return $output_status | |
} | |
function version() | |
{ | |
VERSION=$(head "${PROGRAM_PATH}" | sed -n 's/^# VERSION=//p') | |
echo "$VERSION" | |
} | |
function call_help() | |
{ | |
echo "use any of these cmds [list/start/create/destroy/stop/restore]" | |
} | |
function start_env() | |
{ | |
if [[ ! -d "$env_dir" ]]; then | |
{ | |
echo "dir not found" 1>&2 | |
output_status=2 | |
return | |
} | |
fi | |
source "/${env_dir}/bin/activate" | |
} | |
function destroy_env() | |
{ | |
if [[ ! -d "$env_dir" ]]; then | |
{ | |
echo "dir not found" 1>&2 | |
output_status=2 | |
return | |
} | |
fi | |
echo You are going to "$1 ${env_name}" | |
echo -n "This operation is permanent. Are you sure? [y/n]: " | |
read -r isOK | |
[[ $isOK != 'y' ]] && { output_status=1; return; }; | |
rm -rf "${env_dir}" | |
} | |
function create_env() | |
{ | |
if [[ -d "$env_dir" ]]; then | |
{ | |
echo "dir already created" 1>&2 | |
output_status=2 | |
return | |
} | |
fi | |
echo "$@" | |
python3 -m venv "${env_dir}" | |
} | |
function restore_env() | |
{ | |
destroy_env "$@" | |
(( output_status == 0 )) \ | |
&& create_env "$@" \ | |
&& start_env \ | |
&& pip3 list | |
} | |
function list_env() | |
{ | |
if [[ ! -d $INSTALLATION_DIR ]]; then | |
{ | |
echo "dir not found" 1>&2 | |
output_status=2 | |
return | |
} | |
fi | |
find "$INSTALLATION_DIR" -name activate | rev | cut -d '/' -f3 | rev | |
} | |
#---------------------------------------- | |
# Entry point --------------------------- | |
#---------------------------------------- | |
PROGRAM_PATH=$(bash -c "type -p ${BASH_SOURCE[0]:-$0}") | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README of venv
How to install
Pre: Install the python's module
Installation
First, you need to create the directory, download the script and change it's permitions
Notes
$PATH
has the$HOME/.local/bin
included.profile
has already the importation. Just restart the terminal to see it$HOME/.venv
as installation directory. You can edit it in the header of the filesource
command to run the process and you would need to use it in every command.alias venv="source venv"
in the~/.bashrc
How to use
Suggestions
Please tell me in the comments
Licence
GPLv3
Author
Hi, I'm Gustavo Mejía and if it's works to you.
Please let me know in twitter in @im_tavo.