huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
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
#!/bin/bash
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Starts a python interpreter in virtualenv.
#
# 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
    exit 1
fi
 
realpath() {
    pushd "${bin_dir}" > /dev/null 2>&1
    readlink -e -- "$1"
    popd > /dev/null 2>&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
}
 
readonly create_venv=$(find_create_venv)
if [[ ! -f "${create_venv}" ]]; then
    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
    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 a virtualenv." >&2
    exit 1
fi
 
export PYTHONPATH="${extra_imports_dir}"
exec "${venv_dir}/bin/python" "$@"