-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
venv_sync
executable file
·77 lines (70 loc) · 2.34 KB
/
venv_sync
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
#
# Python venv set-up script for Drake, which uses
# https://github.com/jazzband/pip-tools
# under the hood for setup.
#
# Users must NOT run this manually.
set -eux -o pipefail
# Process command line arguments.
python=
repository=
while [ "${1:-}" != "" ]; do
case "$1" in
--python)
# The python interpreter to use.
readonly python="$2"
shift
;;
--repository)
# The bazel repository rule root to interface with. We'll use the
# requirements.txt found there, and write back the `bin` symlink.
readonly repository="$2"
shift
;;
*)
echo 'Invalid command line argument' >&2
exit 5
esac
shift
done
if [[ -z "${python}" ]]; then
echo "error: --python is required"
exit 1
fi
if [[ -z "${repository}" ]]; then
echo "error: --repository is required"
exit 1
fi
# Place the venv(s) in a sibling directory to the output base. That should be a
# suitable disk location for build artifacts, but without polluting the actual
# output base that Bazel owns.
bazel_output_base=$(cd "${repository}/../.." && pwd)
venv_base="${bazel_output_base}.venv"
mkdir -p "${venv_base}"
# Install pip-tools into a virtual environment. As suggested by the pip-tools
# docs, we segregate the pip-tools apps from the environment they are managing,
# so that changes to the managed environment cannot break pip-tools.
readonly venv_jazzband="${venv_base}/venv.jazzband"
if [ ! -d "${venv_jazzband}" ]; then
"${python}" -m venv "${venv_jazzband}"
fi
# TODO(jeremy.nimmer) Ideally, we also would pin all of the dependencies of
# pip-tools here, but it's not obvious to me how to do that in a way which is
# easy to upgrade/re-pin over time.
"${venv_jazzband}/bin/pip" install -U pip-tools==7.4.1
# Prepare the venv that will hold Drake's requirements.
readonly venv_drake="${venv_base}/venv.drake"
if [ ! -d "${venv_drake}" ]; then
"${python}" -m venv "${venv_drake}"
fi
# Any environment managed by pip-tools requires a sufficiently new version of
# pip as compared to the Ubuntu 22.04 default.
"${venv_drake}/bin/pip" install -U pip==24.2
# Run the pip-sync tool.
"${venv_jazzband}/bin/pip-sync" \
--verbose \
--python-executable="${venv_drake}/bin/python3" \
"${repository}/requirements.txt"
# Symlink our venv bin path for the repository.bzl to use.
ln -nsf "${venv_drake}/bin" "${repository}/bin"