forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpants
executable file
·131 lines (114 loc) · 4.66 KB
/
pants
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env bash
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
# This bootstrap script runs pants from the live sources in this repo.
#
# Further support is added for projects wrapping pants with custom external extensions. In the
# future this will work differently (see: https://github.com/pantsbuild/pants/issues/5), but
# currently pants extenders can invoke this script exporting a few environment variables to include
# the extension source and requirements for development purposes:
# WRAPPER_SRCPATH This is a colon separated list of paths containing extension sourcecode.
# WRAPPER_REQUIREMENTS This is a colon separated list of pip install compatible requirements.txt
# files.
#
# For example, with a wrapping project layed out like so:
# /src/wrapper/
# src/main/python/
# wrapper/
# ...
# dependencies/python/
# BUILD
# requirements.txt
#
# And a pantsbuild/pants clone like so:
# /src/pantsbuild-pants
#
# You could invoke pants in the wrapper with its custom extension enabled using a script like so:
# /src/wrapper/pants
# ==
# #!/usr/bin/env bash
# WRAPPER_REQUIREMENTS="/src/wrapper/dependencies/python/requirements.txt" \
# WRAPPER_SRCPATH=/src/wrapper/src/main/python \
# exec /src/pantsbuild-pants/pants "$@"
#
# The script defaults to running with Python 3. To use a specific Python version,
# such as 3.7, prefix the script with `PY=python3.7`.
set -e
HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# Set RUN_PANTS_FROM_PEX to non-empty to cause this wrapper script to delegate directly
# to ./pants.pex. We use this in CI to avoid bootstrapping in every shard.
if [[ -n "${RUN_PANTS_FROM_PEX}" ]]; then
exec "${HERE}/pants.pex" "$@"
fi
# Otherwise, run directly from sources, bootstrapping if needed.
# Exposes:
# + activate_pants_venv: Activate a virtualenv for pants requirements, creating it if needed.
# shellcheck source=build-support/pants_venv
source "${HERE}/build-support/pants_venv"
# Exposes:
# + bootstrap_native_code: Builds target-specific native engine binaries.
# shellcheck source=build-support/bin/native/bootstrap_code.sh
source "${HERE}/build-support/bin/native/bootstrap_code.sh"
export PY="${PY:-python3}"
PANTS_EXE="${HERE}/src/python/pants/bin/pants_loader.py"
if [[ -n "${WRAPPER_REQUIREMENTS}" ]]; then
# WONTFIX: fixing the array expansion is too difficult to be worth it. See https://github.com/koalaman/shellcheck/wiki/SC2207.
# shellcheck disable=SC2207
REQUIREMENTS=(
$(echo "${WRAPPER_REQUIREMENTS}" | tr : ' ')
"${REQUIREMENTS[@]}"
)
fi
PANTS_SRCPATH=(
"${HERE}/src/python"
)
if [[ -n "${WRAPPER_SRCPATH}" ]]; then
# WONTFIX: fixing the array expansion is too difficult to be worth it. See https://github.com/koalaman/shellcheck/wiki/SC2207.
# shellcheck disable=SC2207
PANTS_SRCPATH=(
$(echo "${WRAPPER_SRCPATH}" | tr : ' ')
"${PANTS_SRCPATH[@]}"
)
fi
PANTS_SRCPATH_STR="$(echo "${PANTS_SRCPATH[@]}" | tr ' ' :)"
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
PYTHONPATH="${PANTS_SRCPATH_STR}:${PYTHONPATH}" \
exec python "${PANTS_EXE}" "$@"
}
if [[ -n "${WRAPPER_REQUIREMENTS}" ]]; then
log "*** Running pants with extra requirements: ${WRAPPER_REQUIREMENTS} ***"
fi
if [[ -n "${WRAPPER_SRCPATH}" ]]; then
log "*** Running pants with extra sources ${WRAPPER_SRCPATH} ***"
fi
if [[ -n "$PANTS_DEV" && "$PANTS_DEV" -eq 0 ]]; then
# Unexport PANTS_DEV if explicitly set to 0.
export -n PANTS_DEV
else
# We're running against a Pants clone.
export PANTS_DEV=1
fi
# Integration tests depend on an up-to-date `pants.pex`. Here, we check if we're running tests
# and ensure the `pants.pex` is generated if so. We do this here, rather than via `pants-plugins`,
# because we want the file to be generated _before_ any tests run, not during that Pants invocation,
# as the dependency is marked via BUILD files so should not be constructed at runtime.
#
# Note that we have no way to distinguish between integration tests vs unit tests here, so we
# unfortunately rebuild the pants.pex for unit tests too.
#
# We do not do this when in CI because CI downloads the PEX from AWS and we never want a worker
# shard to try bootstrapping the PEX itself.
test_goal_used=false
for arg in "$@"; do
if [[ "${arg}" == 'test' || "${arg}" == 'test.*' ]]; then
test_goal_used=true
fi
done
no_regen_pex="${NO_REGEN_PEX:-${TRAVIS}}"
if [[ "${test_goal_used}" == 'true' && "${no_regen_pex}" != 'true' ]]; then
"$HERE/build-support/bin/bootstrap_pants_pex.sh"
fi
exec_pants_bare "$@"