aboutsummaryrefslogtreecommitdiff
path: root/run-on-venv
blob: e6fdf7e42af3a40058083a47500033396c9bc95f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash
#
# Helper script to run a command inside a virtualenv
#
# This is too simple, and you might want to use
# virtualenvwrapper instead:
#
#   https://tracker.debian.org/pkg/virtualenvwrapper
#   https://virtualenvwrapper.readthedocs.io/en/latest/
#   https://doughellmann.com/projects/virtualenvwrapper/
#
# Or pipx:
#
#   https://pipx.pypa.io/stable/
#   https://tracker.debian.org/pkg/python-pipx
#

# Parameters
BASENAME="`basename $0`"
COMMAND="$1"
VENV="${VENV:$HOME/.virtualenvs/$COMMAND}"

# Check for a test
if [ -z "$COMMAND" ]; then
  echo "usage: $BASENAME <command> [args]"
  exit 1
fi

# Deactivate any active virtualenv
deactivate &> /dev/null

# Create and activate a virtualenv as needed
if [ ! -e "$VENV" ]; then
  mkdir -p `dirname $VENV`
  python -m venv $VENV
  . $VENV/bin/activate
  pip install --upgrade pip
  pip install $COMMAND
else
  # Check for a virtualenv
  if [ ! -e "$VENV/bin/activate" ]; then
    echo "$BASENAME: missing or invalid virtualenv"
    exit 1
  fi

  # Activate the virtualenv
  . $VENV/bin/activate
fi

# Run
$*