workspace_status.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. import subprocess
  10. """
  11. Script executed by Bazel before every build via [`workspace_status_command`]
  12. (https://bazel.build/docs/user-manual#workspace-status).
  13. This script is run outside of the Bazel sandbox, so a system must have all of
  14. the required dependencies pre-installed. Changes to this script that depend on
  15. anything other than the Python standard library should be avoided.
  16. """
  17. GIT_COMMIT_HASH_KEY_NAME = "GIT_COMMIT_HASH"
  18. BUILD_TIME_KEY_NAME = "MZ_BUILD_TIME"
  19. def main():
  20. git_hash = get_git_hash(".")
  21. build_time = get_build_time()
  22. # Bazel expects this program to print zero or more key value pairs to
  23. # stdout, one per-line. The first space after the key separates the key
  24. # names from the values, the rest of the line is considered to be the
  25. # value.
  26. #
  27. # There are two kinds of values Bazel supports "stable" and "volatile",
  28. # read the docs for more info.
  29. #
  30. # <https://bazel.build/docs/user-manual#workspace-status>
  31. print(f"{GIT_COMMIT_HASH_KEY_NAME} {git_hash}")
  32. print(f"{BUILD_TIME_KEY_NAME} {build_time}")
  33. def get_git_hash(path):
  34. out = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=path, text=True)
  35. return out.strip()
  36. def get_build_time():
  37. out = subprocess.check_output(["date", "-u", "+%Y-%m-%dT%H:%M:%SZ"], text=True)
  38. return out.strip()
  39. if __name__ == "__main__":
  40. main()