Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 2bfd005

Browse files
Upgrade Pants script
Done by running `curl -L -o ./pants https://pantsbuild.github.io/setup/pants`, per https://www.pantsbuild.org/docs/upgrade-tips#check-for-updates-to-the-pants-script Signed-off-by: Christopher Maier <[email protected]>
1 parent f047f88 commit 2bfd005

File tree

1 file changed

+58
-31
lines changed

1 file changed

+58
-31
lines changed

pants

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
set -eou pipefail
1414

15-
# NOTE: To use an unreleased version of Pants from the pantsbuild/pants master branch,
16-
# locate the master branch SHA, set PANTS_SHA=<SHA> in the environment, and run this script as usual.
15+
# NOTE: To use an unreleased version of Pants from the pantsbuild/pants main branch,
16+
# locate the main branch SHA, set PANTS_SHA=<SHA> in the environment, and run this script as usual.
1717
#
1818
# E.g., PANTS_SHA=725fdaf504237190f6787dda3d72c39010a4c574 ./pants --version
1919

@@ -34,9 +34,9 @@ fi
3434

3535
PANTS_BOOTSTRAP="${PANTS_SETUP_CACHE}/bootstrap-$(uname -s)-$(uname -m)"
3636

37-
PEX_VERSION=2.1.42
38-
PEX_URL="https://github.com/pantsbuild/pex/releases/download/v${PEX_VERSION}/pex"
39-
PEX_EXPECTED_SHA256="69d6b1b1009b00dd14a3a9f19b72cff818a713ca44b3186c9b12074b2a31e51f"
37+
_PEX_VERSION=2.1.42
38+
_PEX_URL="https://github.com/pantsbuild/pex/releases/download/v${_PEX_VERSION}/pex"
39+
_PEX_EXPECTED_SHA256="69d6b1b1009b00dd14a3a9f19b72cff818a713ca44b3186c9b12074b2a31e51f"
4040

4141
VIRTUALENV_VERSION=20.4.7
4242
VIRTUALENV_REQUIREMENTS=$(
@@ -55,6 +55,7 @@ EOF
5555

5656
COLOR_RED="\x1b[31m"
5757
COLOR_GREEN="\x1b[32m"
58+
COLOR_YELLOW="\x1b[33m"
5859
COLOR_RESET="\x1b[0m"
5960

6061
function log() {
@@ -70,6 +71,10 @@ function green() {
7071
(($# > 0)) && log "${COLOR_GREEN}$*${COLOR_RESET}"
7172
}
7273

74+
function warn() {
75+
(($# > 0)) && log "${COLOR_YELLOW}$*${COLOR_RESET}"
76+
}
77+
7378
function tempdir {
7479
mkdir -p "$1"
7580
mktemp -d "$1"/pants.XXXXXX
@@ -168,8 +173,19 @@ function set_supported_python_versions {
168173
fi
169174
}
170175

176+
function check_python_exe_compatible_version {
177+
local python_exe="$1"
178+
local major_minor_version
179+
major_minor_version="$(get_python_major_minor_version "${python_exe}")"
180+
for valid_version in "${supported_python_versions_int[@]}"; do
181+
if [[ "${major_minor_version}" == "${valid_version}" ]]; then
182+
echo "${python_exe}" && return 0
183+
fi
184+
done
185+
}
186+
171187
function determine_default_python_exe {
172-
for version in "${supported_python_versions_decimal[@]}"; do
188+
for version in "${supported_python_versions_decimal[@]}" "3" ""; do
173189
local interpreter_path
174190
interpreter_path="$(command -v "python${version}")"
175191
if [[ -z "${interpreter_path}" ]]; then
@@ -179,7 +195,9 @@ function determine_default_python_exe {
179195
if [[ "$("${interpreter_path}" --version 2>&1 > /dev/null)" == "pyenv: python${version}"* ]]; then
180196
continue
181197
fi
182-
echo "${interpreter_path}" && return 0
198+
if [[ -n "$(check_python_exe_compatible_version "${interpreter_path}")" ]]; then
199+
echo "${interpreter_path}" && return 0
200+
fi
183201
done
184202
}
185203

@@ -188,25 +206,19 @@ function determine_python_exe {
188206
set_supported_python_versions "${pants_version}"
189207
local requirement_str="For \`pants_version = \"${pants_version}\"\`, Pants requires Python ${supported_message} to run."
190208

191-
local python_bin_name
209+
local python_exe
192210
if [[ "${PYTHON_BIN_NAME}" != 'unspecified' ]]; then
193-
python_bin_name="${PYTHON_BIN_NAME}"
211+
python_exe="$(get_exe_path_or_die "${PYTHON_BIN_NAME}")" || exit 1
212+
if [[ -z "$(check_python_exe_compatible_version "${python_exe}")" ]]; then
213+
die "Invalid Python interpreter version for ${python_exe}. ${requirement_str}"
214+
fi
194215
else
195-
python_bin_name="$(determine_default_python_exe)"
196-
if [[ -z "${python_bin_name}" ]]; then
216+
python_exe="$(determine_default_python_exe)"
217+
if [[ -z "${python_exe}" ]]; then
197218
die "No valid Python interpreter found. ${requirement_str} Please check that a valid interpreter is installed and on your \$PATH."
198219
fi
199220
fi
200-
local python_exe
201-
python_exe="$(get_exe_path_or_die "${python_bin_name}")" || exit 1
202-
local major_minor_version
203-
major_minor_version="$(get_python_major_minor_version "${python_exe}")"
204-
for valid_version in "${supported_python_versions_int[@]}"; do
205-
if [[ "${major_minor_version}" == "${valid_version}" ]]; then
206-
echo "${python_exe}" && return 0
207-
fi
208-
done
209-
die "Invalid Python interpreter version for ${python_exe}. ${requirement_str}"
221+
echo "${python_exe}"
210222
}
211223

212224
function compute_sha256 {
@@ -229,20 +241,20 @@ EOF
229241

230242
function bootstrap_pex {
231243
local python="$1"
232-
local bootstrapped="${PANTS_BOOTSTRAP}/pex-${PEX_VERSION}/pex"
244+
local bootstrapped="${PANTS_BOOTSTRAP}/pex-${_PEX_VERSION}/pex"
233245
if [[ ! -f "${bootstrapped}" ]]; then
234246
(
235247
green "Downloading the Pex PEX."
236248
mkdir -p "${PANTS_BOOTSTRAP}"
237249
local staging_dir
238250
staging_dir=$(tempdir "${PANTS_BOOTSTRAP}")
239251
cd "${staging_dir}"
240-
curl -LO "${PEX_URL}"
252+
curl -LO "${_PEX_URL}"
241253
fingerprint="$(compute_sha256 "${python}" "pex")"
242-
if [[ "${PEX_EXPECTED_SHA256}" != "${fingerprint}" ]]; then
243-
die "SHA256 of ${PEX_URL} is not as expected. Aborting."
254+
if [[ "${_PEX_EXPECTED_SHA256}" != "${fingerprint}" ]]; then
255+
die "SHA256 of ${_PEX_URL} is not as expected. Aborting."
244256
fi
245-
green "SHA256 fingerprint of ${PEX_URL} verified."
257+
green "SHA256 fingerprint of ${_PEX_URL} verified."
246258
mkdir -p "$(dirname "${bootstrapped}")"
247259
mv -f "${staging_dir}/pex" "${bootstrapped}"
248260
rmdir "${staging_dir}"
@@ -251,6 +263,15 @@ function bootstrap_pex {
251263
echo "${bootstrapped}"
252264
}
253265

266+
function scrub_PEX_env_vars {
267+
# Ensure the virtualenv PEX runs as shrink-wrapped.
268+
# See: https://github.com/pantsbuild/setup/issues/105
269+
if [[ -n "${!PEX_@}" ]]; then
270+
warn "Scrubbing ${!PEX_@}"
271+
unset "${!PEX_@}"
272+
fi
273+
}
274+
254275
function bootstrap_virtualenv {
255276
local python="$1"
256277
local bootstrapped="${PANTS_BOOTSTRAP}/virtualenv-${VIRTUALENV_VERSION}/virtualenv.pex"
@@ -263,7 +284,10 @@ function bootstrap_virtualenv {
263284
staging_dir=$(tempdir "${PANTS_BOOTSTRAP}")
264285
cd "${staging_dir}"
265286
echo "${VIRTUALENV_REQUIREMENTS}" > requirements.txt
266-
"${python}" "${pex_path}" -r requirements.txt -c virtualenv -o virtualenv.pex
287+
(
288+
scrub_PEX_env_vars
289+
"${python}" "${pex_path}" -r requirements.txt -c virtualenv -o virtualenv.pex
290+
)
267291
mkdir -p "$(dirname "${bootstrapped}")"
268292
mv -f "${staging_dir}/virtualenv.pex" "${bootstrapped}"
269293
rm -rf "${staging_dir}"
@@ -315,10 +339,13 @@ function bootstrap_pants {
315339
local virtualenv_path
316340
virtualenv_path="$(bootstrap_virtualenv "${python}")" || exit 1
317341
green "Installing ${pants_requirement} into a virtual environment at ${bootstrapped}"
318-
# shellcheck disable=SC2086
319-
"${python}" "${virtualenv_path}" --no-download "${staging_dir}/install" && \
320-
"${staging_dir}/install/bin/pip" install -U pip && \
321-
"${staging_dir}/install/bin/pip" install ${maybe_find_links} --progress-bar off "${pants_requirement}" && \
342+
(
343+
scrub_PEX_env_vars
344+
# shellcheck disable=SC2086
345+
"${python}" "${virtualenv_path}" --no-download "${staging_dir}/install" && \
346+
"${staging_dir}/install/bin/pip" install -U pip && \
347+
"${staging_dir}/install/bin/pip" install ${maybe_find_links} --progress-bar off "${pants_requirement}"
348+
) && \
322349
ln -s "${staging_dir}/install" "${staging_dir}/${target_folder_name}" && \
323350
mv "${staging_dir}/${target_folder_name}" "${bootstrapped}" && \
324351
green "New virtual environment successfully created at ${bootstrapped}."

0 commit comments

Comments
 (0)