function use_pip-tools() {
    requirements_file=${1:?"a requirements file must be provided as the first argument"}
    shift
    requirements_dir=$(dirname $requirements_file)

    local has_pip=0
    if has pip; then
        if [[ -n $(which pip) ]]; then
            has_pip=1
        fi
    fi
    if [ $has_pip -eq 0 ]; then
        echo "[use pip-tools] No pip installed via layout; try layout pyenv or layout python"
        return 1
    fi

    if ! test -f $requirements_file; then
        echo "[use pip-tools] No requirements file $requirements_file"
        return 1
    fi

    if ! has pip-compile; then
        echo "[use pip-tools] pip-tools missing; installing"
        pip install pip-tools
    fi

    deps=$(python docker/direnvrc_helpers.py $requirements_file)
    ret_val=$?

    LAST_PYTHON_VERSION=$(head -n 1 .last_python_version)
    LAST_PYTHON_VERSION=${LAST_PYTHON_VERSION:-0.0.0}

    echo "[use pip-tools] Python wanted: ${PYTHON_VERSION} , Python last: ${LAST_PYTHON_VERSION}"
    requirements_txt=$(echo "$requirements_file" | cut -f 1 -d '.').txt
    if [ $ret_val -eq 1 ] || [[ "$PYTHON_VERSION" != "$LAST_PYTHON_VERSION" ]]; then
        find $requirements_dir -name '*.txt' -type f -delete
        echo "[use pip-tools] resyncing main requirements ($requirements_file)"
        pip-compile "$@" $requirements_file -o $requirements_txt
        echo "[use pip-tools] resyncing dependencies ($deps)"
        for other_requirements_file in $(grep -v $requirements_file <<< $(tr ' ' '\n' <<< $deps)); do
            echo "[use pip-tools] resyncing dependency ($other_requirements_file)"
            other_requirements_txt=$(echo "$other_requirements_file" | cut -f 1 -d '.').txt
            pip-compile "$@" $other_requirements_file -o $other_requirements_txt
        done
        echo $PYTHON_VERSION > .last_python_version
    fi
    echo "[use pip-tools] Resyncing $requirements_txt"
    pip-sync $requirements_txt

    echo "[use pip-tools] monitoring $deps"
    watch_file $deps
}

# use a certain pyenv version
use_python() {
    if [ -n "$(which pyenv)" ]; then
        local pyversion=$1
        echo "[use python] pyenv ${pyversion}"
        pyenv local ${pyversion}
    fi
}

layout_virtualenv() {
    local pyversion=$1
    local pvenv=$2
    if [ -n "$(which pyenv virtualenv)" ]; then
        echo "[layout virtualenv] pyenv virtual env $pvenv ${pyversion}"
        pyenv virtualenv --force --quiet ${pyversion} ${pvenv}-${pyversion}
    fi
    pyenv local --unset
}

layout_activate() {
    if [ -n "$(which pyenv)" ]; then
        echo "[layout activate] pyenv $1"
        source $(pyenv root)/versions/$1/bin/activate
    fi
}
