xref: /aosp_15_r20/external/pigweed/pw_build/python_dist/setup.sh (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1#!/bin/bash
2
3# Copyright 2021 The Pigweed Authors
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# the License at
8#
9#     https://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16
17set -o xtrace -o errexit -o nounset
18
19SRC="${BASH_SOURCE[0]}"
20DIR="$(python3 -c "import os; print(os.path.dirname(os.path.abspath(os.path.realpath(\"$SRC\"))))")"
21VENV="${DIR}/python-venv"
22PY_TO_TEST="python3"
23CONSTRAINTS_PATH="${DIR}/constraints.txt"
24EXTRA_REQUIREMENT_PATH="${DIR}/python_wheels/requirements.txt"
25
26if [ ! -z "${1-}" ]; then
27  VENV="${1-}"
28  PY_TO_TEST="${VENV}/bin/python"
29fi
30
31CONSTRAINTS_ARG=""
32if [ -f "${CONSTRAINTS_PATH}" ]; then
33    CONSTRAINTS_ARG="-c ""${CONSTRAINTS_PATH}"
34fi
35
36EXTRA_REQUIREMENT_ARG=""
37if [ -f "${EXTRA_REQUIREMENT_PATH}" ]; then
38    EXTRA_REQUIREMENT_ARG="-r ""${EXTRA_REQUIREMENT_PATH}"
39fi
40
41PY_MAJOR_VERSION=$(${PY_TO_TEST} -c "import sys; print(sys.version_info[0])")
42PY_MINOR_VERSION=$(${PY_TO_TEST} -c "import sys; print(sys.version_info[1])")
43
44if [ ${PY_MAJOR_VERSION} -ne 3 ] || [ ${PY_MINOR_VERSION} -lt 10 ]
45then
46    echo "ERROR: This Python distributable requires Python 3.10 or newer."
47    exit 1
48fi
49
50if [ ! -d "${VENV}" ]
51then
52    ${PY_TO_TEST} -m venv "${VENV}"
53fi
54
55# Note: pip install --require-hashes will be triggered if any hashes are present
56# in the requirement.txt file.
57"${VENV}/bin/python" -m pip install \
58  --find-links="${DIR}/python_wheels" \
59  -r ${DIR}/requirements.txt ${EXTRA_REQUIREMENT_ARG} ${CONSTRAINTS_ARG}
60
61exit 0
62