12
12
13
13
set -eou pipefail
14
14
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.
17
17
#
18
18
# E.g., PANTS_SHA=725fdaf504237190f6787dda3d72c39010a4c574 ./pants --version
19
19
34
34
35
35
PANTS_BOOTSTRAP=" ${PANTS_SETUP_CACHE} /bootstrap-$( uname -s) -$( uname -m) "
36
36
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"
40
40
41
41
VIRTUALENV_VERSION=20.4.7
42
42
VIRTUALENV_REQUIREMENTS=$(
55
55
56
56
COLOR_RED=" \x1b[31m"
57
57
COLOR_GREEN=" \x1b[32m"
58
+ COLOR_YELLOW=" \x1b[33m"
58
59
COLOR_RESET=" \x1b[0m"
59
60
60
61
function log() {
@@ -70,6 +71,10 @@ function green() {
70
71
(( $# > 0 )) && log " ${COLOR_GREEN} $* ${COLOR_RESET} "
71
72
}
72
73
74
+ function warn() {
75
+ (( $# > 0 )) && log " ${COLOR_YELLOW} $* ${COLOR_RESET} "
76
+ }
77
+
73
78
function tempdir {
74
79
mkdir -p " $1 "
75
80
mktemp -d " $1 " /pants.XXXXXX
@@ -168,8 +173,19 @@ function set_supported_python_versions {
168
173
fi
169
174
}
170
175
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
+
171
187
function determine_default_python_exe {
172
- for version in " ${supported_python_versions_decimal[@]} " ; do
188
+ for version in " ${supported_python_versions_decimal[@]} " " 3 " " " ; do
173
189
local interpreter_path
174
190
interpreter_path=" $( command -v " python${version} " ) "
175
191
if [[ -z " ${interpreter_path} " ]]; then
@@ -179,7 +195,9 @@ function determine_default_python_exe {
179
195
if [[ " $( " ${interpreter_path} " --version 2>&1 > /dev/null) " == " pyenv: python${version} " * ]]; then
180
196
continue
181
197
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
183
201
done
184
202
}
185
203
@@ -188,25 +206,19 @@ function determine_python_exe {
188
206
set_supported_python_versions " ${pants_version} "
189
207
local requirement_str=" For \` pants_version = \" ${pants_version} \"\` , Pants requires Python ${supported_message} to run."
190
208
191
- local python_bin_name
209
+ local python_exe
192
210
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
194
215
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
197
218
die " No valid Python interpreter found. ${requirement_str} Please check that a valid interpreter is installed and on your \$ PATH."
198
219
fi
199
220
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} "
210
222
}
211
223
212
224
function compute_sha256 {
@@ -229,20 +241,20 @@ EOF
229
241
230
242
function bootstrap_pex {
231
243
local python=" $1 "
232
- local bootstrapped=" ${PANTS_BOOTSTRAP} /pex-${PEX_VERSION } /pex"
244
+ local bootstrapped=" ${PANTS_BOOTSTRAP} /pex-${_PEX_VERSION } /pex"
233
245
if [[ ! -f " ${bootstrapped} " ]]; then
234
246
(
235
247
green " Downloading the Pex PEX."
236
248
mkdir -p " ${PANTS_BOOTSTRAP} "
237
249
local staging_dir
238
250
staging_dir=$( tempdir " ${PANTS_BOOTSTRAP} " )
239
251
cd " ${staging_dir} "
240
- curl -LO " ${PEX_URL } "
252
+ curl -LO " ${_PEX_URL } "
241
253
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."
244
256
fi
245
- green " SHA256 fingerprint of ${PEX_URL } verified."
257
+ green " SHA256 fingerprint of ${_PEX_URL } verified."
246
258
mkdir -p " $( dirname " ${bootstrapped} " ) "
247
259
mv -f " ${staging_dir} /pex" " ${bootstrapped} "
248
260
rmdir " ${staging_dir} "
@@ -251,6 +263,15 @@ function bootstrap_pex {
251
263
echo " ${bootstrapped} "
252
264
}
253
265
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
+
254
275
function bootstrap_virtualenv {
255
276
local python=" $1 "
256
277
local bootstrapped=" ${PANTS_BOOTSTRAP} /virtualenv-${VIRTUALENV_VERSION} /virtualenv.pex"
@@ -263,7 +284,10 @@ function bootstrap_virtualenv {
263
284
staging_dir=$( tempdir " ${PANTS_BOOTSTRAP} " )
264
285
cd " ${staging_dir} "
265
286
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
+ )
267
291
mkdir -p " $( dirname " ${bootstrapped} " ) "
268
292
mv -f " ${staging_dir} /virtualenv.pex" " ${bootstrapped} "
269
293
rm -rf " ${staging_dir} "
@@ -315,10 +339,13 @@ function bootstrap_pants {
315
339
local virtualenv_path
316
340
virtualenv_path=" $( bootstrap_virtualenv " ${python} " ) " || exit 1
317
341
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
+ ) && \
322
349
ln -s " ${staging_dir} /install" " ${staging_dir} /${target_folder_name} " && \
323
350
mv " ${staging_dir} /${target_folder_name} " " ${bootstrapped} " && \
324
351
green " New virtual environment successfully created at ${bootstrapped} ."
0 commit comments