Skip to content

Instantly share code, notes, and snippets.

@Athesto
Last active September 11, 2021 23:47
Show Gist options
  • Save Athesto/979b93ab8f3ee13842252dfcd91927f2 to your computer and use it in GitHub Desktop.
Save Athesto/979b93ab8f3ee13842252dfcd91927f2 to your computer and use it in GitHub Desktop.
venv is a python's virtual env manager.
#!/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 "$@"
@Athesto
Copy link
Author

Athesto commented Jun 21, 2021

README of venv

LICENSE-BADGE

venv is a script that manages the virtual environments of the python module venv (python3 -m venv)

How to install

Pre: Install the python's module

$ sudo apt update
...
$ sudo apt install python3-pip python3-venv -y
...
$ 

Installation

First, you need to create the directory, download the script and change it's permitions

$ mkdir -p ~/.local/bin
$ cd $_
$ curl -sLo venv https://gist.github.com/Athesto/979b93ab8f3ee13842252dfcd91927f2/raw/venv
$ chmod u+x venv

Notes

  • Remember check if the $PATH has the $HOME/.local/bin included
    • If this is your first time with this directory and the .profile has already the importation. Just restart the terminal to see it
  • The venv (script) uses the path $HOME/.venv as installation directory. You can edit it in the header of the file
$ head -9 ~/.local/bin/venv
  #!/usr/bin/env bash
  # shellcheck disable=SC1090
  
  # VARIABLES ----------------------------------
  INSTALLATION_DIR="$HOME/.venv"
  # --------------------------------------------
$ 
  • The venv (python3 module) uses the source command to run the process and you would need to use it in every command.
    • To prevent this. Create an alias alias venv="source venv" in the ~/.bashrc
$ echo 'alias venv="source venv"' >> .bashrc
$ source .bashrc
$ type venv
venv is aliased to `source venv'

How to use

$ venv -h
use any of these cmds [list/start/create/destroy/restore]
$ venv list
dir not found
$ venv create
create main
$ venv create foo
create foo
$ venv list
main
foo
$ venv start
(main) $
(main) $  venv stop
$ venv destroy 
You are going to destroy main
This operation is permanent. Are you sure? [y/n]: y
$ venv list
foo
$ venv restore foo
You are going to restore foo
This operation is permanent. Are you sure? [y/n]: y
restore foo
Package       Version
------------- -------
pip           20.0.2
pkg-resources 0.0.0
setuptools    44.0.0
(foo) $  venv stop
$

Suggestions

  • If you found something that should not be there (an issue)
  • Or you think that the redaction needs to be more specific
  • Or you think that you need something more (a feature)
  • Or there is something else

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment