forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pants_venv
executable file
·104 lines (86 loc) · 3.24 KB
/
pants_venv
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env bash
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd -P)
# shellcheck source=build-support/common.sh
source "${REPO_ROOT}/build-support/common.sh"
REQUIREMENTS=(
"${REPO_ROOT}/3rdparty/python/requirements.txt"
)
platform=$(uname -mps)
function venv_dir() {
# Include the entire version string in order to differentiate e.g. PyPy from CPython.
# Fingerprinting uname and python output avoids shebang length limits and any odd chars.
venv_fingerprint=$( (
echo "${platform}"
${PY} --version
) | fingerprint_data)
# NB: We house these outside the working copy to avoid needing to gitignore them, but also to
# dodge https://github.com/hashicorp/vagrant/issues/12057.
echo "${HOME}/.cache/pants/pants_dev_deps/${venv_fingerprint}.venv"
}
function activate_venv() {
# Because the venv/bin/activate script's location is dynamic and not located in a fixed
# place, Shellcheck will not be able to find it so we tell Shellcheck to ignore the file.
# shellcheck source=/dev/null
source "$(venv_dir)/bin/activate"
}
function create_venv() {
rm -rf "$(venv_dir)"
"${PY}" -m venv "$(venv_dir)" && "$(venv_dir)/bin/pip" install wheel
}
function ensure_gcc() {
if ! GCC_VERSION="$(gcc -v 2>&1)"; then
die "$(
cat << MESSAGE
ERROR: unable to execute 'gcc'. Please verify that your compiler is installed, in your
\$PATH and functional.
Hint: on macOS, you may need to accept the XCode EULA: 'sudo xcodebuild -license accept'.
MESSAGE
)"
fi
# Prevent bootstrapping failure due to unrecognized flag:
# https://github.com/pantsbuild/pants/issues/78
if [[ "$GCC_VERSION" == *503.0.38* ]]; then
# Required for clang version 503.0.38
export ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future
fi
}
function activate_pants_venv() {
# Allow for multiple calls to `activate_pants_venv`.
if [[ -n "${VENV_ACTIVATED_FOR_PANTS:-}" ]]; then
return
else
export VENV_ACTIVATED_FOR_PANTS="true"
fi
fingerprint=""
for req in "${REQUIREMENTS[@]}"; do
fingerprint="${fingerprint}$(fingerprint_data < "${req}")"
done
fingerprint=$(echo "${fingerprint}" | fingerprint_data)
BOOTSTRAPPED_FILE="$(venv_dir)/BOOTSTRAPPED.${fingerprint}"
if ! [ -f "${BOOTSTRAPPED_FILE}" ]; then
log "Bootstrapping pants_deps with requirements:"
# Use -f ${REPO_ROOT}/third_party if patching in local dependencies like so:
#
# pip_extra=(-f "${REPO_ROOT}/third_party")
#
pip_extra=()
for req in "${REQUIREMENTS[@]}"; do
log " ${req}"
done
create_venv || die "Failed to create venv."
activate_venv || die "Failed to activate venv."
# Needed for compilation of native python distributions.
ensure_gcc
for req in "${REQUIREMENTS[@]}"; do
# NB: For the pip_extra reference, see:
# https://stackoverflow.com/questions/7577052/bash-empty-array-expansion-with-set-u
pip install "${pip_extra[@]+"${pip_extra[@]}"}" -r "${req}" ||
die "Failed to install requirements from ${req}."
done
touch "${BOOTSTRAPPED_FILE}"
else
activate_venv || die "Failed to activate venv."
fi
}