1""" Helpers to check minimum version of bazel."""
2
3def _extract_version_number(bazel_version):
4    """Extracts the semantic version number from a version string
5
6    Args:
7      bazel_version: the version string that begins with the semantic version
8        e.g. "1.2.3rc1 abc1234" where "abc1234" is a commit hash.
9
10    Returns:
11      The semantic version string, like "1.2.3".
12    """
13    for i in range(len(bazel_version)):
14        c = bazel_version[i]
15        if not (c.isdigit() or c == "."):
16            return bazel_version[:i]
17    return bazel_version
18
19# Parse the bazel version string from `native.bazel_version`.
20# e.g.
21# "0.10.0rc1 abc123d" => (0, 10, 0)
22# "0.3.0" => (0, 3, 0)
23def _parse_bazel_version(bazel_version):
24    """Parses a version string into a 3-tuple of ints
25
26    int tuples can be compared directly using binary operators (<, >).
27
28    Args:
29      bazel_version: the Bazel version string
30
31    Returns:
32      An int 3-tuple of a (major, minor, patch) version.
33    """
34
35    version = _extract_version_number(bazel_version)
36    return tuple([int(n) for n in version.split(".")])
37
38def check_bazel_version_at_least(minimum_bazel_version):
39    if "bazel_version" not in dir(native):
40        fail("\nCurrent Bazel version is lower than 0.2.1, expected at least %s\n" % minimum_bazel_version)
41    elif not native.bazel_version:
42        print("\nCurrent Bazel is not a release version, cannot check for compatibility.")
43        print("Make sure that you are running at least Bazel %s.\n" % minimum_bazel_version)
44        return
45
46    if _parse_bazel_version(native.bazel_version) < _parse_bazel_version(minimum_bazel_version):
47        fail("\nCurrent Bazel version is {}, expected at least {}\n".format(
48            native.bazel_version,
49            minimum_bazel_version,
50        ))
51
52parse_bazel_version = _parse_bazel_version
53