layout_poetry

This is an opinionated implementation for supporting Poetry in direnv, based on direnv#995

direnv currently does not support Poetry.
See also tracking issue direnv-stdlib#1

Usage

layout poetry

Notes

To use Poetry only to manage development environment dependencies of given project (e.g Ansible playbook), remove packages section from pyproject.toml

Code

# shellcheck shell=bash
# vim: ft=bash

layout_poetry () {
    if ! has poetry; then
        log_error "Poetry is not installed"
        return 1
    fi

    PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}"
    if [[ ! -f "$PYPROJECT_TOML" ]]; then
        log_error "No pyproject.toml found"
        return 1
    fi

    VIRTUAL_ENV="$(poetry env info --path 2>/dev/null || true)"
    if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV ]]; then
        log_status "Executing \`poetry install\` to set up initial environment"
        poetry install --no-root
        VIRTUAL_ENV="$(poetry env info --path)"
    fi

    PATH_add "$VIRTUAL_ENV/bin"
    export POETRY_ACTIVE=1
    export VIRTUAL_ENV
}