Skip to content

Commit dd7bbaf

Browse files
committed
fix various venv issues
1 parent 9fa8ff5 commit dd7bbaf

1 file changed

Lines changed: 37 additions & 29 deletions

File tree

share/brewkit/python-venv.sh

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#!/bin/sh
22

3-
set -e
3+
#---
4+
# dependencies:
5+
# gnu.org/coreutils: '*'
6+
#---
7+
8+
#TODO no need to make a sub-dir, just make what we got the v-env
9+
10+
set -ex
411

512
CMD_NAME=$(basename "$1")
613
PREFIX="$(dirname "$(dirname "$1")")"
@@ -10,41 +17,42 @@ PYTHON_VERSION=$(python --version | cut -d' ' -f2)
1017
PYTHON_VERSION_MAJ=$(echo $PYTHON_VERSION | cut -d. -f1)
1118
PYTHON_VERSION_MIN=$(echo $PYTHON_VERSION | cut -d. -f2)
1219

13-
python -m venv $PREFIX/libexec
20+
python -m venv "$PREFIX"
1421

15-
cd "$PREFIX"
22+
cd "$PREFIX"/bin
1623

17-
libexec/bin/pip install -v --no-binary :all: --ignore-installed $CMD_NAME
18-
mkdir bin
24+
./pip install $CMD_NAME
1925

20-
cat <<EOF >bin/$CMD_NAME
21-
#!/usr/bin/env bash
22-
self="\${BASH_SOURCE[0]}"
23-
LIBEXEC="\$(cd "\$(dirname "\$self")"/../libexec/bin && pwd)"
24-
source "\$LIBEXEC/activate"
25-
exec "\$LIBEXEC"/$CMD_NAME "\$@"
26-
EOF
27-
chmod +x bin/$CMD_NAME
26+
for x in *; do
27+
if test $x != $CMD_NAME -a $x != python; then
28+
rm $x
29+
fi
30+
done
31+
32+
mkdir ../libexec
33+
mv $CMD_NAME ../libexec/${CMD_NAME}
2834

29-
cd libexec/bin
30-
fix-shebangs.ts *
35+
# python virtual-envs are not relocatable
36+
# our only working choice is to rewrite these files and symlinks every time
37+
# because we promise that tea is relocatable *at any time*
3138

32-
rm Activate.ps1 activate.csh activate.fish
39+
cat <<EOF > $CMD_NAME
40+
#!/bin/sh
41+
42+
export VIRTUAL_ENV="\$(cd "\$(dirname "\$0")"/.. && pwd)"
43+
44+
cat <<EOSH > \$VIRTUAL_ENV/pyvenv.cfg
45+
home = \$TEA_PREFIX/python.org/v$PYTHON_VERSION_MAJ/bin
46+
include-system-site-packages = false
47+
executable = \$TEA_PREFIX/python.org/v$PYTHON_VERSION_MAJ/bin/python
48+
EOSH
3349
34-
sed -i.bak 's|VIRTUAL_ENV=".*"|VIRTUAL_ENV="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. \&\& pwd)"|' activate
35-
rm activate.bak
50+
sed -i.bak "1s|.*|#!\$VIRTUAL_ENV/bin/python|" "\$VIRTUAL_ENV"/libexec/$CMD_NAME
3651
37-
# FIXME a lot: this "updates" the `venv` on each run for relocatability
38-
cat <<EOF >>activate
52+
ln -sf "\$TEA_PREFIX"/python.org/v$PYTHON_VERSION_MAJ/bin/python "\$VIRTUAL_ENV"/bin/python
53+
54+
exec "\$VIRTUAL_ENV"/libexec/$CMD_NAME "\$@"
3955
40-
sed -i.bak \\
41-
-e "s|$TEA_PREFIX/python.org/v$PYTHON_VERSION|\$TEA_PREFIX/python.org/v$PYTHON_VERSION_MAJ|" \\
42-
-e 's|bin/python$PYTHON_VERSION_MAJ\\.$PYTHON_VERSION_MIN|bin/python|' \\
43-
-e "s|$PREFIX/libexec|\$TEA_PREFIX/$PROJECT_NAME/$VERSION/libexec|" \\
44-
\$VIRTUAL_ENV/pyvenv.cfg
45-
rm \$VIRTUAL_ENV/pyvenv.cfg.bak
4656
EOF
4757

48-
for x in python*; do
49-
ln -sf ../../../../python.org/v$PYTHON_VERSION_MAJ/bin/$x $x
50-
done
58+
chmod +x $CMD_NAME

0 commit comments

Comments
 (0)