blob: aae9318eb0f85896e9279124e8700150410d89c7 [file] [log] [blame]
Matthias Sohn9f97b8b2022-01-07 10:19:26 +01001#!/usr/bin/env python3
2
3# This script will be run by bazel when the build process starts to
4# generate key-value information that represents the status of the
5# workspace. The output should be like
6#
7# KEY1 VALUE1
8# KEY2 VALUE2
9#
10# If the script exits with non-zero code, it's considered as a failure
11# and the output will be discarded.
12
13from __future__ import print_function
14import os
15import subprocess
16import sys
17
18ROOT = os.path.abspath(__file__)
19while not os.path.exists(os.path.join(ROOT, 'WORKSPACE')):
20 ROOT = os.path.dirname(ROOT)
21CMD = ['git', 'describe', '--always', '--match', 'v[0-9].*', '--dirty']
22
23
24def revision(directory, parent):
25 try:
26 os.chdir(directory)
27 return subprocess.check_output(CMD).strip().decode("utf-8")
28 except OSError as err:
29 print('could not invoke git: %s' % err, file=sys.stderr)
30 sys.exit(1)
31 except subprocess.CalledProcessError as err:
32 # ignore "not a git repository error" to report unknown version
33 return None
34 finally:
35 os.chdir(parent)
36
37
38print("STABLE_BUILD_GITILES_LABEL %s" % revision(ROOT, ROOT))