[autotest] Import new python_venv script
Grab new python_venv script from infra_virtualenv
BUG=None
TEST=presubmit hook
Change-Id: I6975fe7246d2e56e01f32bf53040ef09ebf2aa72
Reviewed-on: https://chromium-review.googlesource.com/658494
Commit-Ready: Allen Li <ayatane@chromium.org>
Tested-by: Allen Li <ayatane@chromium.org>
Reviewed-by: Prathmesh Prabhu <pprabhu@chromium.org>
diff --git a/bin/python_venv b/bin/python_venv
index 5c1b381..3a68764 100755
--- a/bin/python_venv
+++ b/bin/python_venv
@@ -7,8 +7,21 @@
#
# This script will set up a virtualenv when it has not been created yet and
# executes the Python interpreter.
+#
+# The canonical version of this script is in infra_virtualenv repository.
+# See infra_virtualenv/README.md about how to adopt virtualenv to your project.
+
set -eu
+# Change this constant to the path(s) to infra_virtualenv directory when you
+# copy this script to other repos.
+# A path can be a relative path from this script, or an absolute path. If this
+# array contains multiple paths, they are searched in the listed order.
+readonly -a infra_virtualenv_paths=(
+ "../../../../../infra_virtualenv"
+ "/opt/infra_virtualenv"
+)
+
readonly bin_dir="$(readlink -e -- "$(dirname -- "$0")")"
if [[ ! -d "${bin_dir}" ]]; then
echo "ERROR: Can not locate the location of python_env!" >&2
@@ -17,41 +30,45 @@
realpath() {
pushd "${bin_dir}" > /dev/null 2>&1
- readlink -f -- "$1"
+ readlink -e -- "$1"
popd > /dev/null 2>&1
}
-err_infra_virtualenv() {
- echo "ERROR: infra_virtualenv directory cannot be located." >&2
- echo "You need to update a constant inside python_venv, or your " >&2
- echo "checkout might be incomplete." >&2
- exit 1
+find_create_venv() {
+ local p
+ for p in "${infra_virtualenv_paths[@]}"; do
+ local create_venv=$(realpath "${p}/bin/create_venv")
+ if [[ -f "${create_venv}" ]]; then
+ echo "${create_venv}"
+ break
+ fi
+ done
}
-if [[ -d $(realpath ../../../../../infra_virtualenv) ]]; then
- readonly infra_virtualenv_path=$(realpath ../../../../../infra_virtualenv)
-elif [[ -d /opt/infra_virtualenv ]]; then
- readonly infra_virtualenv_path=/opt/infra_virtualenv
-else
- err_infra_virtualenv
-fi
-
-readonly create_venv="${infra_virtualenv_path}/bin/create_venv"
+readonly create_venv=$(find_create_venv)
if [[ ! -f "${create_venv}" ]]; then
- err_infra_virtualenv
+ cat <<EOF >&2
+ERROR: create_venv script could not be located.
+You need to update a constant inside python_venv, or your checkout might be
+incomplete.
+EOF
+ exit 1
fi
readonly extra_imports_dir=$(realpath ../venv)
if [[ ! -d "${extra_imports_dir}" ]]; then
- echo "ERROR: venv directory is not found at ${bin_dir}/.." >&2
- echo "See infra_virtualenv/README.md for details." >&2
+ cat <<EOF >&2
+ERROR: ${bin_dir}/../venv does not exist
+See infra_virtualenv/README.md for details.
+EOF
exit 1
fi
readonly venv_dir=$("${create_venv}" "${extra_imports_dir}/requirements.txt")
if [[ ! -d "${venv_dir}" ]]; then
- echo "ERROR: Failed to set up virtualenv" >&2
+ echo "ERROR: Failed to set up a virtualenv." >&2
exit 1
fi
+
export PYTHONPATH="${extra_imports_dir}"
exec "${venv_dir}/bin/python" "$@"