Commit 9b0847ff authored by psachin's avatar psachin 🎮

Python 3 support(Default)

With some other improvements like:
- Ability to use custom Python path using -p|--python with -n|--new (issue: #6)
- Confirm delete operation before deleting a virtual environment
- Using Local namespace for variables to avoid conflict with environmental vars.

Resolves: #8Signed-off-by: psachin's avatarSachin Patil <psachin@redhat.com>
parent 70a2b52b
2019-02-16 Sachin Patil <psachin@redhat.com>
* Python 3 support
2016-03-14 Sachin <psachin@redhat.com>
* Independent repo
......@@ -9,4 +13,3 @@
2014-04-25 Sachin <iclcoolster@gmail.com>
* TAB completion
* [[./img/logo.png]]
/Lightweight Python virtual environment manager/
/A Lightweight Python virtual environment manager/
For more than a year *setV* was lying under [[https://github.com/psachin/bash_scripts][bash_scripts]]. I use it
as an alternative to [[https://virtualenvwrapper.readthedocs.org/][virtualenvwrapper]]. It is a Bash function which
provides basic features such as:
- Create new virtual environment
- Delete existing virtual environment(s)
- List existing virtual environment(s)
- In case you don't remember virtual environment name, TAB completion
works :)
- Python 3 support by default
- Create a new virtual environment.
- Create a new virtual environment using custom Python path using =-p|--python=.
- Delete an existing virtual environment(s).
- List all existing virtual environment(s).
- In case you don't remember the virtual environment name, TAB completion works :)
** Installation
Run the command
......@@ -17,7 +18,7 @@
#+END_SRC
** Usage
- First of all, the command is =setv=
- First of all, the command is =setv=!!
- When you install =setv=, the installation script will ask you to
source =~/.bashrc= or =~/.bash_profile=. Please do that.
......@@ -26,17 +27,18 @@
*** Create virtual environment
#+BEGIN_SRC sh
setv --new rango
setv --new rango # setv -n rango
# Or using a custom Python binary path
setv --new --python /opt/python/python3 rango # setv -n -p /opt/python/python3 rango
#+END_SRC
*** Activate existing environment
#+BEGIN_SRC sh
setv VIRTUAL_ENVIRONMENT_NAME
#+END_SRC
For example
#+BEGIN_SRC sh
# For example
setv rango
#+END_SRC
......@@ -63,7 +65,7 @@
- Bash-like TAB completion works for virtual environment names
** Contribute
- Create new issue.
- Create a new issue.
- Patches are welcome for existing issues.
** License
......
#!/usr/bin/env bash
# setV - Lightweight Python virtual environment manager.
# setV - A Lightweight Python virtual environment manager.
# Author: Sachin (psachin) <iclcoolster@gmail.com>
# Author's URL: psachin.github.io/about
# Author's URL: https://psachin.gitlab.io/about
#
# License: GNU GPL v3, See LICENSE file
#
# Configure(Optional):
# Set `VIRTUAL_DIR_PATH` value to your virtual environments
# Set `SETV_VIRTUAL_DIR_PATH` value to your virtual environments
# directory-path. By default it is set to '~/virtualenvs/'
#
# Usage:
......@@ -37,7 +37,10 @@
# $ deactivate
# Path to virtual environment directory
VIRTUAL_DIR_PATH="$HOME/virtualenvs/"
SETV_VIRTUAL_DIR_PATH="$HOME/virtualenvs/"
# Default python version to use. This decides whether to use `virtualenv` or `python3 -m venv`
SETV_PYTHON_VERSION=3 # Defaults to Python3
SETV_PY_PATH=$(which python${SETV_PYTHON_VERSION})
function _setvcomplete_()
{
......@@ -49,12 +52,9 @@ function _setvcomplete_()
local word=${COMP_WORDS[COMP_CWORD]} # Words thats being completed
local xpat='${word}' # Filter pattern. Include
# only words in variable '$names'
local names=$(ls -l "${VIRTUAL_DIR_PATH}" | egrep '^d' | awk -F " " '{print $NF}') # Virtual environment names
local names=$(ls -l "${SETV_VIRTUAL_DIR_PATH}" | egrep '^d' | awk -F " " '{print $NF}') # Virtual environment names
COMPREPLY=($(compgen -W "$names" -X "$xpat" -- "$word")) # 'compgen
# generates
# the
# results'
COMPREPLY=($(compgen -W "$names" -X "$xpat" -- "$word")) # compgen generates the results
}
function _setv_help_() {
......@@ -63,9 +63,27 @@ function _setv_help_() {
echo Positional argument:
echo -e "NAME Activate virtual env."
echo Optional arguments:
echo -e "-l, --list List all virtual envs."
echo -e "-n, --new NAME Create virtual env."
echo -e "-d, --delete NAME Delete existing virtual env."
echo -e "-l, --list List all Virtual Envs."
echo -e "-n, --new NAME Create a new Python Virtual Env."
echo -e "-d, --delete NAME Delete existing Python Virtual Env."
echo -e "-p, --python PATH Python binary path."
}
function _setv_custom_python_path()
{
if [ -f "${1}" ];
then
if [ "`expr $1 : '.*python\([2,3]\)'`" = "3" ];
then
SETV_PYTHON_VERSION=3
else
SETV_PYTHON_VERSION=2
fi
SETV_PY_PATH=${1}
_setv_create $2
else
echo "Error: Path ${1} does not exist!"
fi
}
function _setv_create()
......@@ -77,8 +95,15 @@ function _setv_create()
_setv_help_
else
echo "Creating new virtual environment with the name: $1"
virtualenv -p $(which python) ${VIRTUAL_DIR_PATH}${1}
echo "You can activate the environment by typing: setv ${1}"
if [ ${SETV_PYTHON_VERSION} -eq 3 ];
then
${SETV_PY_PATH} -m venv ${SETV_VIRTUAL_DIR_PATH}${1}
else
virtualenv -p ${SETV_PY_PATH} ${SETV_VIRTUAL_DIR_PATH}${1}
fi
echo "You can now activate the Python virtual environment by typing: setv ${1}"
fi
}
......@@ -91,11 +116,16 @@ function _setv_delete()
echo "You need to pass virtual environment name"
_setv_help_
else
if [ -d ${VIRTUAL_DIR_PATH}${1} ];
if [ -d ${SETV_VIRTUAL_DIR_PATH}${1} ];
then
rm -rvf ${VIRTUAL_DIR_PATH}${1}
read -p "Really delete this virtual environment(Y/N)? " yes_no
case $yes_no in
Y|y) rm -rvf ${SETV_VIRTUAL_DIR_PATH}${1};;
N|n) echo "Leaving the virtual environment as it is.";;
*) echo "You need to enter either Y/y or N/n"
esac
else
echo "No virtual environment with name: ${1}"
echo "Error: No virtual environment found by the name: ${1}"
fi
fi
}
......@@ -103,8 +133,8 @@ function _setv_delete()
function _setv_list()
{
# Lists all virtual environments if ran with -l|--list flag
echo -e "List of virtual environments you have under ${VIRTUAL_DIR_PATH}:\n"
for virt in $(ls -l "${VIRTUAL_DIR_PATH}" | egrep '^d' | awk -F " " '{print $NF}')
echo -e "List of virtual environments you have under ${SETV_VIRTUAL_DIR_PATH}:\n"
for virt in $(ls -l "${SETV_VIRTUAL_DIR_PATH}" | egrep '^d' | awk -F " " '{print $NF}')
do
echo ${virt}
done
......@@ -115,15 +145,16 @@ function setv() {
if [ $# -eq 0 ];
then
_setv_help_
else
elif [ $# -le 3 ];
then
case "${1}" in
-n|--new) _setv_create ${2};;
-d|--delete) _setv_delete ${2};;
-l|--list) _setv_list;;
*) if [ -d ${VIRTUAL_DIR_PATH}${1} ];
*) if [ -d ${SETV_VIRTUAL_DIR_PATH}${1} ];
then
# Activate the virtual environment
source ${VIRTUAL_DIR_PATH}${1}/bin/activate
source ${SETV_VIRTUAL_DIR_PATH}${1}/bin/activate
else
# Else throw an error message
echo "Sorry, you don't have any virtual environment with the name: ${1}"
......@@ -131,6 +162,12 @@ function setv() {
fi
;;
esac
elif [ $# -le 5 ];
then
case "${2}" in
-p|--python) _setv_custom_python_path ${3} ${4};;
*) _setv_help_;;
esac
fi
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment