run_bazel.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. """Runs bazel on arguments.
  3. This is provided for other scripts to run bazel without requiring it be
  4. manually installed.
  5. """
  6. __copyright__ = """
  7. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  8. Exceptions. See /LICENSE for license information.
  9. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. """
  11. import argparse
  12. import subprocess
  13. import time
  14. import scripts_utils
  15. def main() -> None:
  16. parser = argparse.ArgumentParser(description="Runs bazel.")
  17. parser.add_argument(
  18. "--attempts",
  19. metavar="COUNT",
  20. type=int,
  21. default=1,
  22. help="The number of attempts to execute the command, automatically "
  23. "retrying errors that may be transient.",
  24. )
  25. parser.add_argument(
  26. "--jobs-on-last-attempt",
  27. metavar="COUNT",
  28. type=int,
  29. help="Sets the number of jobs in user.bazelrc on the last attempt. If "
  30. "there is only one attempt, this will be set immediately.",
  31. )
  32. parser.add_argument(
  33. "--retry-all-errors",
  34. action="store_true",
  35. help="Retries permanent errors in addition to transient.",
  36. )
  37. script_args, bazel_args = parser.parse_known_args()
  38. bazel = scripts_utils.locate_bazel()
  39. attempt = 0
  40. while True:
  41. attempt += 1
  42. if attempt == script_args.attempts and script_args.jobs_on_last_attempt:
  43. with open("user.bazelrc", "a") as bazelrc:
  44. bazelrc.write(
  45. f"build --jobs={script_args.jobs_on_last_attempt}\n"
  46. )
  47. p = subprocess.run([bazel] + bazel_args)
  48. # If this was the last attempt, or it succeeded, we're done.
  49. if attempt == script_args.attempts or p.returncode == 0:
  50. exit(p.returncode)
  51. # Several error codes are reliably permanent, break immediately.
  52. # `1` -- The build failed.
  53. # `2` -- Command line or environment problem.
  54. # `3` -- Tests failed or timed out, we don't retry at this layer
  55. # on execution timeout.
  56. # `4` -- Test command but no tests found.
  57. # `8` -- Explicitly interrupted build.
  58. #
  59. # Note that `36` is documented as "likely permanent", but we retry
  60. # it as most of our transient failures actually produce that error
  61. # code.
  62. perm_error = (1, 2, 3, 4, 8)
  63. if not script_args.retry_all_errors and p.returncode in perm_error:
  64. exit(p.returncode)
  65. print(
  66. f"Retrying exit code {p.returncode} because it may be transient..."
  67. )
  68. # Also sleep a bit to try to skip over transient machine load.
  69. time.sleep(attempt)
  70. if __name__ == "__main__":
  71. main()