forked from graalvm/mx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mx_gate.py
435 lines (375 loc) · 17.4 KB
/
mx_gate.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#
# ----------------------------------------------------------------------------------------------------
#
# Copyright (c) 2007, 2015, 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 os, re, time, datetime
from os.path import join, exists
from argparse import ArgumentParser
import xml.dom.minidom
import mx
"""
Context manager for a single gate task that can prevent the
task from executing or time and log its execution.
"""
class Task:
# None or a list of strings. If not None, only tasks whose title
# matches at least one of the substrings in this list will return
# a non-None value from __enter__. The body of a 'with Task(...) as t'
# statement should check 't' and exit immediately if it is None.
filters = None
dryRun = False
startAtFilter = None
filtersExclude = False
def __init__(self, title, tasks=None, disableJacoco=False):
self.tasks = tasks
self.title = title
self.skipped = False
if tasks is not None:
for t in tasks:
if t.title == title:
mx.abort('Gate task with title "' + title + '" is already defined')
if Task.startAtFilter:
assert not Task.filters
if Task.startAtFilter in title:
self.skipped = False
Task.startAtFilter = None
else:
self.skipped = True
elif Task.filters:
if Task.filtersExclude:
self.skipped = any([f in title for f in Task.filters])
else:
self.skipped = not any([f in title for f in Task.filters])
if not self.skipped:
self.start = time.time()
self.end = None
self.duration = None
self.disableJacoco = disableJacoco
mx.log(time.strftime('gate: %d %b %Y %H:%M:%S: BEGIN: ') + title)
def __enter__(self):
assert self.tasks is not None, "using Task with 'with' statement requires to pass the tasks list in the constructor"
if self.skipped:
return None
if self.disableJacoco:
self.jacacoSave = _jacoco
if Task.dryRun:
return None
return self
def __exit__(self, exc_type, exc_value, traceback):
if not self.skipped:
self.tasks.append(self.stop())
if self.disableJacoco:
global _jacoco
_jacoco = self.jacacoSave
@staticmethod
def _human_fmt(num):
for unit in ['', 'K', 'M', 'G']:
if abs(num) < 1024.0:
return "%3.1f%sB" % (num, unit)
num /= 1024.0
return "%.1fTB" % (num)
@staticmethod
def _diskstats():
if hasattr(os, 'statvfs'):
_, f_frsize, f_blocks, _, f_bavail, _, _, _, _, _ = os.statvfs(os.getcwd())
total = f_frsize * f_blocks
free = f_frsize * f_bavail
return ' [disk (free/total): {}/{}]'.format(Task._human_fmt(free), Task._human_fmt(total))
return ''
def stop(self):
self.end = time.time()
self.duration = datetime.timedelta(seconds=self.end - self.start)
mx.log(time.strftime('gate: %d %b %Y %H:%M:%S: END: ') + self.title + ' [' + str(self.duration) + ']' + Task._diskstats())
return self
def abort(self, codeOrMessage):
self.end = time.time()
self.duration = datetime.timedelta(seconds=self.end - self.start)
mx.log(time.strftime('gate: %d %b %Y %H:%M:%S: ABORT: ') + self.title + ' [' + str(self.duration) + ']' + Task._diskstats())
mx.abort(codeOrMessage)
return self
_gate_runners = []
_extra_gate_arguments = []
def add_gate_argument(*args, **kwargs):
"""
Adds an argument declaration to the ArgumentParser used by the gate method.
"""
_extra_gate_arguments.append((args, kwargs))
def add_gate_runner(suite, runner):
"""
Adds a gate runner function for a given suite to be called by the gate once common gate tasks
have been executed. The 'runner' function is called with these arguments:
args: the argparse.Namespace object containing result of parsing gate command line
tasks: list of Task to which extra Tasks should be added
"""
suiteRunner = (suite, runner)
_gate_runners.append(suiteRunner)
def add_omit_clean_args(parser):
parser.add_argument('-j', '--omit-java-clean', action='store_false', dest='cleanJava', help='omit cleaning Java native code')
parser.add_argument('-n', '--omit-native-clean', action='store_false', dest='cleanNative', help='omit cleaning and building native code')
parser.add_argument('-e', '--omit-ide-clean', action='store_false', dest='cleanIDE', help='omit ideclean/ideinit')
parser.add_argument('-d', '--omit-dist-clean', action='store_false', dest='cleanDist', help='omit cleaning distributions')
parser.add_argument('-o', '--omit-clean', action='store_true', dest='noClean', help='equivalent to -j -n -e')
def gate_clean(cleanArgs, tasks, name='Clean'):
with Task(name, tasks) as t:
if t:
mx.command_function('clean')(cleanArgs)
def check_gate_noclean_arg(args):
'''
Checks the -o option (noClean) and sets the sub-options in args appropriately
and returns the relevant args for the clean command (N.B. IDE currently ignored).
'''
if args.noClean:
args.cleanIDE = False
args.cleanJava = False
args.cleanNative = False
args.cleanDist = False
cleanArgs = []
if not args.cleanNative:
cleanArgs.append('--no-native')
if not args.cleanJava:
cleanArgs.append('--no-java')
if not args.cleanDist:
cleanArgs.append('--no-dist')
return cleanArgs
def _warn_or_abort(msg, strict_mode):
reporter = mx.abort if strict_mode else mx.warn
reporter(msg)
def gate(args):
"""run the tests used to validate a push
If this command exits with a 0 exit code, then the gate passed."""
parser = ArgumentParser(prog='mx gate')
add_omit_clean_args(parser)
parser.add_argument('--all-suites', action='store_true', help='run gate tasks for all suites, not just the primary suite')
parser.add_argument('--dry-run', action='store_true', help='just show the tasks that will be run without running them')
parser.add_argument('-x', action='store_true', help='makes --task-filter an exclusion instead of inclusion filter')
parser.add_argument('--jacocout', help='specify the output directory for jacoco report')
parser.add_argument('--strict-mode', action='store_true', help='abort if a task cannot be executed due to missing tool configuration')
filtering = parser.add_mutually_exclusive_group()
filtering.add_argument('-t', '--task-filter', help='comma separated list of substrings to select subset of tasks to be run')
filtering.add_argument('-s', '--start-at', help='substring to select starting task')
for a, k in _extra_gate_arguments:
parser.add_argument(*a, **k)
args = parser.parse_args(args)
cleanArgs = check_gate_noclean_arg(args)
global _jacoco
if args.dry_run:
Task.dryRun = True
if args.start_at:
Task.startAtFilter = args.start_at
elif args.task_filter:
Task.filters = args.task_filter.split(',')
Task.filtersExclude = args.x
elif args.x:
mx.abort('-x option cannot be used without --task-filter option')
tasks = []
total = Task('Gate')
try:
with Task('Versions', tasks) as t:
if t:
mx.command_function('version')(['--oneline'])
mx.command_function('sversions')([])
with Task('JDKReleaseInfo', tasks) as t:
if t:
jdkDirs = os.pathsep.join([mx.get_env('JAVA_HOME', ''), mx.get_env('EXTRA_JAVA_HOMES', '')])
for jdkDir in jdkDirs.split(os.pathsep):
release = join(jdkDir, 'release')
if exists(release):
mx.log('==== ' + jdkDir + ' ====')
with open(release) as fp:
mx.log(fp.read().strip())
with Task('Pylint', tasks) as t:
if t:
if mx.command_function('pylint')(['--primary']) != 0:
_warn_or_abort('Pylint not configured correctly. Cannot execute Pylint task.', args.strict_mode)
gate_clean(cleanArgs, tasks)
with Task('Distribution Overlap Check', tasks) as t:
if t:
if mx.command_function('checkoverlap')([]) != 0:
t.abort('Found overlapping distributions.')
with Task('Canonicalization Check', tasks) as t:
if t:
mx.log(time.strftime('%d %b %Y %H:%M:%S - Ensuring mx/projects files are canonicalized...'))
if mx.command_function('canonicalizeprojects')([]) != 0:
t.abort('Rerun "mx canonicalizeprojects" and check-in the modified mx/suite*.py files.')
with Task('BuildJavaWithEcj', tasks) as t:
if t:
if mx.get_env('JDT'):
mx.command_function('build')(['-p', '--no-native', '--warning-as-error'])
gate_clean(cleanArgs, tasks, name='CleanAfterEcjBuild')
else:
_warn_or_abort('JDT environment variable not set. Cannot execute BuildJavaWithEcj task.', args.strict_mode)
with Task('BuildJavaWithJavac', tasks) as t:
if t: mx.command_function('build')(['-p', '--warning-as-error', '--no-native', '--force-javac'])
with Task('IDEConfigCheck', tasks) as t:
if t:
if args.cleanIDE:
mx.command_function('ideclean')([])
mx.command_function('ideinit')([])
with Task('CodeFormatCheck', tasks) as t:
if t:
eclipse_exe = mx.get_env('ECLIPSE_EXE')
if eclipse_exe is not None:
if mx.command_function('eclipseformat')(['-e', eclipse_exe, '--primary']) != 0:
t.abort('Formatter modified files - run "mx eclipseformat", check in changes and repush')
else:
_warn_or_abort('ECLIPSE_EXE environment variable not set. Cannot execute CodeFormatCheck task.', args.strict_mode)
with Task('Checkstyle', tasks) as t:
if t and mx.command_function('checkstyle')(['--primary']) != 0:
t.abort('Checkstyle warnings were found')
with Task('Checkheaders', tasks) as t:
if t and mx.command_function('checkheaders')([]) != 0:
t.abort('Checkheaders warnings were found')
with Task('FindBugs', tasks) as t:
if t and mx.command_function('findbugs')([]) != 0:
t.abort('FindBugs warnings were found')
if exists('jacoco.exec'):
os.unlink('jacoco.exec')
if args.jacocout is not None:
_jacoco = 'append'
else:
_jacoco = 'off'
for suiteRunner in _gate_runners:
suite, runner = suiteRunner
if args.all_suites or suite is mx.primary_suite():
runner(args, tasks)
if args.jacocout is not None:
mx.command_function('jacocoreport')([args.jacocout])
_jacoco = 'off'
except KeyboardInterrupt:
total.abort(1)
except BaseException as e:
import traceback
traceback.print_exc()
total.abort(str(e))
total.stop()
mx.log('Gate task times:')
for t in tasks:
mx.log(' ' + str(t.duration) + '\t' + t.title)
mx.log(' =======')
mx.log(' ' + str(total.duration))
if args.task_filter:
Task.filters = None
def checkheaders(args):
"""check Java source headers against any required pattern"""
failures = {}
for p in mx.projects():
if not p.isJavaProject():
continue
csConfig = join(mx.project(p.checkstyleProj).dir, '.checkstyle_checks.xml')
if not exists(csConfig):
mx.log('Cannot check headers for ' + p.name + ' - ' + csConfig + ' does not exist')
continue
dom = xml.dom.minidom.parse(csConfig)
for module in dom.getElementsByTagName('module'):
if module.getAttribute('name') == 'RegexpHeader':
for prop in module.getElementsByTagName('property'):
if prop.getAttribute('name') == 'header':
value = prop.getAttribute('value')
matcher = re.compile(value, re.MULTILINE)
for sourceDir in p.source_dirs():
for root, _, files in os.walk(sourceDir):
for name in files:
if name.endswith('.java') and name != 'package-info.java':
f = join(root, name)
with open(f) as fp:
content = fp.read()
if not matcher.match(content):
failures[f] = csConfig
for n, v in failures.iteritems():
mx.log('{0}: header does not match RegexpHeader defined in {1}'.format(n, v))
return len(failures)
_jacoco = 'off'
_jacoco_includes = []
def add_jacoco_includes(patterns):
"""
Adds to the list of JaCoCo includes.
"""
_jacoco_includes.extend(patterns)
_jacoco_excluded_annotations = ['@Test']
def add_jacoco_excluded_annotations(annotations):
"""
Adds to the list of annotations which if present denote a class that should
be excluded from JaCoCo analysis.
"""
_jacoco_excluded_annotations.extend(annotations)
def get_jacoco_agent_args():
'''
Gets the args to be added to a VM command line for injecting the JaCoCo agent
if use of JaCoCo has been requested otherwise returns None.
'''
if _jacoco == 'on' or _jacoco == 'append':
jacocoagent = mx.library("JACOCOAGENT", True)
includes = list(_jacoco_includes)
baseExcludes = []
for p in mx.projects():
projsetting = getattr(p, 'jacoco', '')
if projsetting == 'exclude':
baseExcludes.append(p.name)
if projsetting == 'include':
includes.append(p.name + '.*')
def _filter(l):
# filter out specific classes which are already covered by a baseExclude package
return [clazz for clazz in l if not any([clazz.startswith(package) for package in baseExcludes])]
excludes = []
for p in mx.projects():
if p.isJavaProject():
excludes += _filter(p.find_classes_with_annotations(None, _jacoco_excluded_annotations, includeInnerClasses=True).keys())
excludes += _filter(p.find_classes_with_matching_source_line(None, lambda line: 'JaCoCo Exclude' in line, includeInnerClasses=True).keys())
excludes += [package + '.*' for package in baseExcludes]
agentOptions = {
'append' : 'true' if _jacoco == 'append' else 'false',
'bootclasspath' : 'true',
'includes' : ':'.join(includes),
'excludes' : ':'.join(excludes),
'destfile' : 'jacoco.exec'
}
return ['-javaagent:' + jacocoagent.get_path(True) + '=' + ','.join([k + '=' + v for k, v in agentOptions.items()])]
return None
def jacocoreport(args):
"""create a JaCoCo coverage report
Creates the report from the 'jacoco.exec' file in the current directory.
Default output directory is 'coverage', but an alternative can be provided as an argument."""
jacocoreport = mx.library("JACOCOREPORT", True)
out = 'coverage'
if len(args) == 1:
out = args[0]
elif len(args) > 1:
mx.abort('jacocoreport takes only one argument : an output directory')
includes = list(_jacoco_includes)
for p in mx.projects():
projsetting = getattr(p, 'jacoco', '')
if projsetting == 'include' or projsetting == '':
includes.append(p.name)
includedirs = set()
for p in mx.projects():
projsetting = getattr(p, 'jacoco', '')
if projsetting == 'exclude':
continue
for include in includes:
if include in p.dir:
includedirs.add(p.dir)
for i in includedirs:
bindir = i + '/bin'
mx.ensure_dir_exists(bindir)
mx.run_java(['-jar', jacocoreport.get_path(True), '--in', 'jacoco.exec', '--out', out] + sorted(includedirs))