Skip to content

Instantly share code, notes, and snippets.

@mccutchen
Last active January 30, 2025 14:48
Show Gist options
  • Select an option

  • Save mccutchen/e9c0ba406d4b89147b97c2329d65d740 to your computer and use it in GitHub Desktop.

Select an option

Save mccutchen/e9c0ba406d4b89147b97c2329d65d740 to your computer and use it in GitHub Desktop.

Revisions

  1. mccutchen revised this gist Jan 30, 2025. 1 changed file with 15 additions and 10 deletions.
    25 changes: 15 additions & 10 deletions .envrc
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,30 @@
    #!/bin/bash
    set -euo pipefail

    VENV_DIR=".venv"

    REQUIREMENTS_HASH_FILE="$VENV_DIR/requirements.shasum"
    VIRTUAL_ENV=".venv"
    REQUIREMENTS_HASH_FILE="$VIRTUAL_ENV/requirements.shasum"
    REQUIREMENTS_HASH_FUNC="shasum"
    REQUIREMENTS_HASH_VAL=$($REQUIREMENTS_HASH_FUNC requirements.txt | cut -d ' ' -f 1)

    function reset_venv() {
    rm -rf "$VENV_DIR"
    python3 -m venv "$VENV_DIR"
    "$VENV_DIR/bin/pip" install -r "requirements.txt"
    echo -n "$REQUIREMENTS_HASH_VAL" > "$REQUIREMENTS_HASH_FILE"
    if ! has uv; then
    echo 'error: `uv` command not found. install with `brew install uv`'
    exit 1
    fi

    rm -rf "$VIRTUAL_ENV"
    uv venv
    uv pip install -r requirements.txt
    printf '%s' "$REQUIREMENTS_HASH_VAL" >"$REQUIREMENTS_HASH_FILE"
    }

    if [ ! -f "$REQUIREMENTS_HASH_FILE" ]; then
    echo "setting up new python virtualenv ..."
    echo "setting up new python virtualenv in $VIRTUAL_ENV ..."
    reset_venv
    elif [ "$(cat $REQUIREMENTS_HASH_FILE)" != "$REQUIREMENTS_HASH_VAL" ]; then
    echo "python requirements changed, rebuilding virtualenv ..."
    echo "python requirements changed, rebuilding virtualenv in $VIRTUAL_ENV ..."
    echo "(found hash $(cat $REQUIREMENTS_HASH_FILE), want hash $REQUIREMENTS_HASH_VAL)"
    reset_venv
    fi

    PATH_add "$VENV_DIR/bin"
    PATH_add "$VIRTUAL_ENV/bin"
  2. mccutchen created this gist Nov 3, 2022.
    25 changes: 25 additions & 0 deletions .envrc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #!/bin/bash

    VENV_DIR=".venv"

    REQUIREMENTS_HASH_FILE="$VENV_DIR/requirements.shasum"
    REQUIREMENTS_HASH_FUNC="shasum"
    REQUIREMENTS_HASH_VAL=$($REQUIREMENTS_HASH_FUNC requirements.txt | cut -d ' ' -f 1)

    function reset_venv() {
    rm -rf "$VENV_DIR"
    python3 -m venv "$VENV_DIR"
    "$VENV_DIR/bin/pip" install -r "requirements.txt"
    echo -n "$REQUIREMENTS_HASH_VAL" > "$REQUIREMENTS_HASH_FILE"
    }

    if [ ! -f "$REQUIREMENTS_HASH_FILE" ]; then
    echo "setting up new python virtualenv ..."
    reset_venv
    elif [ "$(cat $REQUIREMENTS_HASH_FILE)" != "$REQUIREMENTS_HASH_VAL" ]; then
    echo "python requirements changed, rebuilding virtualenv ..."
    echo "(found hash $(cat $REQUIREMENTS_HASH_FILE), want hash $REQUIREMENTS_HASH_VAL)"
    reset_venv
    fi

    PATH_add "$VENV_DIR/bin"