<commit_before>#!/bin/bash
#
# VirtualEnv shell helpers: easier create, remove, list/find and activate.
# Written by <NAME>, Feb 2015.

_SHENV_DEFAULT_ENVNAME=env
_SHENV_DEFAULT_PYTHON=python
_SHENV_CONFIG_DIR="$HOME/.config/shenv"
_SHENV_DB_PATH="$_SHENV_CONFIG_DIR/locate.db"
_SHENV_INDEX_ROOT="$HOME"
_SHENV_FIND_LIMIT=0.1  # in seconds

function fail() {
    echo "$@" >&2
}

# Creates a new environment in <path/to/env>, based on <python_exec>.
# Usage: mkenv [<path/to/env>] [<python_exec>]
function mkenv() {
    local envpath="${1:-$_SHENV_DEFAULT_ENVNAME}" output
    if [ -d "$envpath" ]; then
        fail "Directory '$envpath' already exists."
        return 1
    fi
    echo "Creating python environment in '$envpath'."

    local pyexe="${2:-$_SHENV_DEFAULT_PYTHON}" pypath
    if ! pypath=$(which "$pyexe"); then
        fail "Python executable '$pyexe' not found, failing-back to: '$_SHENV_DEFAULT_PYTHON'."
        pypath=$(which "$_SHENV_DEFAULT_PYTHON")
    fi
    local pyver=$("$pypath" --version 2>&1)
    if [[ ! $pyver =~ Python ]]; then
        fail "Unrecognized Python version/executable: '$pypath'."
        return 1
    fi
    echo "Using $pyver ($pypath)."

    mkdir -p "$envpath"
    cd "$envpath"
    output=$(virtualenv --no-site-packages -p "$pypath" -v . 2>&1)
    if [ $? -ne 0 ]; then
        echo -en "$output"
    fi
    . bin/activate
    cd - >/dev/null
}

# Destroys the active environment.
# Usage (while env active): rmenv
function rmenv() {
    local envpath="$VIRTUAL_ENV"
    if [ ! "$envpath" ]; then
        fail "Active environment not detected."
        return 1
    fi
    deactivate
    if _is_virtualenv "$envpath"; then
        rm -rf "$envpath"
    else
        fail "Invalid VirtualEnv path in VIRTUAL_ENV: '$envpath'."
        return 1
    fi
}

function _deactivate() {
    [ "$VIRTUAL_ENV" ] && deactivate
}

function _activate() {
    _deactivate
    source "$1/bin/activate"
}

function _is_virtualenv() {
    [ -e "$1/bin/activate_this.py" ] && [ -x "$1/bin/python" ]
}

# Lists all environments below the <start_dir>.
# Usage: lsenv [<start_dir> [<avoid_subdir>]]
function _lsenv_find() {
    local dir="${1:-.}" avoid="${2:-}"
    find "$dir" -path "$avoid" -prune -o \
        -name .git -o -name .hg -o -name .svn -prune -o -path '*/bin/activate_this.py' \
        -exec dirname '{}' \; 2>/dev/null | xargs -d'\n' -n1 -r dirname
}

# `lsenv` via `locate`
# Compatible with: lsenv [<start_dir> [<avoid_subdir>]]
function _lsenv_locate() {
    local dir="${1:-.}" avoid="${2:-}"
    local absdir=$(readlink -e "$dir")
    locate -d "$_SHENV_DB_PATH" "$absdir"'*/bin/activate_this.py' \
        | sed -e 's#/bin/activate_this\.py$##' -e "s#^$absdir#$dir#"
}

# Run `lsenv` via both `find` and `locate` in parallel and return
# as soon as the first method yields the results.
function _lsenv_locate_vs_find_race() {
    set +m
    local p_pid_find=$(mkftemp) p_pid_locate=$(mkftemp) p_pid_timer=$(mkftemp)
    { __find_and_return "$@" & echo $! >"$p_pid_find"; } 2>/dev/null
    { __locate_and_return "$@" & echo $! >"$p_pid_locate"; } 2>/dev/null
    { __find_fast_bailout & echo $! >"$p_pid_timer"; } 2>/dev/null
    wait
    rm "$p_pid_find" "$p_pid_locate" "$p_pid_timer"
    set -m
}
function __find_and_return() {
    local result=$(_lsenv_find "$@")
    local pid_locate pid_timer
    read pid_locate <"$p_pid_locate"
    read pid_timer <"$p_pid_timer"
    __kill $pid_locate $pid_timer
    [ "$result" ] && echo "$result"
}
function __locate_and_return() {
    local result=$(_lsenv_locate "$@")
    local pid_find pid_timer
    read pid_find <"$p_pid_find"
    read pid_timer <"$p_pid_timer"
    __kill $pid_find $pid_timer
    [ "$result" ] && echo "$result"
}
function __find_fast_bailout() {
    sleep "$_SHENV_FIND_LIMIT"
    local pid_find
    read pid_find <"$p_pid_find"
    __kill $pid_find
}
function __kill() {
    while [ "$#" -gt 0 ]; do
        sudo -u"$USER" rkill -TERM "$1" 1>/dev/null
        shift
    done
}

# Make fastest temporary file: like mktemp, but tries<commit_msg>fixed killing of descendant processes; ditched rkill (pslist) dependency<commit_after>#!/bin/bash
#
# VirtualEnv shell helpers: easier create, remove, list/find and activate.
# Written by <NAME>, Feb 2015.

_SHENV_DEFAULT_ENVNAME=env
_SHENV_DEFAULT_PYTHON=python
_SHENV_CONFIG_DIR="$HOME/.config/shenv"
_SHENV_DB_PATH="$_SHENV_CONFIG_DIR/locate.db"
_SHENV_INDEX_ROOT="$HOME"
_SHENV_FIND_LIMIT=0.1  # in seconds

function fail() {
    echo "$@" >&2
}

# Creates a new environment in <path/to/env>, based on <python_exec>.
# Usage: mkenv [<path/to/env>] [<python_exec>]
function mkenv() {
    local envpath="${1:-$_SHENV_DEFAULT_ENVNAME}" output
    if [ -d "$envpath" ]; then
        fail "Directory '$envpath' already exists."
        return 1
    fi
    echo "Creating python environment in '$envpath'."

    local pyexe="${2:-$_SHENV_DEFAULT_PYTHON}" pypath
    if ! pypath=$(which "$pyexe"); then
        fail "Python executable '$pyexe' not found, failing-back to: '$_SHENV_DEFAULT_PYTHON'."
        pypath=$(which "$_SHENV_DEFAULT_PYTHON")
    fi
    local pyver=$("$pypath" --version 2>&1)
    if [[ ! $pyver =~ Python ]]; then
        fail "Unrecognized Python version/executable: '$pypath'."
        return 1
    fi
    echo "Using $pyver ($pypath)."

    mkdir -p "$envpath"
    cd "$envpath"
    output=$(virtualenv --no-site-packages -p "$pypath" -v . 2>&1)
    if [ $? -ne 0 ]; then
        echo -en "$output"
    fi
    . bin/activate
    cd - >/dev/null
}

# Destroys the active environment.
# Usage (while env active): rmenv
function rmenv() {
    local envpath="$VIRTUAL_ENV"
    if [ ! "$envpath" ]; then
        fail "Active environment not detected."
        return 1
    fi
    deactivate
    if _is_virtualenv "$envpath"; then
        rm -rf "$envpath"
    else
        fail "Invalid VirtualEnv path in VIRTUAL_ENV: '$envpath'."
        return 1
    fi
}

function _deactivate() {
    [ "$VIRTUAL_ENV" ] && deactivate
}

function _activate() {
    _deactivate
    source "$1/bin/activate"
}

function _is_virtualenv() {
    [ -e "$1/bin/activate_this.py" ] && [ -x "$1/bin/python" ]
}

# Lists all environments below the <start_dir>.
# Usage: lsenv [<start_dir> [<avoid_subdir>]]
function _lsenv_find() {
    local dir="${1:-.}" avoid="${2:-}"
    find "$dir" -path "$avoid" -prune -o \
        -name .git -o -name .hg -o -name .svn -prune -o -path '*/bin/activate_this.py' \
        -exec dirname '{}' \; 2>/dev/null | xargs -d'\n' -n1 -r dirname
}

# `lsenv` via `locate`
# Compatible with: lsenv [<start_dir> [<avoid_subdir>]]
function _lsenv_locate() {
    local dir="${1:-.}" avoid="${2:-}"
    local absdir=$(readlink -e "$dir")
    locate -d "$_SHENV_DB_PATH" "$absdir"'*/bin/activate_this.py' \
        | sed -e 's#/bin/activate_this\.py$##' -e "s#^$absdir#$dir#"
}

# Run `lsenv` via both `find` and `locate` in parallel and return
# as soon as the first method yields the results.
function _lsenv_locate_vs_find_race() {
    set +m
    local p_pid_find=$(mkftemp) p_pid_locate=$(mkftemp) p_pid_timer=$(mkftemp)
    { __find_and_return "$@" & echo $! >"$p_pid_find"; } 2>/dev/null
    { __locate_and_return "$@" & echo $! >"$p_pid_locate"; } 2>/dev/null
    { __find_fast_bailout & echo $! >"$p_pid_timer"; } 2>/dev/null
    wait
    rm "$p_pid_find" "$p_pid_locate" "$p_pid_timer"
    set -m
}
function __find_and_return() {
    local result=$(_lsenv_find "$@")
    local pid_locate pid_timer
    read pid_locate <"$p_pid_locate"
    read pid_timer <"$p_pid_timer"
    __kill $pid_locate $pid_timer
    [ "$result" ] && echo "$result"
}
function __locate_and_return() {
    local result=$(_lsenv_locate "$@")
    local pid_find pid_timer
    read pid_find <"$p_pid_find"
    read pid_timer <"$p_pid_timer"
    __kill $pid_find $pid_timer
    [ "$result" ] && echo "$result"
}
function __find_fast_bailout() {
    sleep "$_SHENV_FIND_LIMIT"
    local pid_find
    read pid_find <"$p_pid_find"
    __kill $pid_find
}
function __kill() {
    while [ "$#" -gt 0 ]; do
        _kill_proc_tree "$1" &>/dev/null
        shift
    done
}

# Kills a complete process tree rooted at `pid`.
# Usage: _kill_proc_tree pid
function _kill_proc_tree() {
    local pid ppid="$1"
    for pid in $(ps hopid --ppid "$ppid"); do
        _kill_proc_tree "$pid"
        kill -TERM "$pid"
    done
}

# Make fastest temporary file: like mktemp, but tries
{'fraction_non_alphanumeric': 0.15555291884195538, 'fraction_numerical': 0.007831039392501186, 'mean_word_length': 3.1789786812097174, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '447209', 'n_tokens_mistral': 3220, 'n_tokens_neox': 3101, 'n_words': 768}