xref: /JGit/tools/workspace_status.py (revision d35f0ffb7c33a11f8192ec64da5a736827ab472d)
1*d35f0ffbSDavid Ostrovsky#!/usr/bin/env python
2*d35f0ffbSDavid Ostrovsky# Copyright (C) 2020, David Ostrovsky <david@ostrovsky.org> and others
3*d35f0ffbSDavid Ostrovsky#
4*d35f0ffbSDavid Ostrovsky# This program and the accompanying materials are made available under the
5*d35f0ffbSDavid Ostrovsky# terms of the Eclipse Distribution License v. 1.0 which is available at
6*d35f0ffbSDavid Ostrovsky# http://www.eclipse.org/org/documents/edl-v10.php.
7*d35f0ffbSDavid Ostrovsky#
8*d35f0ffbSDavid Ostrovsky# SPDX-License-Identifier: BSD-3-Clause
9*d35f0ffbSDavid Ostrovsky
10*d35f0ffbSDavid Ostrovsky# This script will be run by bazel when the build process starts to
11*d35f0ffbSDavid Ostrovsky# generate key-value information that represents the status of the
12*d35f0ffbSDavid Ostrovsky# workspace. The output should be like
13*d35f0ffbSDavid Ostrovsky#
14*d35f0ffbSDavid Ostrovsky# KEY1 VALUE1
15*d35f0ffbSDavid Ostrovsky# KEY2 VALUE2
16*d35f0ffbSDavid Ostrovsky#
17*d35f0ffbSDavid Ostrovsky# If the script exits with non-zero code, it's considered as a failure
18*d35f0ffbSDavid Ostrovsky# and the output will be discarded.
19*d35f0ffbSDavid Ostrovsky
20*d35f0ffbSDavid Ostrovskyfrom __future__ import print_function
21*d35f0ffbSDavid Ostrovskyimport os
22*d35f0ffbSDavid Ostrovskyimport subprocess
23*d35f0ffbSDavid Ostrovskyimport sys
24*d35f0ffbSDavid Ostrovsky
25*d35f0ffbSDavid OstrovskyROOT = os.path.abspath(__file__)
26*d35f0ffbSDavid Ostrovskywhile not os.path.exists(os.path.join(ROOT, 'WORKSPACE')):
27*d35f0ffbSDavid Ostrovsky    ROOT = os.path.dirname(ROOT)
28*d35f0ffbSDavid OstrovskyCMD = ['git', 'describe', '--always', '--match', 'v[0-9].*', '--dirty']
29*d35f0ffbSDavid Ostrovsky
30*d35f0ffbSDavid Ostrovsky
31*d35f0ffbSDavid Ostrovskydef revision(directory, parent):
32*d35f0ffbSDavid Ostrovsky    try:
33*d35f0ffbSDavid Ostrovsky        os.chdir(directory)
34*d35f0ffbSDavid Ostrovsky        return subprocess.check_output(CMD).strip().decode("utf-8")
35*d35f0ffbSDavid Ostrovsky    except OSError as err:
36*d35f0ffbSDavid Ostrovsky        print('could not invoke git: %s' % err, file=sys.stderr)
37*d35f0ffbSDavid Ostrovsky        sys.exit(1)
38*d35f0ffbSDavid Ostrovsky    except subprocess.CalledProcessError as err:
39*d35f0ffbSDavid Ostrovsky        # ignore "not a git repository error" to report unknown version
40*d35f0ffbSDavid Ostrovsky        return None
41*d35f0ffbSDavid Ostrovsky    finally:
42*d35f0ffbSDavid Ostrovsky        os.chdir(parent)
43*d35f0ffbSDavid Ostrovsky
44*d35f0ffbSDavid Ostrovsky
45*d35f0ffbSDavid Ostrovskyprint("STABLE_BUILD_JGIT_LABEL %s" % revision(ROOT, ROOT))
46