Skip to content

Commit 50d3fa0

Browse files
author
Benjy Weinberger
authored
Upgrade to Pants 1.14.0 (#10)
Update ci config and pants script.
1 parent a4ef16f commit 50d3fa0

File tree

3 files changed

+198
-47
lines changed

3 files changed

+198
-47
lines changed

Diff for: .github/workflows/pants.yaml

+27-10
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,34 @@ jobs:
1818
build:
1919
name: Perform CI Checks
2020
needs: org-check
21-
runs-on: ubuntu-latest
21+
runs-on: ubuntu-20.04
22+
strategy:
23+
matrix:
24+
# The python version to run Pants itself on.
25+
python-version: [3.9]
2226
steps:
23-
- uses: actions/checkout@v2
24-
- uses: actions/cache@v2
25-
id: cache
27+
- uses: actions/checkout@v3
28+
- uses: pantsbuild/actions/init-pants@v2
29+
# This action bootstraps pants and manages 2-3 GHA caches.
30+
# See: github.com/pantsbuild/actions/tree/main/init-pants/
2631
with:
27-
path: |
28-
~/.cache/pants/setup
29-
~/.cache/pants/lmdb_store
30-
~/.cache/pants/named_caches
31-
key: ${{ runner.os }}-
32+
pants-python-version: ${{ matrix.python-version }}
33+
# cache0 makes it easy to bust the cache if needed
34+
# just increase the integer to start with a fresh cache
35+
gha-cache-key: cache0-py${{ matrix.python-version }}
36+
# The Kotlin backend uses named_caches for Coursier state,
37+
# so it is appropriate to invalidate on lockfile changes.
38+
named-caches-hash: ${{ hashFiles('3rdparty/jvm/default.lock') }}
39+
# If you're not using a fine-grained remote caching service (see https://www.pantsbuild.org/docs/remote-caching),
40+
# then you may also want to preserve the local Pants cache (lmdb_store). However this must invalidate for
41+
# changes to any file that can affect the build, so may not be practical in larger repos.
42+
# A remote cache service integrates with Pants's fine-grained invalidation and avoids these problems.
43+
cache-lmdb-store: 'true' # defaults to 'false'
44+
# Note that named_caches and lmdb_store falls back to partial restore keys which
45+
# may give a useful partial result that will save time over completely clean state,
46+
# but will cause the cache entry to grow without bound over time.
47+
# See https://pants.readme.io/docs/using-pants-in-ci for tips on how to periodically clean it up.
48+
# Alternatively you change gha-cache-key to ignore old caches.
3249
- name: Bootstrap Pants
3350
run: ./pants --version
3451
- name: Check Pants config files
@@ -39,7 +56,7 @@ jobs:
3956
run: |
4057
./pants package ::
4158
- name: Upload Pants log
42-
uses: actions/upload-artifact@v2
59+
uses: actions/upload-artifact@v3
4360
with:
4461
name: pants-log
4562
path: .pants.d/pants.log

Diff for: pants

+170-32
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,24 @@
1212

1313
set -eou pipefail
1414

15+
# an arbitrary number: bump when there's a change that someone might want to query for
16+
# (e.g. checking $(PANTS_BOOTSTRAP_TOOLS=1 ./pants version) >= ...)
17+
SCRIPT_VERSION=1
18+
19+
# Source any custom bootstrap settings for Pants from PANTS_BOOTSTRAP if it exists.
20+
: ${PANTS_BOOTSTRAP:=".pants.bootstrap"}
21+
if [[ -f "${PANTS_BOOTSTRAP}" ]]; then
22+
source "${PANTS_BOOTSTRAP}"
23+
fi
24+
1525
# NOTE: To use an unreleased version of Pants from the pantsbuild/pants main branch,
1626
# locate the main branch SHA, set PANTS_SHA=<SHA> in the environment, and run this script as usual.
1727
#
1828
# E.g., PANTS_SHA=725fdaf504237190f6787dda3d72c39010a4c574 ./pants --version
29+
#
30+
# You can also use PANTS_VERSION=<VERSION> to override the config version that is in the pants.toml file.
31+
#
32+
# E.g., PANTS_VERSION=2.13.0 ./pants --version
1933

2034
PYTHON_BIN_NAME="${PYTHON:-unspecified}"
2135

@@ -34,9 +48,9 @@ fi
3448

3549
PANTS_BOOTSTRAP="${PANTS_SETUP_CACHE}/bootstrap-$(uname -s)-$(uname -m)"
3650

37-
_PEX_VERSION=2.1.42
51+
_PEX_VERSION=2.1.103
3852
_PEX_URL="https://github.com/pantsbuild/pex/releases/download/v${_PEX_VERSION}/pex"
39-
_PEX_EXPECTED_SHA256="69d6b1b1009b00dd14a3a9f19b72cff818a713ca44b3186c9b12074b2a31e51f"
53+
_PEX_EXPECTED_SHA256="4d45336511484100ae4e2bab24542a8b86b12c8cb89230463593c60d08c4b8d3"
4054

4155
VIRTUALENV_VERSION=20.4.7
4256
VIRTUALENV_REQUIREMENTS=$(
@@ -58,6 +72,8 @@ COLOR_GREEN="\x1b[32m"
5872
COLOR_YELLOW="\x1b[33m"
5973
COLOR_RESET="\x1b[0m"
6074

75+
INSTALL_URL="https://www.pantsbuild.org/docs/installation"
76+
6177
function log() {
6278
echo -e "$@" 1>&2
6379
}
@@ -87,13 +103,18 @@ function get_exe_path_or_die {
87103
fi
88104
}
89105

90-
function get_pants_config_value {
106+
function get_pants_config_string_value {
91107
local config_key="$1"
92108
local optional_space="[[:space:]]*"
93109
local prefix="^${config_key}${optional_space}=${optional_space}"
94110
local raw_value
95-
raw_value="$(sed -ne "/${prefix}/ s#${prefix}##p" "${PANTS_TOML}")"
96-
echo "${raw_value}" | tr -d \"\' && return 0
111+
raw_value="$(sed -ne "/${prefix}/ s|${prefix}||p" "${PANTS_TOML}")"
112+
local optional_suffix="${optional_space}(#.*)?$"
113+
echo "${raw_value}" \
114+
| sed -E \
115+
-e "s|^'([^']*)'${optional_suffix}|\1|" \
116+
-e 's|^"([^"]*)"'"${optional_suffix}"'$|\1|' \
117+
&& return 0
97118
return 0
98119
}
99120

@@ -125,11 +146,16 @@ function determine_pants_version {
125146
return
126147
fi
127148

128-
pants_version="$(get_pants_config_value 'pants_version')"
149+
if [ -n "${PANTS_VERSION:-}" ]; then
150+
echo "${PANTS_VERSION}"
151+
return
152+
fi
153+
154+
pants_version="$(get_pants_config_string_value 'pants_version')"
129155
if [[ -z "${pants_version}" ]]; then
130156
die "Please explicitly specify the \`pants_version\` in your \`pants.toml\` under the \`[GLOBAL]\` scope.
131157
See https://pypi.org/project/pantsbuild.pants/#history for all released versions
132-
and https://www.pantsbuild.org/docs/installation for more instructions."
158+
and ${INSTALL_URL} for more instructions."
133159
fi
134160
pants_major_version="$(echo "${pants_version}" | cut -d '.' -f1)"
135161
pants_minor_version="$(echo "${pants_version}" | cut -d '.' -f2)"
@@ -191,8 +217,8 @@ function determine_default_python_exe {
191217
if [[ -z "${interpreter_path}" ]]; then
192218
continue
193219
fi
194-
# Check if the Python version is installed via Pyenv but not activated.
195-
if [[ "$("${interpreter_path}" --version 2>&1 > /dev/null)" == "pyenv: python${version}"* ]]; then
220+
# Check if a version is shimmed by pyenv or asdf but not configured.
221+
if ! "${interpreter_path}" --version >/dev/null 2>&1; then
196222
continue
197223
fi
198224
if [[ -n "$(check_python_exe_compatible_version "${interpreter_path}")" ]]; then
@@ -248,9 +274,13 @@ function bootstrap_pex {
248274
mkdir -p "${PANTS_BOOTSTRAP}"
249275
local staging_dir
250276
staging_dir=$(tempdir "${PANTS_BOOTSTRAP}")
251-
cd "${staging_dir}"
252-
curl -LO "${_PEX_URL}"
253-
fingerprint="$(compute_sha256 "${python}" "pex")"
277+
curl --proto "=https" \
278+
--tlsv1.2 \
279+
--silent \
280+
--location \
281+
-o "${staging_dir}/pex" \
282+
"${_PEX_URL}"
283+
fingerprint="$(compute_sha256 "${python}" "${staging_dir}/pex")"
254284
if [[ "${_PEX_EXPECTED_SHA256}" != "${fingerprint}" ]]; then
255285
die "SHA256 of ${_PEX_URL} is not as expected. Aborting."
256286
fi
@@ -263,12 +293,22 @@ function bootstrap_pex {
263293
echo "${bootstrapped}"
264294
}
265295

266-
function scrub_PEX_env_vars {
296+
function scrub_env_vars {
267297
# Ensure the virtualenv PEX runs as shrink-wrapped.
268298
# See: https://github.com/pantsbuild/setup/issues/105
269-
if [[ -n "${!PEX_@}" ]]; then
270-
warn "Scrubbing ${!PEX_@}"
271-
unset "${!PEX_@}"
299+
local -r pex_env_vars=(${!PEX_@})
300+
if [[ ! ${#pex_env_vars[@]} -eq 0 ]]; then
301+
local -r pex_env_vars_to_scrub="${pex_env_vars[@]/PEX_ROOT}"
302+
if [[ -n "${pex_env_vars_to_scrub[@]}" ]]; then
303+
warn "Scrubbing ${pex_env_vars_to_scrub[@]}"
304+
unset ${pex_env_vars_to_scrub[@]}
305+
fi
306+
fi
307+
# Also ensure pip doesn't think packages on PYTHONPATH
308+
# are already installed.
309+
if [ -n "${PYTHONPATH:-}" ]; then
310+
warn "Scrubbing PYTHONPATH"
311+
unset PYTHONPATH
272312
fi
273313
}
274314

@@ -282,11 +322,10 @@ function bootstrap_virtualenv {
282322
mkdir -p "${PANTS_BOOTSTRAP}"
283323
local staging_dir
284324
staging_dir=$(tempdir "${PANTS_BOOTSTRAP}")
285-
cd "${staging_dir}"
286-
echo "${VIRTUALENV_REQUIREMENTS}" > requirements.txt
325+
echo "${VIRTUALENV_REQUIREMENTS}" > "${staging_dir}/requirements.txt"
287326
(
288-
scrub_PEX_env_vars
289-
"${python}" "${pex_path}" -r requirements.txt -c virtualenv -o virtualenv.pex
327+
scrub_env_vars
328+
"${python}" "${pex_path}" -r "${staging_dir}/requirements.txt" -c virtualenv -o "${staging_dir}/virtualenv.pex"
290329
)
291330
mkdir -p "$(dirname "${bootstrapped}")"
292331
mv -f "${staging_dir}/virtualenv.pex" "${bootstrapped}"
@@ -307,7 +346,12 @@ function get_version_for_sha {
307346

308347
# Retrieve the Pants version associated with this commit.
309348
local pants_version
310-
pants_version="$(curl --fail -sL "https://raw.githubusercontent.com/pantsbuild/pants/${sha}/src/python/pants/VERSION")"
349+
pants_version="$(curl --proto "=https" \
350+
--tlsv1.2 \
351+
--fail \
352+
--silent \
353+
--location \
354+
"https://raw.githubusercontent.com/pantsbuild/pants/${sha}/src/python/pants/VERSION")"
311355

312356
# Construct the version as the release version from src/python/pants/VERSION, plus the string `+gitXXXXXXXX`,
313357
# where the XXXXXXXX is the first 8 characters of the SHA.
@@ -318,17 +362,27 @@ function bootstrap_pants {
318362
local pants_version="$1"
319363
local python="$2"
320364
local pants_sha="${3:-}"
365+
local pants_debug="${4:-}"
321366

322-
local pants_requirement="pantsbuild.pants==${pants_version}"
367+
local pants_requirements=(pantsbuild.pants==${pants_version})
323368
local maybe_find_links
324369
if [[ -z "${pants_sha}" ]]; then
325370
maybe_find_links=""
326371
else
327372
maybe_find_links="--find-links=$(find_links_url "${pants_version}" "${pants_sha}")"
328-
fi
373+
fi
374+
375+
local debug_suffix
376+
if [[ -z "${pants_debug}" ]]; then
377+
debug_suffix=""
378+
else
379+
debug_suffix="-debug"
380+
pants_requirements+=(debugpy==1.6.0)
381+
fi
382+
329383
local python_major_minor_version
330384
python_major_minor_version="$(get_python_major_minor_version "${python}")"
331-
local target_folder_name="${pants_version}_py${python_major_minor_version}"
385+
local target_folder_name="${pants_version}_py${python_major_minor_version}${debug_suffix}"
332386
local bootstrapped="${PANTS_BOOTSTRAP}/${target_folder_name}"
333387

334388
if [[ ! -d "${bootstrapped}" ]]; then
@@ -338,13 +392,15 @@ function bootstrap_pants {
338392
staging_dir=$(tempdir "${PANTS_BOOTSTRAP}")
339393
local virtualenv_path
340394
virtualenv_path="$(bootstrap_virtualenv "${python}")" || exit 1
341-
green "Installing ${pants_requirement} into a virtual environment at ${bootstrapped}"
395+
green "Installing ${pants_requirements[@]} into a virtual environment at ${bootstrapped}"
342396
(
343-
scrub_PEX_env_vars
397+
scrub_env_vars
344398
# 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}"
399+
"${python}" "${virtualenv_path}" --quiet --no-download "${staging_dir}/install" && \
400+
# Grab the latest pip, but don't advance setuptools past 58 which drops support for the
401+
# `setup` kwarg `use_2to3` which Pants 1.x sdist dependencies (pystache) use.
402+
"${staging_dir}/install/bin/pip" install --quiet -U pip "setuptools<58" && \
403+
"${staging_dir}/install/bin/pip" install ${maybe_find_links} --quiet --progress-bar off "${pants_requirements[@]}"
348404
) && \
349405
ln -s "${staging_dir}/install" "${staging_dir}/${target_folder_name}" && \
350406
mv "${staging_dir}/${target_folder_name}" "${bootstrapped}" && \
@@ -354,19 +410,101 @@ function bootstrap_pants {
354410
echo "${bootstrapped}"
355411
}
356412

413+
function run_bootstrap_tools {
414+
# functionality for introspecting the bootstrapping process, without actually doing it
415+
if [[ "${PANTS_BOOTSTRAP_TOOLS}" -gt "${SCRIPT_VERSION}" ]]; then
416+
die "$0 script (bootstrap version ${SCRIPT_VERSION}) is too old for this invocation (with PANTS_BOOTSTRAP_TOOLS=${PANTS_BOOTSTRAP_TOOLS}).
417+
Please update it by following ${INSTALL_URL}"
418+
fi
419+
420+
case "${1:-}" in
421+
bootstrap-cache-key)
422+
local pants_version=$(determine_pants_version)
423+
local python="$(determine_python_exe "${pants_version}")"
424+
# the python above may be a shim (e.g. pyenv or homebrew), so let's get an estimate of the
425+
# actual path, as will be symlinked in the virtualenv. (NB. virtualenv does more complicated
426+
# things, but we at least emulate the symlink-resolution that it does.)
427+
local python_executable_path="$("${python}" -c 'import os, sys; print(os.path.realpath(sys.executable))')"
428+
429+
local requirements_file="$(mktemp)"
430+
echo "${VIRTUALENV_REQUIREMENTS}" > "${requirements_file}"
431+
local virtualenv_requirements_sha256="$(compute_sha256 "${python}" "${requirements_file}")"
432+
rm "${requirements_file}"
433+
434+
local parts=(
435+
"os_name=$(uname -s)"
436+
"arch=$(uname -m)"
437+
"python_path=${python}"
438+
"python_executable_path=${python_executable_path}"
439+
# the full interpreter information, for maximum compatibility
440+
"python_version=$("$python" --version)"
441+
"pex_version=${_PEX_VERSION}"
442+
"virtualenv_requirements_sha256=${virtualenv_requirements_sha256}"
443+
"pants_version=${pants_version}"
444+
)
445+
echo "${parts[*]}"
446+
;;
447+
bootstrap-version)
448+
echo "${SCRIPT_VERSION}"
449+
;;
450+
help|"")
451+
cat <<EOF
452+
Usage: PANTS_BOOTSTRAP_TOOLS=1 $0 ...
453+
454+
Subcommands:
455+
bootstrap-cache-key
456+
Print an opaque that can be used as a key for accurate and safe caching of
457+
the pants bootstrap directories.
458+
459+
(Added in bootstrap version 1.)
460+
461+
bootstrap-version
462+
Print a version number for the bootstrap script itself.
463+
464+
Distributed scripts (such as reusable CI formulae) that use these bootstrap
465+
tools should set PANTS_BOOTSTRAP_TOOLS to the minimum script version for the
466+
features they require. For example, if 'some-tool' was added in version 123:
467+
468+
PANTS_BOOTSTRAP_TOOLS=123 ./pants some-tool
469+
470+
(Added in bootstrap version 1.)
471+
EOF
472+
;;
473+
*)
474+
die "Unknown subcommand for bootstrap tools: $1. Do you mean to run without PANTS_BOOTSTRAP_TOOLS=1? Or, update this script ($INSTALL_URL)?"
475+
esac
476+
}
477+
478+
if [[ "${PANTS_BOOTSTRAP_TOOLS:-}" -gt 0 ]]; then
479+
run_bootstrap_tools "$@"
480+
exit 0
481+
fi
482+
357483
# Ensure we operate from the context of the ./pants buildroot.
358484
cd "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
359485
pants_version="$(determine_pants_version)"
360486
python="$(determine_python_exe "${pants_version}")"
361-
pants_dir="$(bootstrap_pants "${pants_version}" "${python}" "${PANTS_SHA:-}")" || exit 1
487+
pants_dir="$(bootstrap_pants "${pants_version}" "${python}" "${PANTS_SHA:-}" "${PANTS_DEBUG:-}")" || exit 1
362488

363489
pants_python="${pants_dir}/bin/python"
364-
pants_binary="${pants_dir}/bin/pants"
490+
pants_binary=(${pants_dir}/bin/pants)
365491
pants_extra_args=""
366492
if [[ -n "${PANTS_SHA:-}" ]]; then
367493
pants_extra_args="${pants_extra_args} --python-repos-repos=$(find_links_url "$pants_version" "$PANTS_SHA")"
368494
fi
369495

496+
pants_debug_args=()
497+
if [ -n "${PANTS_DEBUG:-}" ]; then
498+
if [[ "$*" != *"--no-pantsd"* ]]; then
499+
echo "Error! Must pass '--no-pantsd' when using PANTS_DEBUG"
500+
exit 1
501+
fi
502+
# NB: We can't invoke `-m debugpy` as that'll prepend CWD to sys.path which might have unintended side-effects.
503+
# `-c` also prepends, but we strip that ourselves.
504+
pants_binary=(-c "__import__(\"sys\").path.pop(0);__import__(\"debugpy.server.cli\").server.cli.main()" --listen 127.0.0.1:5678 --wait-for-client "${pants_binary[@]}")
505+
echo "Will launch debugpy server at '127.0.0.1:5678' waiting for client connection."
506+
fi
507+
370508
# shellcheck disable=SC2086
371-
exec "${pants_python}" "${pants_binary}" ${pants_extra_args} \
509+
exec "${pants_python}" "${pants_binary[@]}" ${pants_extra_args} \
372510
--pants-bin-name="${PANTS_BIN_NAME}" --pants-version=${pants_version} "$@"

0 commit comments

Comments
 (0)