From a7e95e5bc17359a638f1e7183abc28e71becf9be Mon Sep 17 00:00:00 2001 From: David Ostrovsky Date: Sat, 2 Nov 2019 10:12:49 +0100 Subject: [PATCH 1/4] 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 --- .bazelrc | 2 +- tools/workspace-status.sh | 21 ------------------- tools/workspace_status.py | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 22 deletions(-) delete mode 100755 tools/workspace-status.sh create mode 100644 tools/workspace_status.py diff --git a/.bazelrc b/.bazelrc index fef1fa364f..bf3aa6c3b0 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 diff --git a/tools/workspace-status.sh b/tools/workspace-status.sh deleted file mode 100755 index 2370786059..0000000000 --- a/tools/workspace-status.sh +++ /dev/null @@ -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 diff --git a/tools/workspace_status.py b/tools/workspace_status.py new file mode 100644 index 0000000000..eef864eee3 --- /dev/null +++ b/tools/workspace_status.py @@ -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)) From 4cdd7f7d86f682c481480c75032cd9b47eface66 Mon Sep 17 00:00:00 2001 From: David Ostrovsky Date: Sun, 3 Nov 2019 13:26:35 +0100 Subject: [PATCH 2/4] Bazel: Special handling for plugins not under git control To be compatible with the shell script, ignore "not a git repository error" and report "unknown" version. Change-Id: Ifa5dafea63743e6e09958f4283231c3eb7e45afe --- tools/workspace_status.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/workspace_status.py b/tools/workspace_status.py index eef864eee3..86df519ed5 100644 --- a/tools/workspace_status.py +++ b/tools/workspace_status.py @@ -29,8 +29,8 @@ def revision(directory, parent): 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) + # ignore "not a git repository error" to report unknown version + return None finally: os.chdir(parent) @@ -40,5 +40,5 @@ 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)) + print('STABLE_BUILD_%s_LABEL %s' % (os.path.basename(p).upper(), + v if v else 'unknown')) From 262a4da88f131f8302d6f77f49b5e077abd9a11a Mon Sep 17 00:00:00 2001 From: David Ostrovsky Date: Wed, 30 Aug 2017 09:03:20 +0200 Subject: [PATCH 3/4] Bazel: Portable way to guess whether root directory is reached Change-Id: I582aca7d9ba8a8b17e6d54580490f242dfe0926d (cherry picked from commit e89717db20a364b6f8ad9d3614280a32ef70b844) --- tools/download_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/download_file.py b/tools/download_file.py index b705c1158d..26671f0f20 100755 --- a/tools/download_file.py +++ b/tools/download_file.py @@ -84,7 +84,7 @@ opts.add_option('--unsign', action='store_true') args, _ = opts.parse_args() root_dir = args.o -while root_dir and root_dir != "/": +while root_dir and path.dirname(root_dir) != root_dir: root_dir, n = path.split(root_dir) if n == 'WORKSPACE': break From 1a55d7fb861d36f23ee92f2a6873ec7ec9a8fc2a Mon Sep 17 00:00:00 2001 From: David Ostrovsky Date: Mon, 4 Nov 2019 09:19:36 +0100 Subject: [PATCH 4/4] Bazel: Remove platform specific workspace status script Now that workspace status script was migrated to Python we don't need platform specific scripts any more. Change-Id: I88940285d04af99c786a3764aae5a444d767fb2f --- tools/workspace-status.cmd | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 tools/workspace-status.cmd diff --git a/tools/workspace-status.cmd b/tools/workspace-status.cmd deleted file mode 100644 index 4a3b88e164..0000000000 --- a/tools/workspace-status.cmd +++ /dev/null @@ -1,2 +0,0 @@ -echo STABLE_BUILD_GERRIT_LABEL dev -echo STABLE_WORKSPACE_ROOT %cd%