1+ #! /bin/bash -ex
2+ # vim: ts=4 sw=4 expandtab
3+ #
4+ # Script: Python Virtual Environment and Package Installer
5+
6+ create_venv_dir () {
7+ local venv_dir
8+ venv_dir=$( mktemp -td venv.XXXXXXXXXX)
9+ trap " rm -rf ${venv_dir} " EXIT
10+ echo " ${venv_dir} "
11+ }
12+
13+ create_virtualenv () {
14+ local path=$1
15+ if [ -d $path ]; then
16+ echo " Will reuse existing virtual env: $path "
17+ else
18+ if command -v python3 > /dev/null; then
19+ python3 -m venv $path
20+ else
21+ virtualenv -p python $path
22+ fi
23+ fi
24+ }
25+
26+ pip_download () {
27+ local venv=$1
28+ shift
29+ local package=$1
30+ shift
31+ local options=$@
32+ if ! $venv /pip download $options --dest=" $PIP_SDIST_INDEX " $package ; then
33+ # pip <8.0.0 does not have "download" command
34+ $venv /pip install $options \
35+ --upgrade --exists-action=i --cache-dir=" $PIP_SDIST_INDEX " \
36+ $package
37+ fi
38+ }
39+
40+
41+ install_python_packages () {
42+ local venv_dir=$1
43+ shift
44+ local venv=" $venv_dir /bin"
45+ # Use this function to create a virtualenv and install
46+ # python packages. Pass a list of package names.
47+ #
48+ # Usage (with pip 24.0 [the default]):
49+ #
50+ # to_install=( "ansible" "chacractl>=0.0.21" )
51+ # install_python_packages $TEMPVENV "to_install[@]"
52+ #
53+ # Usage (with pip<X.X.X [can also do ==X.X.X or !=X.X.X]):
54+ #
55+ # to_install=( "ansible" "chacractl>=0.0.21" )
56+ # install_python_packages_no_binary $TEMPVENV "to_install[@]" "pip<X.X.X"
57+ #
58+ # Usage (with latest pip):
59+ #
60+ # to_install=( "ansible" "chacractl>=0.0.21" )
61+ # install_python_packages $TEMPVENV "to_install[@]" latest
62+
63+ create_virtualenv $venv_dir
64+
65+ # Define and ensure the PIP cache
66+ PIP_SDIST_INDEX=" $HOME /.cache/pip"
67+ mkdir -p $PIP_SDIST_INDEX
68+
69+ # Avoid UnicodeErrors when installing packages.
70+ # See https://github.com/ceph/ceph/pull/42811
71+ export LC_ALL=en_US.UTF-8
72+
73+ if [ " $2 " == " latest" ]; then
74+ echo " Ensuring latest pip is installed"
75+ $venv /pip install -U pip
76+ elif [[ -n $2 && " $2 " != " latest" ]]; then
77+ echo " Installing $2 "
78+ $venv /pip install " $2 "
79+ else
80+ # This is the default for most jobs.
81+ # See ff01d2c5 and fea10f52
82+ echo " Installing pip 24.0"
83+ $venv /pip install " pip==24.0"
84+ fi
85+
86+ echo " Ensuring latest wheel is installed"
87+ $venv /pip install -U wheel
88+
89+ echo " Updating setuptools"
90+ pip_download $venv setuptools
91+
92+ pkgs=(" ${! 1} " )
93+ for package in ${pkgs[@]} ; do
94+ echo $package
95+ # download packages to the local pip cache
96+ pip_download $venv $package
97+ # install packages from the local pip cache, ignoring pypi
98+ $venv /pip install --upgrade --exists-action=i --find-links=" file://$PIP_SDIST_INDEX " --no-index $package
99+ done
100+
101+ # See https://tracker.ceph.com/issues/59652
102+ echo " Pinning urllib3 and requests"
103+ $venv /pip install " urllib3<2.0.0"
104+ $venv /pip install " requests<2.30.0"
105+ }
0 commit comments