|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). |
| 3 | +# Licensed under the Apache License, Version 2.0 (see LICENSE). |
| 4 | + |
| 5 | +# =============================== NOTE =============================== |
| 6 | +# This ./pants bootstrap script comes from the pantsbuild/setup |
| 7 | +# project. It is intended to be checked into your code repository so |
| 8 | +# that other developers have the same setup. |
| 9 | +# |
| 10 | +# Learn more here: https://www.pantsbuild.org/docs/installation |
| 11 | +# ==================================================================== |
| 12 | + |
| 13 | +set -eou pipefail |
| 14 | + |
| 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 | +# |
| 18 | +# E.g., PANTS_SHA=725fdaf504237190f6787dda3d72c39010a4c574 ./pants --version |
| 19 | + |
| 20 | +PYTHON_BIN_NAME="${PYTHON:-unspecified}" |
| 21 | + |
| 22 | +# Set this to specify a non-standard location for this script to read the Pants version from. |
| 23 | +# NB: This will *not* cause Pants itself to use this location as a config file. |
| 24 | +# You can use PANTS_CONFIG_FILES or --pants-config-files to do so. |
| 25 | +PANTS_TOML=${PANTS_TOML:-pants.toml} |
| 26 | + |
| 27 | +PANTS_BIN_NAME="${PANTS_BIN_NAME:-$0}" |
| 28 | + |
| 29 | +PANTS_SETUP_CACHE="${PANTS_SETUP_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/pants/setup}" |
| 30 | +# If given a relative path, we fix it to be absolute. |
| 31 | +if [[ "$PANTS_SETUP_CACHE" != /* ]]; then |
| 32 | + PANTS_SETUP_CACHE="${PWD}/${PANTS_SETUP_CACHE}" |
| 33 | +fi |
| 34 | + |
| 35 | +PANTS_BOOTSTRAP="${PANTS_SETUP_CACHE}/bootstrap-$(uname -s)-$(uname -m)" |
| 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" |
| 40 | + |
| 41 | +VIRTUALENV_VERSION=20.4.7 |
| 42 | +VIRTUALENV_REQUIREMENTS=$( |
| 43 | +cat << EOF |
| 44 | +virtualenv==${VIRTUALENV_VERSION} --hash sha256:2b0126166ea7c9c3661f5b8e06773d28f83322de7a3ff7d06f0aed18c9de6a76 |
| 45 | +filelock==3.0.12 --hash sha256:929b7d63ec5b7d6b71b0fa5ac14e030b3f70b75747cef1b10da9b879fef15836 |
| 46 | +six==1.16.0 --hash sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254 |
| 47 | +distlib==0.3.2 --hash sha256:23e223426b28491b1ced97dc3bbe183027419dfc7982b4fa2f05d5f3ff10711c |
| 48 | +appdirs==1.4.4 --hash sha256:a841dacd6b99318a741b166adb07e19ee71a274450e68237b4650ca1055ab128 |
| 49 | +importlib-resources==5.1.4; python_version < "3.7" --hash sha256:e962bff7440364183203d179d7ae9ad90cb1f2b74dcb84300e88ecc42dca3351 |
| 50 | +importlib-metadata==4.5.0; python_version < "3.8" --hash sha256:833b26fb89d5de469b24a390e9df088d4e52e4ba33b01dc5e0e4f41b81a16c00 |
| 51 | +zipp==3.4.1; python_version < "3.10" --hash sha256:51cb66cc54621609dd593d1787f286ee42a5c0adbb4b29abea5a63edc3e03098 |
| 52 | +typing-extensions==3.10.0.0; python_version < "3.8" --hash sha256:779383f6086d90c99ae41cf0ff39aac8a7937a9283ce0a414e5dd782f4c94a84 |
| 53 | +EOF |
| 54 | +) |
| 55 | + |
| 56 | +COLOR_RED="\x1b[31m" |
| 57 | +COLOR_GREEN="\x1b[32m" |
| 58 | +COLOR_YELLOW="\x1b[33m" |
| 59 | +COLOR_RESET="\x1b[0m" |
| 60 | + |
| 61 | +function log() { |
| 62 | + echo -e "$@" 1>&2 |
| 63 | +} |
| 64 | + |
| 65 | +function die() { |
| 66 | + (($# > 0)) && log "${COLOR_RED}$*${COLOR_RESET}" |
| 67 | + exit 1 |
| 68 | +} |
| 69 | + |
| 70 | +function green() { |
| 71 | + (($# > 0)) && log "${COLOR_GREEN}$*${COLOR_RESET}" |
| 72 | +} |
| 73 | + |
| 74 | +function warn() { |
| 75 | + (($# > 0)) && log "${COLOR_YELLOW}$*${COLOR_RESET}" |
| 76 | +} |
| 77 | + |
| 78 | +function tempdir { |
| 79 | + mkdir -p "$1" |
| 80 | + mktemp -d "$1"/pants.XXXXXX |
| 81 | +} |
| 82 | + |
| 83 | +function get_exe_path_or_die { |
| 84 | + local exe="$1" |
| 85 | + if ! command -v "${exe}"; then |
| 86 | + die "Could not find ${exe}. Please ensure ${exe} is on your PATH." |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +function get_pants_config_value { |
| 91 | + local config_key="$1" |
| 92 | + local optional_space="[[:space:]]*" |
| 93 | + local prefix="^${config_key}${optional_space}=${optional_space}" |
| 94 | + local raw_value |
| 95 | + raw_value="$(sed -ne "/${prefix}/ s#${prefix}##p" "${PANTS_TOML}")" |
| 96 | + echo "${raw_value}" | tr -d \"\' && return 0 |
| 97 | + return 0 |
| 98 | +} |
| 99 | + |
| 100 | +function get_python_major_minor_version { |
| 101 | + local python_exe="$1" |
| 102 | + "$python_exe" <<EOF |
| 103 | +import sys |
| 104 | +major_minor_version = ''.join(str(version_num) for version_num in sys.version_info[0:2]) |
| 105 | +print(major_minor_version) |
| 106 | +EOF |
| 107 | +} |
| 108 | + |
| 109 | +# The high-level flow: |
| 110 | +# |
| 111 | +# 1.) Resolve the Pants version from config so that we know what interpreters we can use, what to name the venv, |
| 112 | +# and what to install via pip. |
| 113 | +# 2.) Resolve the Python interpreter, first reading from the env var $PYTHON, then using a default based on the Pants |
| 114 | +# version. |
| 115 | +# 3.) Check if the venv already exists via a naming convention, and create the venv if not found. |
| 116 | +# 4.) Execute Pants with the resolved Python interpreter and venv. |
| 117 | +# |
| 118 | +# After that, Pants itself will handle making sure any requested plugins |
| 119 | +# are installed and up to date. |
| 120 | + |
| 121 | +function determine_pants_version { |
| 122 | + if [ -n "${PANTS_SHA:-}" ]; then |
| 123 | + # get_version_for_sha will echo the version, thus "returning" it from this function. |
| 124 | + get_version_for_sha "$PANTS_SHA" |
| 125 | + return |
| 126 | + fi |
| 127 | + |
| 128 | + pants_version="$(get_pants_config_value 'pants_version')" |
| 129 | + if [[ -z "${pants_version}" ]]; then |
| 130 | + die "Please explicitly specify the \`pants_version\` in your \`pants.toml\` under the \`[GLOBAL]\` scope. |
| 131 | +See https://pypi.org/project/pantsbuild.pants/#history for all released versions |
| 132 | +and https://www.pantsbuild.org/docs/installation for more instructions." |
| 133 | + fi |
| 134 | + pants_major_version="$(echo "${pants_version}" | cut -d '.' -f1)" |
| 135 | + pants_minor_version="$(echo "${pants_version}" | cut -d '.' -f2)" |
| 136 | + # 1.26 is the first version to support `pants.toml`, so we fail eagerly if using an outdated version. |
| 137 | + if [[ "${pants_major_version}" -eq 1 && "${pants_minor_version}" -le 25 ]]; then |
| 138 | + die "This version of the \`./pants\` script does not work with Pants <= 1.25.0 (and it also requires using \`pants.toml\`, |
| 139 | +rather than \`pants.ini\`). Instead, either upgrade your \`pants_version\` or use the version of the \`./pants\` script |
| 140 | +at https://raw.githubusercontent.com/Eric-Arellano/setup/0d445edef57cb89fd830db70810e38f050b0a268/pants." |
| 141 | + fi |
| 142 | + echo "${pants_version}" |
| 143 | +} |
| 144 | + |
| 145 | +function set_supported_python_versions { |
| 146 | + local pants_version="$1" |
| 147 | + local pants_major_version |
| 148 | + local pants_minor_version |
| 149 | + pants_major_version="$(echo "${pants_version}" | cut -d '.' -f1)" |
| 150 | + pants_minor_version="$(echo "${pants_version}" | cut -d '.' -f2)" |
| 151 | + if [[ "${pants_major_version}" -eq 1 ]]; then |
| 152 | + supported_python_versions_decimal=('3.6' '3.7' '3.8') |
| 153 | + supported_python_versions_int=('36' '37' '38') |
| 154 | + supported_message='3.6, 3.7, or 3.8' |
| 155 | + elif [[ "${pants_major_version}" -eq 2 && "${pants_minor_version}" -eq 0 ]]; then |
| 156 | + supported_python_versions_decimal=('3.6' '3.7' '3.8') |
| 157 | + supported_python_versions_int=('36' '37' '38') |
| 158 | + supported_message='3.6, 3.7, or 3.8' |
| 159 | + elif [[ "${pants_major_version}" -eq 2 && "${pants_minor_version}" -eq 1 ]]; then |
| 160 | + supported_python_versions_decimal=('3.7' '3.8' '3.6') |
| 161 | + supported_python_versions_int=('37' '38' '36') |
| 162 | + supported_message='3.7, 3.8, or 3.6 (deprecated)' |
| 163 | + elif [[ "${pants_major_version}" -eq 2 && "${pants_minor_version}" -lt 5 ]]; then |
| 164 | + supported_python_versions_decimal=('3.8' '3.7') |
| 165 | + supported_python_versions_int=('38' '37') |
| 166 | + supported_message='3.7 or 3.8' |
| 167 | + else |
| 168 | + # We put 3.9 first because Apple Silicon only works properly with Python 3.9, even though it's possible to have |
| 169 | + # older Pythons installed. This makes it more likely that Pants will work out-of-the-box. |
| 170 | + supported_python_versions_decimal=('3.9' '3.8' '3.7') |
| 171 | + supported_python_versions_int=('39' '38' '37') |
| 172 | + supported_message='3.7, 3.8, or 3.9' |
| 173 | + fi |
| 174 | +} |
| 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 | + |
| 187 | +function determine_default_python_exe { |
| 188 | + for version in "${supported_python_versions_decimal[@]}" "3" ""; do |
| 189 | + local interpreter_path |
| 190 | + interpreter_path="$(command -v "python${version}")" |
| 191 | + if [[ -z "${interpreter_path}" ]]; then |
| 192 | + continue |
| 193 | + 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 |
| 196 | + continue |
| 197 | + fi |
| 198 | + if [[ -n "$(check_python_exe_compatible_version "${interpreter_path}")" ]]; then |
| 199 | + echo "${interpreter_path}" && return 0 |
| 200 | + fi |
| 201 | + done |
| 202 | +} |
| 203 | + |
| 204 | +function determine_python_exe { |
| 205 | + local pants_version="$1" |
| 206 | + set_supported_python_versions "${pants_version}" |
| 207 | + local requirement_str="For \`pants_version = \"${pants_version}\"\`, Pants requires Python ${supported_message} to run." |
| 208 | + |
| 209 | + local python_exe |
| 210 | + if [[ "${PYTHON_BIN_NAME}" != 'unspecified' ]]; then |
| 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 |
| 215 | + else |
| 216 | + python_exe="$(determine_default_python_exe)" |
| 217 | + if [[ -z "${python_exe}" ]]; then |
| 218 | + die "No valid Python interpreter found. ${requirement_str} Please check that a valid interpreter is installed and on your \$PATH." |
| 219 | + fi |
| 220 | + fi |
| 221 | + echo "${python_exe}" |
| 222 | +} |
| 223 | + |
| 224 | +function compute_sha256 { |
| 225 | + local python="$1" |
| 226 | + local path="$2" |
| 227 | + |
| 228 | + "$python" <<EOF |
| 229 | +import hashlib |
| 230 | +
|
| 231 | +hasher = hashlib.sha256() |
| 232 | +with open('${path}', 'rb') as fp: |
| 233 | + buf = fp.read() |
| 234 | + hasher.update(buf) |
| 235 | +print(hasher.hexdigest()) |
| 236 | +EOF |
| 237 | +} |
| 238 | + |
| 239 | +# TODO(John Sirois): GC race loser tmp dirs leftover from bootstrap_XXX |
| 240 | +# functions. Any tmp dir w/o a symlink pointing to it can go. |
| 241 | + |
| 242 | +function bootstrap_pex { |
| 243 | + local python="$1" |
| 244 | + local bootstrapped="${PANTS_BOOTSTRAP}/pex-${_PEX_VERSION}/pex" |
| 245 | + if [[ ! -f "${bootstrapped}" ]]; then |
| 246 | + ( |
| 247 | + green "Downloading the Pex PEX." |
| 248 | + mkdir -p "${PANTS_BOOTSTRAP}" |
| 249 | + local staging_dir |
| 250 | + staging_dir=$(tempdir "${PANTS_BOOTSTRAP}") |
| 251 | + cd "${staging_dir}" |
| 252 | + curl -LO "${_PEX_URL}" |
| 253 | + fingerprint="$(compute_sha256 "${python}" "pex")" |
| 254 | + if [[ "${_PEX_EXPECTED_SHA256}" != "${fingerprint}" ]]; then |
| 255 | + die "SHA256 of ${_PEX_URL} is not as expected. Aborting." |
| 256 | + fi |
| 257 | + green "SHA256 fingerprint of ${_PEX_URL} verified." |
| 258 | + mkdir -p "$(dirname "${bootstrapped}")" |
| 259 | + mv -f "${staging_dir}/pex" "${bootstrapped}" |
| 260 | + rmdir "${staging_dir}" |
| 261 | + ) 1>&2 || exit 1 |
| 262 | + fi |
| 263 | + echo "${bootstrapped}" |
| 264 | +} |
| 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 | + |
| 275 | +function bootstrap_virtualenv { |
| 276 | + local python="$1" |
| 277 | + local bootstrapped="${PANTS_BOOTSTRAP}/virtualenv-${VIRTUALENV_VERSION}/virtualenv.pex" |
| 278 | + if [[ ! -f "${bootstrapped}" ]]; then |
| 279 | + ( |
| 280 | + green "Creating the virtualenv PEX." |
| 281 | + pex_path="$(bootstrap_pex "${python}")" || exit 1 |
| 282 | + mkdir -p "${PANTS_BOOTSTRAP}" |
| 283 | + local staging_dir |
| 284 | + staging_dir=$(tempdir "${PANTS_BOOTSTRAP}") |
| 285 | + cd "${staging_dir}" |
| 286 | + echo "${VIRTUALENV_REQUIREMENTS}" > requirements.txt |
| 287 | + ( |
| 288 | + scrub_PEX_env_vars |
| 289 | + "${python}" "${pex_path}" -r requirements.txt -c virtualenv -o virtualenv.pex |
| 290 | + ) |
| 291 | + mkdir -p "$(dirname "${bootstrapped}")" |
| 292 | + mv -f "${staging_dir}/virtualenv.pex" "${bootstrapped}" |
| 293 | + rm -rf "${staging_dir}" |
| 294 | + ) 1>&2 || exit 1 |
| 295 | + fi |
| 296 | + echo "${bootstrapped}" |
| 297 | +} |
| 298 | + |
| 299 | +function find_links_url { |
| 300 | + local pants_version="$1" |
| 301 | + local pants_sha="$2" |
| 302 | + echo -n "https://binaries.pantsbuild.org/wheels/pantsbuild.pants/${pants_sha}/${pants_version/+/%2B}/index.html" |
| 303 | +} |
| 304 | + |
| 305 | +function get_version_for_sha { |
| 306 | + local sha="$1" |
| 307 | + |
| 308 | + # Retrieve the Pants version associated with this commit. |
| 309 | + local pants_version |
| 310 | + pants_version="$(curl --fail -sL "https://raw.githubusercontent.com/pantsbuild/pants/${sha}/src/python/pants/VERSION")" |
| 311 | + |
| 312 | + # Construct the version as the release version from src/python/pants/VERSION, plus the string `+gitXXXXXXXX`, |
| 313 | + # where the XXXXXXXX is the first 8 characters of the SHA. |
| 314 | + echo "${pants_version}+git${sha:0:8}" |
| 315 | +} |
| 316 | + |
| 317 | +function bootstrap_pants { |
| 318 | + local pants_version="$1" |
| 319 | + local python="$2" |
| 320 | + local pants_sha="${3:-}" |
| 321 | + |
| 322 | + local pants_requirement="pantsbuild.pants==${pants_version}" |
| 323 | + local maybe_find_links |
| 324 | + if [[ -z "${pants_sha}" ]]; then |
| 325 | + maybe_find_links="" |
| 326 | + else |
| 327 | + maybe_find_links="--find-links=$(find_links_url "${pants_version}" "${pants_sha}")" |
| 328 | + fi |
| 329 | + local python_major_minor_version |
| 330 | + python_major_minor_version="$(get_python_major_minor_version "${python}")" |
| 331 | + local target_folder_name="${pants_version}_py${python_major_minor_version}" |
| 332 | + local bootstrapped="${PANTS_BOOTSTRAP}/${target_folder_name}" |
| 333 | + |
| 334 | + if [[ ! -d "${bootstrapped}" ]]; then |
| 335 | + ( |
| 336 | + green "Bootstrapping Pants using ${python}" |
| 337 | + local staging_dir |
| 338 | + staging_dir=$(tempdir "${PANTS_BOOTSTRAP}") |
| 339 | + local virtualenv_path |
| 340 | + virtualenv_path="$(bootstrap_virtualenv "${python}")" || exit 1 |
| 341 | + green "Installing ${pants_requirement} into a virtual environment at ${bootstrapped}" |
| 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 | + ) && \ |
| 349 | + ln -s "${staging_dir}/install" "${staging_dir}/${target_folder_name}" && \ |
| 350 | + mv "${staging_dir}/${target_folder_name}" "${bootstrapped}" && \ |
| 351 | + green "New virtual environment successfully created at ${bootstrapped}." |
| 352 | + ) 1>&2 || exit 1 |
| 353 | + fi |
| 354 | + echo "${bootstrapped}" |
| 355 | +} |
| 356 | + |
| 357 | +# Ensure we operate from the context of the ./pants buildroot. |
| 358 | +cd "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" |
| 359 | +pants_version="$(determine_pants_version)" |
| 360 | +python="$(determine_python_exe "${pants_version}")" |
| 361 | +pants_dir="$(bootstrap_pants "${pants_version}" "${python}" "${PANTS_SHA:-}")" || exit 1 |
| 362 | + |
| 363 | +pants_python="${pants_dir}/bin/python" |
| 364 | +pants_binary="${pants_dir}/bin/pants" |
| 365 | +pants_extra_args="" |
| 366 | +if [[ -n "${PANTS_SHA:-}" ]]; then |
| 367 | + pants_extra_args="${pants_extra_args} --python-repos-repos=$(find_links_url "$pants_version" "$PANTS_SHA")" |
| 368 | +fi |
| 369 | + |
| 370 | +# shellcheck disable=SC2086 |
| 371 | +exec "${pants_python}" "${pants_binary}" ${pants_extra_args} \ |
| 372 | + --pants-bin-name="${PANTS_BIN_NAME}" --pants-version=${pants_version} "$@" |
0 commit comments