blob: bd0689ab30cab893b3bab9a63f8ba78b4c1d35d9 [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))
Matthias Sohnb3ee1562022-01-07 10:23:02 +010039for kind in ['modules']:
40 kind_dir = os.path.join(ROOT, kind)
41 for d in os.listdir(kind_dir):
42 p = os.path.join(kind_dir, d)
43 if os.path.isdir(p):
44 v = revision(p, ROOT)
45 print('STABLE_BUILD_%s_LABEL %s' % (os.path.basename(p).upper(),
46 v if v else 'unknown'))