Skip to content

Commit

Permalink
Fix ./pants script to work with both 3.7 and 3.8 by default
Browse files Browse the repository at this point in the history
[ci skip-rust]
[ci skip-build-wheels]
# Conflicts:
#	src/python/pants/backend/awslambda/python/rules_test.py
  • Loading branch information
Eric-Arellano committed Nov 13, 2020
1 parent e4cb434 commit 6157010
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 22 deletions.
6 changes: 4 additions & 2 deletions build-support/bin/bootstrap_pants_pex.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ cd "$REPO_ROOT" || exit 1

# This script is used to generate pants.pex, which is used to run Pants in CI.

export PY="${PY:-python3.7}"

# shellcheck source=build-support/common.sh
source "${REPO_ROOT}/build-support/common.sh"

PY="$(determine_python)"
export PY

# shellcheck source=build-support/pants_venv
source "${REPO_ROOT}/build-support/pants_venv"
# shellcheck source=build-support/bin/native/bootstrap_code.sh
Expand Down
6 changes: 5 additions & 1 deletion build-support/bin/native/cargo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ set -euo pipefail

REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd ../../.. && pwd -P)

export PY=${PY:-python3.7}
# shellcheck source=build-support/common.sh
source "${REPO_ROOT}/build-support/common.sh"

PY="$(determine_python)"
export PY
# Consumed by the cpython crate.
export PYTHON_SYS_EXECUTABLE="${PY}"

Expand Down
21 changes: 13 additions & 8 deletions build-support/bin/native/print_engine_hash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd ../../.. && pwd -P)"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd ../../.. && pwd -P)"

# shellcheck source=build-support/bin/native/calculate_engine_hash.sh
source "${REPO_ROOT}/build-support/bin/native/calculate_engine_hash.sh"
# shellcheck source=build-support/common.sh
source "${REPO_ROOT}/build-support/common.sh"

# shellcheck source=build-support/pants_venv
source "${REPO_ROOT}/build-support/pants_venv"
PY="$(determine_python)"
export PY

export PY="${PY:-python3.7}"
activate_pants_venv 1>&2 # Redirect to ensure that we don't interfere with stdout.
# shellcheck source=build-support/bin/native/calculate_engine_hash.sh
source "${REPO_ROOT}/build-support/bin/native/calculate_engine_hash.sh"

calculate_current_hash
# shellcheck source=build-support/pants_venv
source "${REPO_ROOT}/build-support/pants_venv"

activate_pants_venv 1>&2 # Redirect to ensure that we don't interfere with stdout.

calculate_current_hash
10 changes: 7 additions & 3 deletions build-support/bin/upload_coverage.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/bin/bash -e

export PY=${PY:-python3.7}
export PYTHON_SYS_EXECUTABLE="${PY}"

REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd ../.. && pwd -P)

# shellcheck source=build-support/common.sh
source "${REPO_ROOT}/build-support/common.sh"

PY="$(determine_python)"
export PY

# shellcheck source=build-support/pants_venv
source "${REPO_ROOT}/build-support/pants_venv"

Expand Down
19 changes: 19 additions & 0 deletions build-support/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,22 @@ function git_merge_base() {
# This prints the tracking branch if set and otherwise falls back to local "master".
git rev-parse --symbolic-full-name --abbrev-ref HEAD@\{upstream\} 2>/dev/null || echo 'master'
}

function determine_python() {
if [[ -n "${PY}" ]]; then
echo "${PY}"
return 0
fi
for version in '3.7' '3.8'; do
local interpreter_path
interpreter_path="$(command -v "python${version}")"
if [[ -z "${interpreter_path}" ]]; then
continue
fi
# Check if the Python version is installed via Pyenv but not activated.
if [[ "$("${interpreter_path}" --version 2>&1 > /dev/null)" == "pyenv: python${version}"* ]]; then
continue
fi
echo "${interpreter_path}" && return 0
done
}
8 changes: 6 additions & 2 deletions build-support/virtualenv
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && git rev-parse --show-toplevel
source "${REPO_ROOT}/build-support/common.sh"

# Locate the binary's path to log to user or die gracefully if not found.
if [[ -z "${PY}" ]]; then
die "Please check that you have a Python 3.7 or 3.8 interpreter installed and discoverable on your \$PATH. If that \
still fails, try setting the env var \$PY to the interpreter you'd like to run Pants with."
fi
if command -v "${PY}" >/dev/null; then
PY="$(command -v "${PY}")"
else
die "No ${PY} interpreter found on the path. Python will not work!"
die "No ${PY} interpreter found on the path. Please check that is installed and discoverable on your \$PATH."
fi

log "Using python at ${PY}"
log "Using Python at ${PY}"

HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
if ! [ -f "${HERE}/virtualenv.dist/BOOTSTRAPPED-${VIRTUALENV_VERSION}" ]; then
Expand Down
20 changes: 14 additions & 6 deletions pants
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
# WRAPPER_SRCPATH=/src/wrapper/src/main/python \
# exec /src/pantsbuild-pants/pants "$@"
#
# The script defaults to running with Python 3.7. To use another Python version,
# such as 3.8, prefix the script with `PY=python3.8`.
# The script defaults to running with either Python 3.7 or Python 3.8. To use another Python version,
# prefix the script with `PY=python3.8`.

set -e
set -eo pipefail

HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

Expand All @@ -47,6 +47,14 @@ fi

# Otherwise, run directly from sources, bootstrapping if needed.

# Exposes:
# + determine_python: Determine which interpreter version to use.
# shellcheck source=build-support/common.sh
source "${HERE}/build-support/common.sh"

PY="$(determine_python)"
export PY

# Exposes:
# + activate_pants_venv: Activate a virtualenv for pants requirements, creating it if needed.
# shellcheck source=build-support/pants_venv
Expand All @@ -57,8 +65,6 @@ source "${HERE}/build-support/pants_venv"
# shellcheck source=build-support/bin/native/bootstrap_code.sh
source "${HERE}/build-support/bin/native/bootstrap_code.sh"

export PY="${PY:-python3.7}"

PANTS_EXE="${HERE}/src/python/pants/bin/pants_loader.py"

if [[ -n "${WRAPPER_REQUIREMENTS}" ]]; then
Expand Down Expand Up @@ -87,8 +93,10 @@ function exec_pants_bare() {
# Redirect activation and native bootstrap to ensure that they don't interfere with stdout.
activate_pants_venv 1>&2
bootstrap_native_code 1>&2
# Because the venv has been activated, we simply say `python` and it will use the venv's version. We cannot use
# `${PY}` because it could use the wrong interpreter if the value is an absolute path.
PYTHONPATH="${PANTS_SRCPATH_STR}:${PYTHONPATH}" RUNNING_PANTS_FROM_SOURCES=1 \
exec "${PY}" "${PANTS_EXE}" "$@"
exec python "${PANTS_EXE}" "$@"
}

if [[ -n "${WRAPPER_REQUIREMENTS}" ]]; then
Expand Down

0 comments on commit 6157010

Please sign in to comment.