1212
1313set -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
3434
3535PANTS_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.62
38+ _PEX_URL =" https://github.com/pantsbuild/pex/releases/download/v${_PEX_VERSION } /pex"
39+ _PEX_EXPECTED_SHA256= " 56668b1ca147bd63141e586ffee97c7cc51ce8e6eac6c9b7a4bf1215b94396e5 "
4040
4141VIRTUALENV_VERSION=20.4.7
4242VIRTUALENV_REQUIREMENTS=$(
5555
5656COLOR_RED=" \x1b[31m"
5757COLOR_GREEN=" \x1b[32m"
58+ COLOR_YELLOW=" \x1b[33m"
5859COLOR_RESET=" \x1b[0m"
5960
6061function 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+
7378function tempdir {
7479 mkdir -p " $1 "
7580 mktemp -d " $1 " /pants.XXXXXX
@@ -82,13 +87,18 @@ function get_exe_path_or_die {
8287 fi
8388}
8489
85- function get_pants_config_value {
90+ function get_pants_config_string_value {
8691 local config_key=" $1 "
8792 local optional_space=" [[:space:]]*"
8893 local prefix=" ^${config_key}${optional_space} =${optional_space} "
8994 local raw_value
90- raw_value=" $( sed -ne " /${prefix} / s#${prefix} ##p" " ${PANTS_TOML} " ) "
91- echo " ${raw_value} " | tr -d \"\' && return 0
95+ raw_value=" $( sed -ne " /${prefix} / s|${prefix} ||p" " ${PANTS_TOML} " ) "
96+ local optional_suffix=" ${optional_space} (#.*)?$"
97+ echo " ${raw_value} " \
98+ | sed -E \
99+ -e " s|^'([^']*)'${optional_suffix} |\1|" \
100+ -e ' s|^"([^"]*)"' " ${optional_suffix} " ' $|\1|' \
101+ && return 0
92102 return 0
93103}
94104
@@ -120,7 +130,7 @@ function determine_pants_version {
120130 return
121131 fi
122132
123- pants_version=" $( get_pants_config_value ' pants_version' ) "
133+ pants_version=" $( get_pants_config_string_value ' pants_version' ) "
124134 if [[ -z " ${pants_version} " ]]; then
125135 die " Please explicitly specify the \` pants_version\` in your \` pants.toml\` under the \` [GLOBAL]\` scope.
126136See https://pypi.org/project/pantsbuild.pants/#history for all released versions
@@ -168,18 +178,31 @@ function set_supported_python_versions {
168178 fi
169179}
170180
181+ function check_python_exe_compatible_version {
182+ local python_exe=" $1 "
183+ local major_minor_version
184+ major_minor_version=" $( get_python_major_minor_version " ${python_exe} " ) "
185+ for valid_version in " ${supported_python_versions_int[@]} " ; do
186+ if [[ " ${major_minor_version} " == " ${valid_version} " ]]; then
187+ echo " ${python_exe} " && return 0
188+ fi
189+ done
190+ }
191+
171192function determine_default_python_exe {
172- for version in " ${supported_python_versions_decimal[@]} " ; do
193+ for version in " ${supported_python_versions_decimal[@]} " " 3 " " " ; do
173194 local interpreter_path
174195 interpreter_path=" $( command -v " python${version} " ) "
175196 if [[ -z " ${interpreter_path} " ]]; then
176197 continue
177198 fi
178- # Check if the Python version is installed via Pyenv but not activated .
179- if [[ " $( " $ {interpreter_path}" --version 2>&1 > /dev/null) " == " pyenv: python ${version} " * ]] ; then
199+ # Check if a version is shimmed by pyenv or asdf but not configured .
200+ if ! " ${interpreter_path} " --version > /dev/null 2>&1 ; then
180201 continue
181202 fi
182- echo " ${interpreter_path} " && return 0
203+ if [[ -n " $( check_python_exe_compatible_version " ${interpreter_path} " ) " ]]; then
204+ echo " ${interpreter_path} " && return 0
205+ fi
183206 done
184207}
185208
@@ -188,25 +211,19 @@ function determine_python_exe {
188211 set_supported_python_versions " ${pants_version} "
189212 local requirement_str=" For \` pants_version = \" ${pants_version} \"\` , Pants requires Python ${supported_message} to run."
190213
191- local python_bin_name
214+ local python_exe
192215 if [[ " ${PYTHON_BIN_NAME} " != ' unspecified' ]]; then
193- python_bin_name=" ${PYTHON_BIN_NAME} "
216+ python_exe=" $( get_exe_path_or_die " ${PYTHON_BIN_NAME} " ) " || exit 1
217+ if [[ -z " $( check_python_exe_compatible_version " ${python_exe} " ) " ]]; then
218+ die " Invalid Python interpreter version for ${python_exe} . ${requirement_str} "
219+ fi
194220 else
195- python_bin_name =" $( determine_default_python_exe) "
196- if [[ -z " ${python_bin_name } " ]]; then
221+ python_exe =" $( determine_default_python_exe) "
222+ if [[ -z " ${python_exe } " ]]; then
197223 die " No valid Python interpreter found. ${requirement_str} Please check that a valid interpreter is installed and on your \$ PATH."
198224 fi
199225 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} "
226+ echo " ${python_exe} "
210227}
211228
212229function compute_sha256 {
@@ -229,20 +246,25 @@ EOF
229246
230247function bootstrap_pex {
231248 local python=" $1 "
232- local bootstrapped=" ${PANTS_BOOTSTRAP} /pex-${PEX_VERSION } /pex"
249+ local bootstrapped=" ${PANTS_BOOTSTRAP} /pex-${_PEX_VERSION } /pex"
233250 if [[ ! -f " ${bootstrapped} " ]]; then
234251 (
235252 green " Downloading the Pex PEX."
236253 mkdir -p " ${PANTS_BOOTSTRAP} "
237254 local staging_dir
238255 staging_dir=$( tempdir " ${PANTS_BOOTSTRAP} " )
239256 cd " ${staging_dir} "
240- curl -LO " ${PEX_URL} "
257+ curl --proto " =https" \
258+ --tlsv1.2 \
259+ --silent \
260+ --location \
261+ --remote-name \
262+ " ${_PEX_URL} "
241263 fingerprint=" $( compute_sha256 " ${python} " " pex" ) "
242- if [[ " ${PEX_EXPECTED_SHA256 } " != " ${fingerprint} " ]]; then
243- die " SHA256 of ${PEX_URL } is not as expected. Aborting."
264+ if [[ " ${_PEX_EXPECTED_SHA256 } " != " ${fingerprint} " ]]; then
265+ die " SHA256 of ${_PEX_URL } is not as expected. Aborting."
244266 fi
245- green " SHA256 fingerprint of ${PEX_URL } verified."
267+ green " SHA256 fingerprint of ${_PEX_URL } verified."
246268 mkdir -p " $( dirname " ${bootstrapped} " ) "
247269 mv -f " ${staging_dir} /pex" " ${bootstrapped} "
248270 rmdir " ${staging_dir} "
@@ -251,6 +273,15 @@ function bootstrap_pex {
251273 echo " ${bootstrapped} "
252274}
253275
276+ function scrub_PEX_env_vars {
277+ # Ensure the virtualenv PEX runs as shrink-wrapped.
278+ # See: https://github.com/pantsbuild/setup/issues/105
279+ if [[ -n " ${! PEX_@ } " ]]; then
280+ warn " Scrubbing ${! PEX_@ } "
281+ unset " ${! PEX_@ } "
282+ fi
283+ }
284+
254285function bootstrap_virtualenv {
255286 local python=" $1 "
256287 local bootstrapped=" ${PANTS_BOOTSTRAP} /virtualenv-${VIRTUALENV_VERSION} /virtualenv.pex"
@@ -263,7 +294,10 @@ function bootstrap_virtualenv {
263294 staging_dir=$( tempdir " ${PANTS_BOOTSTRAP} " )
264295 cd " ${staging_dir} "
265296 echo " ${VIRTUALENV_REQUIREMENTS} " > requirements.txt
266- " ${python} " " ${pex_path} " -r requirements.txt -c virtualenv -o virtualenv.pex
297+ (
298+ scrub_PEX_env_vars
299+ " ${python} " " ${pex_path} " -r requirements.txt -c virtualenv -o virtualenv.pex
300+ )
267301 mkdir -p " $( dirname " ${bootstrapped} " ) "
268302 mv -f " ${staging_dir} /virtualenv.pex" " ${bootstrapped} "
269303 rm -rf " ${staging_dir} "
@@ -283,7 +317,12 @@ function get_version_for_sha {
283317
284318 # Retrieve the Pants version associated with this commit.
285319 local pants_version
286- pants_version=" $( curl --fail -sL " https://raw.githubusercontent.com/pantsbuild/pants/${sha} /src/python/pants/VERSION" ) "
320+ pants_version=" $( curl --proto " =https" \
321+ --tlsv1.2 \
322+ --fail \
323+ --silent \
324+ --location \
325+ " https://raw.githubusercontent.com/pantsbuild/pants/${sha} /src/python/pants/VERSION" ) "
287326
288327 # Construct the version as the release version from src/python/pants/VERSION, plus the string `+gitXXXXXXXX`,
289328 # where the XXXXXXXX is the first 8 characters of the SHA.
@@ -315,10 +354,15 @@ function bootstrap_pants {
315354 local virtualenv_path
316355 virtualenv_path=" $( bootstrap_virtualenv " ${python} " ) " || exit 1
317356 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} " && \
357+ (
358+ scrub_PEX_env_vars
359+ # shellcheck disable=SC2086
360+ " ${python} " " ${virtualenv_path} " --quiet --no-download " ${staging_dir} /install" && \
361+ # Grab the latest pip, but don't advance setuptools past 58 which drops support for the
362+ # `setup` kwarg `use_2to3` which Pants 1.x sdist dependencies (pystache) use.
363+ " ${staging_dir} /install/bin/pip" install --quiet -U pip " setuptools<58" && \
364+ " ${staging_dir} /install/bin/pip" install ${maybe_find_links} --quiet --progress-bar off " ${pants_requirement} "
365+ ) && \
322366 ln -s " ${staging_dir} /install" " ${staging_dir} /${target_folder_name} " && \
323367 mv " ${staging_dir} /${target_folder_name} " " ${bootstrapped} " && \
324368 green " New virtual environment successfully created at ${bootstrapped} ."
0 commit comments