/
ci.py
executable file
·357 lines (294 loc) · 11.5 KB
/
ci.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python3
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
import argparse
import os
import platform
import subprocess
from enum import Enum
from pathlib import Path
from typing import List, Optional
from common import banner, die, green, travis_section
def main() -> None:
banner("CI BEGINS")
args = create_parser().parse_args()
setup_environment(python_version=args.python_version)
if args.bootstrap:
bootstrap(
clean=args.bootstrap_clean,
try_to_skip_rust_compilation=args.bootstrap_try_to_skip_rust_compilation,
python_version=args.python_version,
)
set_run_from_pex()
if args.githooks:
run_githooks()
if args.smoke_tests:
run_smoke_tests()
if args.lint:
run_lint(remote_cache_enabled=args.remote_cache_enabled)
if args.clippy:
run_clippy()
if args.cargo_audit:
run_cargo_audit()
if args.unit_tests or args.integration_tests:
run_python_tests(
include_unit=args.unit_tests,
include_integration=args.integration_tests,
remote_cache_enabled=args.remote_cache_enabled,
)
if args.rust_tests:
run_rust_tests()
if args.platform_specific_tests:
run_platform_specific_tests()
banner("CI ENDS")
print()
green("SUCCESS")
# -------------------------------------------------------------------------
# Options
# -------------------------------------------------------------------------
class PythonVersion(Enum):
py37 = "3.7"
py38 = "3.8"
def __str__(self) -> str:
return str(self.value)
def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Runs commons tests for local or hosted CI.")
parser.add_argument(
"--python-version",
type=PythonVersion,
choices=list(PythonVersion),
default=PythonVersion.py37,
help="Run Pants with this version.",
)
parser.add_argument(
"--remote-cache-enabled",
action="store_true",
help=(
"Enable remote caching via Toolchain. This requires setting the options "
"`remote_oauth_bearer_token_path` and `remote_ca_certs_path` in your environment."
),
)
parser.add_argument(
"--bootstrap", action="store_true", help="Bootstrap a pants.pex from local sources."
)
parser.add_argument(
"--bootstrap-clean",
action="store_true",
help="Before bootstrapping, clean the environment so that it's like a fresh git clone.",
)
parser.add_argument(
"--bootstrap-try-to-skip-rust-compilation",
action="store_true",
help=(
"If possible, i.e. `native_engine.so` if is already present, don't rebuild the Rust "
"engine. Otherwise, build. This means that you may end up using an outdated version of "
"native_engine.so; this option should generally be avoided."
),
)
parser.add_argument("--githooks", action="store_true", help="Run pre-commit githook.")
parser.add_argument(
"--smoke-tests",
action="store_true",
help="Run smoke tests of bootstrapped Pants and repo BUILD files.",
)
parser.add_argument("--lint", action="store_true", help="Run lint over whole codebase.")
parser.add_argument("--clippy", action="store_true", help="Run Clippy on Rust code.")
parser.add_argument(
"--cargo-audit", action="store_true", help="Run Cargo audit of Rust dependencies."
)
parser.add_argument("--unit-tests", action="store_true", help="Run Python unit tests.")
parser.add_argument("--rust-tests", action="store_true", help="Run Rust tests.")
parser.add_argument(
"--integration-tests", action="store_true", help="Run Python integration tests."
)
parser.add_argument(
"--platform-specific-tests", action="store_true", help="Test platform-specific behavior."
)
return parser
# -------------------------------------------------------------------------
# Set up the environment
# -------------------------------------------------------------------------
def setup_environment(*, python_version: PythonVersion):
set_cxx_compiler()
set_pants_dev()
setup_python_interpreter(python_version)
def set_pants_dev() -> None:
"""We do this because we are running against a Pants clone."""
os.environ["PANTS_DEV"] = "1"
def set_cxx_compiler() -> None:
compiler = "g++" if platform.system() != "Darwin" else "clang++"
os.environ["CXX"] = compiler
def setup_python_interpreter(version: PythonVersion) -> None:
if "PY" not in os.environ:
os.environ["PY"] = f"python{version}"
constraints_env_var = "PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS"
if constraints_env_var not in os.environ:
os.environ[constraints_env_var] = f"['CPython=={version}.*']"
banner(f"Setting interpreter constraints to {os.environ[constraints_env_var]}")
def set_run_from_pex() -> None:
os.environ["RUN_PANTS_FROM_PEX"] = "1"
IS_PR_BUILD = "CI" in os.environ and os.environ.get("TRAVIS_PULL_REQUEST", "false") != "false"
# -------------------------------------------------------------------------
# Bootstrap pants.pex
# -------------------------------------------------------------------------
def bootstrap(
*, clean: bool, try_to_skip_rust_compilation: bool, python_version: PythonVersion
) -> None:
with travis_section("Bootstrap", f"Bootstrapping Pants as a Python {python_version} PEX"):
if clean:
try:
subprocess.run(["./build-support/python/clean.sh"], check=True)
except subprocess.CalledProcessError:
die("Failed to clean before bootstrapping Pants.")
try:
skip_native_engine_so_bootstrap = (
try_to_skip_rust_compilation
and Path("src/python/pants/engine/internals/native_engine.so").exists()
)
subprocess.run(
["./build-support/bin/bootstrap_pants_pex.sh"],
check=True,
env={
**os.environ,
"SKIP_NATIVE_ENGINE_SO_BOOTSTRAP": (
"true" if skip_native_engine_so_bootstrap else "false"
),
},
)
except subprocess.CalledProcessError:
die("Failed to bootstrap Pants.")
def check_pants_pex_exists() -> None:
if not Path("pants.pex").is_file():
die(
"pants.pex not found! Either run `./build-support/bin/ci.py --bootstrap` or check that "
"AWS is properly downloading the uploaded `pants.pex`."
)
# -------------------------------------------------------------------------
# Test commands
# -------------------------------------------------------------------------
def _run_command(
command: List[str],
*,
slug: str,
start_message: str,
die_message: str,
requires_pex: bool = True,
) -> None:
with travis_section(slug, start_message):
if requires_pex:
check_pants_pex_exists()
try:
subprocess.run(command, check=True)
except subprocess.CalledProcessError:
die(die_message)
def _test_command(*, extra_args: Optional[List[str]] = None) -> List[str]:
targets = ["build-support::", "src::", "tests::", "pants-plugins::"]
command = ["./pants.pex", "test", *targets]
if extra_args:
command.extend(extra_args)
return command
def run_githooks() -> None:
_run_command(
["./build-support/githooks/pre-commit"],
slug="PreCommit",
start_message="Running pre-commit checks.",
die_message="Pre-commit checks failed.",
)
def run_smoke_tests() -> None:
def run_check(command: List[str]) -> None:
print(f"* Executing `./pants.pex {' '.join(command)}` as a smoke test")
try:
subprocess.run(
["./pants.pex", *command],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
check=True,
)
except subprocess.CalledProcessError:
die(f"Failed to execute `./pants {command}`.")
checks = [
["help", "goals"],
["list", "::"],
["roots"],
["help", "targets"],
]
with travis_section("SmokeTest", "Smoke testing bootstrapped Pants and repo BUILD files"):
check_pants_pex_exists()
for check in checks:
run_check(check)
def run_lint(*, remote_cache_enabled: bool) -> None:
targets = ["build-support::", "src::", "tests::"]
command = ["./pants.pex", "--tag=-nolint", "lint", "typecheck", *targets]
if remote_cache_enabled and IS_PR_BUILD is False:
command.append("--pants-config-files=pants.remote-cache.toml")
_run_command(
command,
slug="Lint",
start_message="Running lint checks.",
die_message="Lint check failure.",
)
def run_clippy() -> None:
_run_command(
["./cargo", "clippy", "--all"],
slug="RustClippy",
start_message="Running Clippy on Rust code.",
die_message="Clippy failure.",
requires_pex=False,
)
def run_cargo_audit() -> None:
with travis_section("CargoAudit", "Running Cargo audit on Rust code"):
try:
subprocess.run(["./cargo", "install", "--version", "0.13.1", "cargo-audit"], check=True)
subprocess.run(["./cargo", "audit"], check=True)
except subprocess.CalledProcessError:
die("Cargo audit failure")
def run_rust_tests() -> None:
is_macos = platform.system() == "Darwin"
command = [
"./cargo",
"test",
"--all",
# We pass --tests to skip doc tests because our generated protos contain invalid doc tests
# in their comments.
"--tests",
"--",
"--nocapture",
]
if is_macos:
# The osx travis environment has a low file descriptors ulimit, so we avoid running too many
# tests in parallel.
command.append("--test-threads=1")
with travis_section("RustTests", "Running Rust tests"):
try:
subprocess.run(command, env={**os.environ, "RUST_BACKTRACE": "all"}, check=True)
except subprocess.CalledProcessError:
die("Rust test failure.")
def run_python_tests(
*, include_unit: bool, include_integration: bool, remote_cache_enabled: bool
) -> None:
extra_args = []
if remote_cache_enabled and IS_PR_BUILD is False:
extra_args.append("--pants-config-files=pants.remote-cache.toml")
if not include_unit and not include_integration:
raise ValueError(
"Must specify True for at least one of `include_unit` and `include_integration`."
)
elif include_unit and not include_integration:
extra_args.append("--tag=-integration")
elif not include_unit and include_integration:
extra_args.append("--tag=+integration")
_run_command(
command=_test_command(extra_args=extra_args),
slug="PythonTests",
start_message="Running Python tests.",
die_message="Python test failure.",
)
def run_platform_specific_tests() -> None:
_run_command(
command=_test_command(extra_args=["--tag=+platform_specific_behavior"]),
slug="PlatformSpecificTests",
start_message=f"Running platform-specific tests on platform {platform.system()}",
die_message="Platform-specific test failure.",
)
if __name__ == "__main__":
main()