forked from graalvm/mx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mx_javacompliance.py
344 lines (304 loc) · 12.3 KB
/
mx_javacompliance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#
# ----------------------------------------------------------------------------------------------------
#
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
# ----------------------------------------------------------------------------------------------------
#
import re
import itertools
import mx
class JavaCompliance(mx.Comparable):
"""
Represents one or more major Java versions.
Example valid compliance specifications and the JDKs they match:
"8+" - jdk8, jdk9, jdk10, ...
"1.8" - jdk8
"8..12" - jdk8, jdk9, jdk10, jdk11, jdk12
"8,13+" - jdk8, jdk13, jdk14, ...
"8..9,13+" - jdk8, jdk9, jdk13, jdk14, ...
"8,11,13+" - jdk8, jdk11, jdk13, jdk14, ...
There can be multiple parts to a version string specifying a non-contiguous range.
Part N of a multi-part version string must have a strict upper bound (i.e. cannot end with "+")
and its upper bound must be less than the lower bound of part N+1. Only major versions less
than 10 can have an optional "1." prefix. The lowest recognized major version is 2.
"""
# Examples: "8", "13"
_int_re = re.compile(r'(\d+)$')
# Example: "1.8..13"
_version_range_re = re.compile(r'(1\.)?(\d+)\.\.(1\.)?(\d+)$')
# Example: "13+"
_open_range_re = re.compile(r'(1\.)?(\d+)\+$')
# Examples: "1.8", "13"
_singleton_range_re = re.compile(r'(1\.)?(\d+)$')
# Examples: "17-loom"
_loom_re = re.compile(r'(\d+)-loom$')
@staticmethod
def _error_prefix(spec, part_index, part):
return 'JavaCompliance("{}"): Part {} ("{}")'.format(spec, part_index, part)
class _Range(mx.Comparable):
"""
Represents a contiguous range of version values.
"""
def __init__(self, low, high):
self._low = low
self._high = high
def __repr__(self):
if self._low == self._high:
return str(self._low)
if self._high is None:
return str(self._low) + '+'
return '{}..{}'.format(self._low, self._high)
def __cmp__(self, other):
r = mx.compare(self._low, other._low)
if r != 0:
return r
if self._high is None:
if other._high is None:
return 0
# self has no high bound, other does
return 1
elif other._high is None:
# other has no high bound, self does
return -1
return mx.compare(self._high, other._high)
def __hash__(self):
return self._low ** (self._high or 1)
def __contains__(self, other):
if isinstance(other, int):
value = int(other)
if value < self._low:
return False
if self._high is None:
return True
return value <= self._high
return False
def _values(self, stop=None):
"""
Returns an iterator over all the Java versions in this range stopping at `stop - 1`.
If `stop` is None and this is an open ended range, this will generate an infinite sequence.
"""
if self._high is None:
if stop is None:
return itertools.count(self._low)
return iter(range(self._low, stop))
return iter(range(self._low, self._high + 1))
def __init__(self, spec, parse_error=None, context=None):
"""
Creates a JavaCompliance based on `spec`.
:param spec: an int specifying a Java version or a str specifying one or more Java versions
:param parse_error: if not None, then it must be a callable that will be called if
`spec` is not a valid compliance specification. It will be called with
an error message and it must raise an exception (i.e. it cannot return
normally). If None, then `mx.abort` is called.
:param context: the context argument if `mx.abort` is called
"""
if parse_error is None:
parse_error = lambda m: mx.abort(m, context=context)
self._loom = False
def _error(part, index, msg):
parse_error('JavaCompliance("{}"): Part {} ("{}") {}'.format(spec, index, part, msg))
def _check_value(value, value_desc='value'):
value = int(value)
if value < 2:
_error(value, 0, 'has unsupported {} since it is less than 2'.format(value_desc))
return value
int_spec = spec if isinstance(spec, int) else int(spec) if isinstance(spec, str) and JavaCompliance._int_re.match(spec) else None
if int_spec is not None:
value = _check_value(spec)
self._parts = (JavaCompliance._Range(value, value),)
return
if not isinstance(spec, str):
spec = str(spec)
parts = spec.split(',')
def _parse_part(part, index):
def _part_error(msg):
_error(part, index, msg)
def _check_part_value(prefix, value, value_desc):
value = _check_value(value, value_desc)
if prefix and value > 9:
_part_error('cannot have "1." prefix on {} since {} > 9'.format(value_desc, value_desc))
return value
m = JavaCompliance._version_range_re.match(part)
if m:
low = _check_part_value(m.group(1), m.group(2), 'low bound')
high = _check_part_value(m.group(3), m.group(4), 'high bound')
if low >= high:
_part_error('has low bound ({}) greater or equal to high bound ({})'.format(low, high))
return JavaCompliance._Range(low, high)
m = JavaCompliance._open_range_re.match(part)
if m:
low = _check_part_value(m.group(1), m.group(2), 'bound')
return JavaCompliance._Range(low, None)
m = JavaCompliance._loom_re.match(part)
if m:
self._loom = True
part = m.group(1)
m = JavaCompliance._singleton_range_re.match(part)
if m:
low = _check_part_value(m.group(1), m.group(2), 'bound')
return JavaCompliance._Range(low, low)
_part_error('is not a recognized version range')
self._parts = tuple((_parse_part(parts[i], i) for i in range(len(parts))))
if len(self._parts) > 1:
for i in range(1, len(self._parts)):
first = self._parts[i - 1]
second = self._parts[i]
if first._high is None:
_error(first, i - 1, 'must have a high bound')
if second._low <= first._high:
_error(first, i - 1, 'must have a high bound ({}) less than the low bound ({}) of part {} ("{}")'.format(first._high, second._low, i, second))
@property
def value(self):
return self._parts[0]._low
def __str__(self):
if self.value >= 9:
return str(self.value)
return '1.' + str(self.value)
def __repr__(self):
return ','.join((repr(b) for b in self._parts))
def _high_bound(self):
return self._parts[-1]._high
def __cmp__(self, other):
if isinstance(other, str):
other = JavaCompliance(other)
return mx.compare(self._parts, other._parts)
def __contains__(self, other):
if isinstance(other, (int, str)):
other = JavaCompliance(other)
assert other._high_bound() is not None, "Contains check cannot be done with version ranges"
r = mx.compare(self.value, other.value)
if r == 0:
return True
elif r > 0:
return False
else: # r < 0
if self._high_bound() is None:
return True
else:
return mx.compare(self._high_bound(), other.value) >= 0
def __hash__(self):
return hash((self._parts, self._loom))
def _is_exact_bound(self):
return self.value == self._high_bound()
def _exact_match(self, version):
assert isinstance(version, mx.VersionSpec)
if self._loom and not version._loom:
# only skip those suites who require Loom
return False
if len(version.parts) > 0:
if len(version.parts) > 1 and version.parts[0] == 1:
# First part is a '1', e.g. '1.8.0'.
value = version.parts[1]
else:
# No preceding '1', e.g. '9-ea'. Used for Java 9 early access releases.
value = version.parts[0]
return any((value in b for b in self._parts))
return False
def as_version_check(self):
if self._is_exact_bound():
versionDesc = str(self)
elif self._high_bound() is None:
versionDesc = '>=' + str(self)
else:
versionDesc = 'in ' + repr(self)
versionCheck = self._exact_match
return (versionCheck, versionDesc)
def _values(self, stop=None):
"""
Returns an iterator over all the Java versions that match this compliance object
up to but not including `stop`. If `stop` is None and this is an open ended
compliance, this will generate an infinite sequence.
"""
return itertools.chain(*(p._values(stop=stop) for p in self._parts))
def highest_specified_value(self):
"""
Gets the highest explicitly specified value of this Java compliance.
Examples:
8+ --> 8
8,13+ --> 13
8,11,13+ --> 13
8..11,13+ --> 13
"""
highest_part = self._parts[-1]
return highest_part._high or highest_part._low
def _test():
"""
Mx suite specific tests.
"""
# JavaCompliance tests
good_specs = [
(2, True),
(1.2, True),
(11, True),
(200, True),
('2', True),
('1.2', True),
('1.8', True),
('1.5+', False),
('2..4', False),
('1.8..9', False),
('2..3,4+', False),
('2..3,4,7+', False),
('2..3,4..5,7+', False),
('2..3,4..5,7,8,9,10,15..18,120', False),
]
bad_specs = [
1,
'1',
'1.1',
'1.10',
'1.8..1.10',
'1.10+',
'2..1',
'2..2',
'1,,3',
'1..3+',
'1+,4..5',
'13+ignored',
'1..3,7..5',
'4,7,1..3,',
'4..5,1..3',
]
for spec, exact in good_specs:
p = mx.JavaCompliance(spec)
assert p._is_exact_bound() is exact, p
# Just ensure these methods execute without exception
p.as_version_check()
p._values(stop=20)
hash(p)
if mx.get_opts().verbose:
if isinstance(spec, str):
spec = '"' + spec + '"'
mx.log('{}: str="{}", repr="{}", hash={}'.format(spec, str(p), repr(p), hash(p)))
for spec in bad_specs:
class SpecError(Exception):
pass
def _parse_error(msg):
if mx.get_opts().verbose:
mx.log('saw expected SpecError: ' + msg)
raise SpecError(msg)
try:
mx.JavaCompliance(spec, parse_error=_parse_error)
mx.abort('expected SpecError while parsing "{}"'.format(spec))
except SpecError:
pass