Bazel: Migrate workspace status script to python

This allows to seamlessly build Gerrit on different operating systems.
See also discussions in: [1], [2].

[1] https://github.com/bazelbuild/bazel/issues/5958
[2] https://github.com/bazelbuild/bazel/issues/10151

Change-Id: I7738a23c08f4890a2010182587b64a2979256ad4
This commit is contained in:
David Ostrovsky 2019-11-02 10:12:49 +01:00 committed by David Pursehouse
parent df03895a49
commit a7e95e5bc1
3 changed files with 45 additions and 22 deletions

View File

@ -1,4 +1,4 @@
build --workspace_status_command=./tools/workspace-status.sh --strategy=Closure=worker
build --workspace_status_command="python ./tools/workspace_status.py" --strategy=Closure=worker
build --repository_cache=~/.gerritcodereview/bazel-cache/repository
build --action_env=PATH
build --disk_cache=~/.gerritcodereview/bazel-cache/cas

View File

@ -1,21 +0,0 @@
#!/usr/bin/env bash
# This script will be run by bazel when the build process starts to
# generate key-value information that represents the status of the
# workspace. The output should be like
#
# KEY1 VALUE1
# KEY2 VALUE2
#
# If the script exits with non-zero code, it's considered as a failure
# and the output will be discarded.
function rev() {
cd $1; git describe --always --match "v[0-9].*" --dirty
}
echo STABLE_BUILD_GERRIT_LABEL $(rev .)
for p in plugins/* ; do
test -d "$p" || continue
echo STABLE_BUILD_$(echo $(basename $p)_LABEL|tr '[a-z]' '[A-Z]' ) $(rev $p)
done

44
tools/workspace_status.py Normal file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env python
# This script will be run by bazel when the build process starts to
# generate key-value information that represents the status of the
# workspace. The output should be like
#
# KEY1 VALUE1
# KEY2 VALUE2
#
# If the script exits with non-zero code, it's considered as a failure
# and the output will be discarded.
from __future__ import print_function
import os
import subprocess
import sys
ROOT = os.path.abspath(__file__)
while not os.path.exists(os.path.join(ROOT, 'WORKSPACE')):
ROOT = os.path.dirname(ROOT)
CMD = ['git', 'describe', '--always', '--match', 'v[0-9].*', '--dirty']
def revision(directory, parent):
try:
os.chdir(directory)
return subprocess.check_output(CMD).strip().decode("utf-8")
except OSError as err:
print('could not invoke git: %s' % err, file=sys.stderr)
sys.exit(1)
except subprocess.CalledProcessError as err:
print('error using git: %s' % err, file=sys.stderr)
sys.exit(1)
finally:
os.chdir(parent)
print("STABLE_BUILD_GERRIT_LABEL %s" % revision(ROOT, ROOT))
for d in os.listdir(os.path.join(ROOT, 'plugins')):
p = os.path.join('plugins', d)
if os.path.isdir(p):
v = revision(p, ROOT)
if v:
print('STABLE_BUILD_%s_LABEL %s' % (os.path.basename(p).upper(), v))