Debian Codegraph @

scripts/activate.sh

raw
1 #! /usr/bin/env bash
2
3 # Make sure this is sourced.
4 if [[ "$0" == "${BASH_SOURCE[0]}" ]]; then
5 echo This must be sourced.
6 exit 1
7 fi
8
9 if [[ -n "$ZSH_VERSION" ]]; then
10 export PYAV_ROOT="$(realpath -- "$(dirname -- "$(readlink -f -- "$0")")/..")"
11 else
12 export PYAV_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.."; pwd)"
13 fi
14
15 if [[ ! "$PYAV_LIBRARY" ]]; then
16 if [[ "$1" ]]; then
17 if [[ "$1" == ffmpeg-* ]]; then
18 PYAV_LIBRARY="$1"
19 else
20 echo "Error: PYAV_LIBRARY must start with 'ffmpeg-'" >&2
21 return 1
22 fi
23 else
24 PYAV_LIBRARY=ffmpeg-8.1.1
25 echo "No \$PYAV_LIBRARY set; defaulting to $PYAV_LIBRARY"
26 fi
27 fi
28 export PYAV_LIBRARY
29
30 if [[ "$VIRTUAL_ENV" ]]; then
31 PYAV_PYTHON="${VIRTUAL_ENV}/bin/python3"
32 echo "Using activated venv: $VIRTUAL_ENV"
33 elif [[ ! "$PYAV_PYTHON" ]]; then
34 PYAV_PYTHON="${PYAV_PYTHON-python3}"
35 echo 'No $PYAV_PYTHON set; defaulting to python3.'
36 fi
37
38 # Hack for PyPy on GitHub Actions.
39 # This is because PYAV_PYTHON is constructed from "python${{ matrix.config.python }}"
40 # resulting in "pythonpypy3", which won't work.
41 if [[ "$PYAV_PYTHON" == *pypy* ]]; then
42 PYAV_PYTHON=python
43 fi
44
45 export PYAV_PYTHON
46 export PYAV_PIP="${PYAV_PIP-$PYAV_PYTHON -m pip}"
47
48 if [[ "$GITHUB_ACTION" ]]; then
49
50 # GitHub has a very self-contained environment. Lets just work in that.
51 echo "We're on CI, so not setting up another virtualenv."
52
53 elif [[ "$VIRTUAL_ENV" ]]; then
54 # Using activated venv
55 true
56 else
57
58 export PYAV_VENV_NAME="$(uname -s).$(uname -r).$("$PYAV_PYTHON" -c '
59 import sys
60 import platform
61 print("{}{}.{}".format(platform.python_implementation().lower(), *sys.version_info[:2]))
62 ')"
63 export PYAV_VENV="$PYAV_ROOT/venvs/$PYAV_VENV_NAME"
64
65 if [[ ! -e "$PYAV_VENV/bin/python" ]]; then
66 mkdir -p "$PYAV_VENV"
67 virtualenv -p "$PYAV_PYTHON" "$PYAV_VENV"
68 "$PYAV_VENV/bin/pip" install --upgrade pip setuptools
69 fi
70
71 if [[ -e "$PYAV_VENV/bin/activate" ]]; then
72 source "$PYAV_VENV/bin/activate"
73 else
74 # Not a virtualenv (perhaps a debug Python); lets manually "activate" it.
75 PATH="$PYAV_VENV/bin:$PATH"
76 fi
77
78 fi
79
80 # Just a flag so that we know this was supposedly run.
81 export _PYAV_ACTIVATED=1
82
83 if [[ ! "$PYAV_LIBRARY_BUILD_ROOT" && -d /vagrant ]]; then
84 # On Vagrant, building the library in the shared directory causes some
85 # problems, so we move it to the user's home.
86 PYAV_LIBRARY_ROOT="/home/vagrant/vendor"
87 fi
88 export PYAV_LIBRARY_ROOT="${PYAV_LIBRARY_ROOT-$PYAV_ROOT/vendor}"
89 export PYAV_LIBRARY_BUILD="${PYAV_LIBRARY_BUILD-$PYAV_LIBRARY_ROOT/build}"
90 export PYAV_LIBRARY_PREFIX="$PYAV_LIBRARY_BUILD/$PYAV_LIBRARY"
91
92 export PATH="$PYAV_LIBRARY_PREFIX/bin:$PATH"
93 export PYTHONPATH="$PYAV_ROOT:$PYTHONPATH"
94 export PKG_CONFIG_PATH="$PYAV_LIBRARY_PREFIX/lib/pkgconfig:$PKG_CONFIG_PATH"
95 export LD_LIBRARY_PATH="$PYAV_LIBRARY_PREFIX/lib:$LD_LIBRARY_PATH"
96 export DYLD_LIBRARY_PATH="$PYAV_LIBRARY_PREFIX/lib:$DYLD_LIBRARY_PATH"