#!/bin/bash
# sys/tool/virtualenv - Python ``virtualenv`` Tool Support
#
# This library encapsulates the Python ``virtualenv`` tool to provide
# an interface for managing virtual environment directories.

set -e


######
# Paths

# ve_dir() - Prints path to a private virtual environment for the
# given directory ($1).
ve_dir() { echo "${1:-.}/.venv"; }

# ve_activate_script() - Prints path to a private virtual environment for the
# given directory ($1).
ve_activate_script() {
	local vdir
	vdir=$(ve_dir "$1")
	echo "$vdir/bin/activate"
}


######
# Activation

# ve_activate() - Activates the private virtual environment for the
# given directory ($1).
ve_activate() {
	local dir=$1

	if is_function deactivate; then
		debug "ve_activate: skipped"
		return
	fi

	# find activation script or create it
	local activate
	activate=$(ve_activate_script "$dir")
	[ -f "$activate" ] || ve_create "$dir"

	# load the activation script in this shell
	debug "ve_activate: running \"$activate\""
	source "$activate"
}


######
# Management

# ve_create() - Creates a private virtual environment for the
# given directory ($1).  If the environment exists, this does nothing,
# to avoid disrupting it.
ve_create() {
	local vdir
	vdir=$(ve_dir "$1")
	if [ -d "$vdir" ]; then
		debug "ve_create: '$vdir' exists"
	else
		ve_update "$@"
	fi
}

# ve_update() - Updates the private virtual environment for the
# given directory ($1).  If the environment does not exist, it will be
# created in that directory.
ve_update() {
	local vdir
	vdir=$(ve_dir "$1")
	run virtualenv "$vdir"
}

# ve_delete() - Deletes the private virtual environment for the given
# directory ($1).  Since the environment exists inside that directory,
# this function must be called before removing it.
ve_delete() {
	local vdir
	vdir=$(ve_dir "$1")
	if [ -d "$vdir" ]; then
		run rm -r "$vdir"
	else
		debug "ve_delete: '$vdir' does not exist"
	fi
}


######
# ``easy_install`` Support

# ve_install() - Uses ``easy_install`` to install Python packages from
# PyPI, version control, local projects, or distribution files.
#
# Each target may be specified using one of the following forms:
#
#   * ``<module>[-<version>]``
#   * ``svn://<url>`` or ``{http,https}://<url>``
#   * ``git+{http,https,ssh}://<url>``
#   * ``bzr+{http,https}://<url>``
#   * ``hg+{http,https}://<url>``
#
# Where the ``url`` may be of the form::
#
#   [<username>[:<password>]@]<hostname>[<path_to_repo>]
#
# The ``easy_install`` tool extends the ``url`` specification with the
# following optional components:
#
#   * ``@<tag>``: Specifies the branch/tag/version
#   * ``#egg=<module>``: Specifies the module name
#
# For more information, see the ``pip`` user guide:
#
#   https://pip.pypa.io/en/latest/user_guide/#installing-packages
#
# $@ - List of package targets
ve_install() { run_for_each easy_install "$@"; }

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:36:17 PDT 2017 by mcsh d14 v0.23.0.