Skip to content

Commit 8fca370

Browse files
Yan TangKeen Yee Liau
authored andcommitted
Add check_bazel_version_range function
1 parent 2640ba0 commit 8fca370

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎internal/common/check_bazel_version.bzl‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ as the continuous integration, so they don't trip over incompatibilities with
1919
rules used in the project.
2020
"""
2121

22-
load(":check_version.bzl", "check_version")
22+
load(":check_version.bzl", "check_version", "check_version_range")
2323

2424
# From https://github.com/tensorflow/tensorflow/blob/5541ef4fbba56cf8930198373162dd3119e6ee70/tensorflow/workspace.bzl#L44
2525

@@ -55,3 +55,44 @@ def check_bazel_version(minimum_bazel_version, message = ""):
5555
minimum_bazel_version,
5656
message,
5757
))
58+
59+
# Check that a bazel version being used is in the version range.
60+
# Args:
61+
# minimum_bazel_version in the form "<major>.<minor>.<patch>"
62+
# maximum_bazel_version in the form "<major>.<minor>.<patch>"
63+
def check_bazel_version_range(minimum_bazel_version, maximum_bazel_version, message = ""):
64+
"""
65+
Verify the users Bazel version is in the version range.
66+
67+
This should be called from the `WORKSPACE` file so that the build fails as
68+
early as possible. For example:
69+
70+
```
71+
# in WORKSPACE:
72+
load("@build_bazel_rules_nodejs//:defs.bzl", "check_bazel_version_range")
73+
check_bazel_version_range("0.11.0", "0.22.0")
74+
```
75+
76+
Args:
77+
minimum_bazel_version: a string indicating the minimum version
78+
maximum_bazel_version: a string indicating the maximum version
79+
message: optional string to print to your users, could be used to help them update
80+
"""
81+
if "bazel_version" not in dir(native):
82+
fail("\nCurrent Bazel version is lower than 0.2.1, expected at least %s\n" %
83+
minimum_bazel_version)
84+
elif not native.bazel_version:
85+
print("\nCurrent Bazel is not a release version, cannot check for " +
86+
"compatibility.")
87+
print("Make sure that you are running at least Bazel %s.\n" % minimum_bazel_version)
88+
elif not check_version_range(
89+
native.bazel_version,
90+
minimum_bazel_version,
91+
maximum_bazel_version,
92+
):
93+
fail("\nCurrent Bazel version is {}, expected >= {} and <= {}\n{}".format(
94+
native.bazel_version,
95+
minimum_bazel_version,
96+
maximum_bazel_version,
97+
message,
98+
))

‎internal/common/check_version.bzl‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,21 @@ def check_version(current_version, minimum_version):
6060
True if current_version is greater or equal to the minimum_version, False otherwise
6161
"""
6262
return parse_version(current_version) >= parse_version(minimum_version)
63+
64+
def check_version_range(current_version, minimum_version, maximum_version):
65+
"""
66+
Verify that a version string >= minimum version and <= maximum version.
67+
68+
The format handled for the version strings is "<major>.<minor>.<patch>-<date> <commit>"
69+
70+
Args:
71+
current_version: a string indicating the version to check
72+
minimum_version: a string indicating the minimum version
73+
maximum_version: a string indicating the maximum version
74+
75+
Returns:
76+
True if current_version >= minimum_version and <= maximum_version,
77+
False otherwise.
78+
"""
79+
return (parse_version(current_version) >= parse_version(minimum_version) and
80+
parse_version(current_version) <= parse_version(maximum_version))

‎internal/common/check_version_test.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ def testVersionComparison(self):
2424
result = self.globals['check_version']('1.2.2', '1.2.3')
2525
self.assertIs(result, False)
2626

27+
def testVersionRangeWithin(self):
28+
result = self.globals['check_version_range']('1.2.2', '1.2.1', '1.2.3')
29+
self.assertIs(result, True)
30+
31+
def testVersionOutOfLowRange(self):
32+
result = self.globals['check_version_range']('1.2.0', '1.2.1', '1.2.3')
33+
self.assertIs(result, False)
34+
35+
def testVersionOutOfHighRange(self):
36+
result = self.globals['check_version_range']('1.2.4', '1.2.1', '1.2.3')
37+
self.assertIs(result, False)
38+
2739
def testNotAlphaComparison(self):
2840
result = self.globals['check_version']('1.12.3', '1.2.1')
2941
self.assertIs(result, True)

0 commit comments

Comments
 (0)