forked from graalvm/mx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mx_gate.py
1350 lines (1183 loc) · 57.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# ----------------------------------------------------------------------------------------------------
#
# Copyright (c) 2007, 2021, 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.
#
# ----------------------------------------------------------------------------------------------------
from __future__ import print_function
import os, re, time, datetime, json
import tempfile
import atexit
import zipfile
from os.path import join, exists
from argparse import ArgumentParser
import mx
import mx_javacompliance
import sys
from mx_urlrewrites import rewriteurl
"""
Predefined Task tags.
"""
class Tags:
always = 'always' # special tag that is always implicitly selected
style = 'style' # code style checks (without build)
build = 'build' # build
ecjbuild = 'ecjbuild' # build with ecj only
fullbuild = 'fullbuild' # full build (including warnings, spotbugs and ide init)
"""
Context manager for a single gate task that can prevent the
task from executing or time and log its execution.
"""
class Task:
# A list of strings. If not empty, 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 = []
log = True # whether to log task messages
dryRun = False
startAtFilter = None
filtersExclude = False
tags = None
tagsExclude = False
# map from tag to a pair [from(inclusive), to(exclusive)]
tags_range = dict()
# map from tag to count
tags_count = dict()
verbose = False
startTime = None
def tag_matches(self, _tags):
for t in _tags:
assert isinstance(t, str), '{} is not a string and thus not a valid tag'.format(t)
if Task.tags is not None and t in Task.tags: # pylint: disable=unsupported-membership-test
if t not in Task.tags_range:
# no range restriction
return True
else:
frm, to = Task.tags_range[t]
cnt = Task.tags_count[t]
# increment counter
Task.tags_count[t] += 1
if frm <= cnt < to:
return True
return False
def _timestamp(self, suffix):
stamp = time.strftime('gate: %d %b %Y %H:%M:%S')
if Task.startTime:
duration = datetime.timedelta(seconds=time.time() - Task.startTime)
# Strip microseconds and convert to a string
duration = str(duration - datetime.timedelta(microseconds=duration.microseconds))
# Strip hours if 0
if duration.startswith('0:'):
duration = duration[2:]
stamp += '(+{})'.format(duration)
return stamp + suffix
def __init__(self, title, tasks=None, disableJacoco=False, tags=None, legacyTitles=None, description=None):
self.tasks = tasks
self.title = title
self.legacyTitles = legacyTitles or []
self.skipped = False
self.tags = tags
self.description = description
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 len(Task.filters) > 0:
titles = [self.title] + self.legacyTitles
if Task.filtersExclude:
self.skipped = any([f in t for t in titles for f in Task.filters])
else:
self.skipped = not any([f in t for t in titles for f in Task.filters])
if Task.tags is not None:
if Task.tagsExclude:
self.skipped = all([t in Task.tags for t in self.tags]) if tags else False # pylint: disable=unsupported-membership-test
else:
_tags = self.tags if self.tags else []
self.skipped = not self.tag_matches(_tags)
if not self.skipped:
self.start = time.time()
self.end = None
self.duration = None
self.disableJacoco = disableJacoco
if Task.log:
mx.log(self._timestamp(' 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):
if Task.log:
self.end = time.time()
self.duration = datetime.timedelta(seconds=self.end - self.start)
mx.log(self._timestamp(' END: ') + self.title + ' [' + str(self.duration) + ']' + Task._diskstats())
return self
def abort(self, codeOrMessage):
if Task.log:
self.end = time.time()
self.duration = datetime.timedelta(seconds=self.end - self.start)
mx.log(self._timestamp(' ABORT: ') + self.title + ' [' + str(self.duration) + ']' + Task._diskstats())
mx.abort(codeOrMessage)
return self
def __repr__(self):
return "Task: " + self.title
_gate_runners = []
_pre_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 prepend_gate_runner(suite, runner):
"""
Prepends a gate runner function for a given suite to be called by the gate before common gate tasks
are 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)
_pre_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('--omit-clean-all', action='store_false', dest='cleanAll', help='omit cleaning non-default build targets')
parser.add_argument('-o', '--omit-clean', action='store_true', dest='noClean', help='equivalent to -j -n -e')
def gate_clean(cleanArgs, tasks, name='Clean', tags=None):
with Task(name, tasks, tags=tags) 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
args.cleanAll = 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')
if args.cleanAll:
cleanArgs.append('--all')
return cleanArgs
def _warn_or_abort(msg, strict_mode):
reporter = mx.abort if strict_mode else mx.warn
reporter(msg)
def parse_tags_argument(tags_arg, exclude):
pattern = re.compile(r"^(?P<tag>[^:]*)(?::(?P<from>\d+):(?P<to>\d+)?)?$")
tags = tags_arg.split(',')
Task.tags = []
for tag_spec in tags:
m = pattern.match(tag_spec)
if not m:
mx.abort('--tags option requires the format `name[:from:[to]]`: {0}'.format(tag_spec))
(tag, t_from, t_to) = m.groups()
if t_from:
if exclude:
mx.abort('-x option cannot be used tag ranges: {0}'.format(tag_spec))
frm = int(t_from)
to = int(t_to) if t_to else sys.maxsize
# insert range tuple
Task.tags_range[tag] = (frm, to)
# sanity check
if to <= frm:
mx.abort('`from` must be less than `to` for tag ranges: {0}'.format(tag_spec))
# init counter
Task.tags_count[tag] = 0
Task.tags.append(tag)
_command_level = 0
def gate(args):
"""run the tests used to validate a push
If this command exits with a 0 exit code, then the gate passed."""
default_summary = ['duration', 'title', 'description', 'tags']
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 or --tags an exclusion instead of inclusion filter')
jacoco = parser.add_mutually_exclusive_group()
jacoco.add_argument('--jacocout', help='specify the output directory for jacoco report')
jacoco.add_argument('--jacoco-zip', help='specify the output zip file for jacoco report')
parser.add_argument('--jacoco-omit-excluded', action='store_true', help='omit excluded files from jacoco report')
parser.add_argument('--strict-mode', action='store_true', help='abort if a task cannot be executed due to missing tool configuration')
parser.add_argument('--no-warning-as-error', action='store_true', help='compile warnings are not treated as errors')
parser.add_argument('-B', dest='extra_build_args', action='append', metavar='<build_args>', help='append additional arguments to mx build commands used in the gate')
parser.add_argument('-p', '--partial', help='run only a subset of the tasks in the gate (index/total). Eg. "--partial 2/5" runs the second fifth of the tasks in the gate. Tasks with tag build are repeated for each run.')
summary = parser.add_mutually_exclusive_group()
summary.add_argument('--summary', action='store_const', const=default_summary, default=None, help='print a human readable summary of the executed tasks')
summary.add_argument('--summary-format', dest='summary', action='store', default=None, help='--summary with a comma separated list of entries. Possible values ' + str(default_summary))
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')
filtering.add_argument('--tags', help='comma separated list of tags to select subset of tasks to be run. Tags can have a range specifier `name[:from:[to]]`.'
'If present only the [from,to) tasks are executed. If `to` is omitted all tasks starting with `from` are executed.')
for a, k in _extra_gate_arguments:
parser.add_argument(*a, **k)
args = parser.parse_args(args)
cleanArgs = check_gate_noclean_arg(args)
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.tags:
parse_tags_argument(args.tags, args.x)
Task.tagsExclude = args.x
if not Task.tagsExclude:
# implicitly include 'always'
Task.tags += [Tags.always]
elif args.x:
mx.abort('-x option cannot be used without --task-filter or the --tags option')
if args.jacoco_zip:
args.jacocout = 'html'
if not args.extra_build_args:
args.extra_build_args = []
if args.partial:
partialArgs = args.partial.split('/')
if len(partialArgs) != 2:
mx.abort('invalid partial argument specified')
selected = int(partialArgs[0]) - 1
total = int(partialArgs[1])
if selected < 0 or selected >= total:
mx.abort('out of bounds partial argument specified')
tasks = _collect_tasks(cleanArgs, args)
# build and always tags must be run by every partial gate run
alwaysTags = [Tags.always, Tags.build]
buildTasks = [task for task in tasks if not task.skipped and any([f in t for t in alwaysTags for f in task.tags])]
nonBuildTasks = [task for task in tasks if not task.skipped and not any([f in t for t in alwaysTags for f in task.tags])]
partialTasks = nonBuildTasks[selected::total]
runTaskNames = [task.title for task in buildTasks + partialTasks]
# we have already ran the filters in the dry run when collecting
# so we can safely overwrite other filter settings.
Task.filters = runTaskNames
Task.filtersExclude = False
mx.log('Running gate with partial tasks ' + args.partial + ". " + str(len(partialTasks)) + " out of " + str(len(nonBuildTasks)) + " non-build tasks selected.")
if len(partialTasks) == 0:
mx.log('No partial tasks left to run. Finishing gate early.')
return
Task.startTime = time.time()
tasks = []
total = Task('Gate')
all_commands = []
def mx_command_entered(command, *args, **kwargs):
global _command_level
if _command_level == 0:
gate_command_str = command_in_gate_message(command.command, args, kwargs)
# store the formatted gate command as the command might modify args/kwargs
all_commands.append(gate_command_str)
mx.log(mx.colorize('Running: ' + gate_command_str, color='blue'))
_command_level = _command_level + 1
def mx_command_left(_, *__, **___):
global _command_level
assert _command_level >= 0
_command_level = _command_level - 1
def print_commands_on_failure():
sys.stdout.flush()
sys.stderr.flush()
mx.log_error('\nThe sequence of mx commands that were executed until the failure follows:\n')
for gate_command_str in all_commands:
mx.log_error(gate_command_str)
mx.log_error('\nIf the previous sequence is incomplete or some commands were executed programmatically use:\n')
mx.log_error(mx.current_mx_command() + '\n')
sys.stderr.flush()
def command_in_gate_message(command, command_args, kwargs):
one_list = len(command_args) == 1 and isinstance(command_args[0], list)
kwargs_absent = len(kwargs) == 0
if one_list and kwargs_absent: # gate command reproducible on the command line
message = mx.current_mx_command([command] + command_args[0])
else:
args_message = '(Programmatically executed. '
if not one_list:
args_message += 'Args: ' + str(command_args)
if not kwargs_absent:
args_message += 'Kwargs: ' + str(kwargs)
args_message += ')'
message = mx.current_mx_command([command, args_message])
return message
try:
mx._mx_commands.add_command_callback(mx_command_entered, mx_command_left)
_run_gate(cleanArgs, args, tasks)
except KeyboardInterrupt:
total.abort(1)
except BaseException as e:
import traceback
traceback.print_exc()
print_commands_on_failure()
total.abort(str(e))
finally:
mx._mx_commands.remove_command_callback(mx_command_entered, mx_command_left)
total.stop()
if args.summary:
mx.log('Gate task summary:')
res = [{'duration': str(t.duration), 'title': t.title, 'tags': '[{}]'.format(', '.join(t.tags)) if t.tags else '', 'description': t.description or ''} for t in tasks]
# collect lengths
maxLengths = {}
for e in res:
for key in e.keys():
maxLengths[key + 'Max'] = max(maxLengths.get(key + 'Max', 0), len(e[key]))
# build format string
fmt = ' '
args_summary = args.summary
if not isinstance(args_summary, list):
args_summary = args_summary.split(',')
if args.dry_run:
args_summary.remove('duration')
for entry in args_summary:
if entry + 'Max' in maxLengths:
fmt += ' {{{0}:<{{{0}Max}}}}'.format(entry)
else:
mx.abort('Unknown entry supplied to `mx gate --summary-format`: {}\n'
'Known entries are: {}'.format(entry, ', '.join(default_summary)))
for e in res:
# Python >= 3.5 could use {**e, **maxLengths} directly
values = e.copy()
values.update(maxLengths)
mx.log(fmt.format(**values))
else:
mx.log('Gate task times:')
for t in tasks:
mx.log(' ' + str(t.duration) + '\t' + t.title + ("" if not (Task.verbose and t.tags) else (' [' + ','.join(t.tags) + ']')))
mx.log(' =======')
mx.log(' ' + str(total.duration))
if args.task_filter:
Task.filters = []
def _collect_tasks(cleanArgs, args):
prevDryRun = Task.dryRun
prevLog = Task.log
Task.dryRun = True
Task.log = False
tasks = []
try:
_run_gate(cleanArgs, args, tasks)
finally:
Task.dryRun = prevDryRun
Task.log = prevLog
return tasks
def _run_mx_suite_tests():
"""
Mx suite specific tests.
"""
mx_javacompliance._test()
if mx.is_windows():
def win(s, min_length=0):
extra = min_length - len(s)
if extra > 0:
padding = 'X' * extra
s += padding
return s.replace('/', '\\')
def _test(value, expect, open_fp):
actual = mx._safe_path(value)
if actual != expect:
nl = os.linesep
assert False, 'Failed safe_path test{} input: {} (len={}){}expect: {} (len={}){}actual: {} (len={})'.format(nl,
value, len(value), nl,
expect, len(expect), nl,
actual, len(actual))
if open_fp and value != open_fp.name:
try:
with mx.open(value, 'w') as fp:
fp.write('blah')
with mx.open(value, 'r') as fp:
contents = fp.read()
assert contents == 'blah', contents
finally:
if os.path.exists(value):
os.unlink(value)
with tempfile.NamedTemporaryFile(prefix="safe_path_test", mode="w") as fp:
cases = {
win('C:/Home/mydir') : win('C:/Home/mydir'),
win('C:/Home/mydir', 258) : win('C:/Home/mydir', 258),
win('C:/Home/mydir', 259) : win('//?/') + win('C:/Home/mydir', 259),
win('C:/Home/mydir', 260) : win('//?/') + win('C:/Home/mydir', 260),
win('//Mac/Home/mydir') : win('//Mac/Home/mydir'),
win('//Mac/Home/mydir', 258) : win('//Mac/Home/mydir', 258),
win('//Mac/Home/mydir', 259) : win('//?/UNC/') + win('Mac/Home/mydir', 257),
win('//Mac/Home/mydir', 260) : win('//?/UNC/') + win('Mac/Home/mydir', 258),
win(fp.name) : win(fp.name),
win(fp.name, 258) : win(fp.name, 258),
win(fp.name, 259) : win('//?/') + win(fp.name, 259),
win(fp.name, 260) : win('//?/') + win(fp.name, 260),
}
for value, expect in cases.items():
_test(value, expect, fp if value.startswith(fp.name) else None)
def _run_gate(cleanArgs, args, tasks):
global _jacoco
with Task('Versions', tasks, tags=[Tags.always]) as t:
if t:
mx.command_function('version')(['--oneline'])
mx.command_function('sversions')([])
mx.log("Python version: {}".format(sys.version_info))
with Task('JDKReleaseInfo', tasks, tags=[Tags.always]) 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())
if mx.primary_suite() is mx._mx_suite:
with Task('MxTests', tasks, tags=[Tags.always]) as t:
if t:
_run_mx_suite_tests()
with Task('VerifyMultiReleaseProjects', tasks, tags=[Tags.always]) as t:
if t:
mx.command_function('verifymultireleaseprojects')([])
for suiteRunner in _pre_gate_runners:
suite, runner = suiteRunner
if args.all_suites or suite is mx.primary_suite():
runner(args, tasks)
with Task('Pylint', tasks, tags=[Tags.style]) 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, tags=[Tags.build, Tags.fullbuild, Tags.ecjbuild])
with Task('Distribution Overlap Check', tasks, tags=[Tags.style]) as t:
if t:
if mx.command_function('checkoverlap')([]) != 0:
t.abort('Found overlapping distributions.')
with Task('Canonicalization Check', tasks, tags=[Tags.style]) 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 modify the suite.py files as suggested.')
with Task('Verify Java Sources in Project', tasks, tags=[Tags.style]) as t:
if t:
mx.log(time.strftime('%d %b %Y %H:%M:%S - Ensuring all Java sources are in a Java project directory...'))
if mx.command_function('verifysourceinproject')([]) != 0:
t.abort('Move or delete the Java sources that are not in a Java project directory.')
if mx._is_supported_by_jdt(mx.DEFAULT_JDK_TAG):
with Task('BuildWithEcj', tasks, tags=[Tags.fullbuild, Tags.ecjbuild], legacyTitles=['BuildJavaWithEcj']) as t:
if t:
defaultBuildArgs = ['-p']
fullbuild = True if Task.tags is None else Tags.fullbuild in Task.tags # pylint: disable=unsupported-membership-test
# Using ecj alone is not compatible with --warning-as-error (see GR-3969)
if not args.no_warning_as_error and fullbuild:
defaultBuildArgs += ['--warning-as-error']
if mx.get_env('JDT'):
mx.command_function('build')(defaultBuildArgs + args.extra_build_args)
if fullbuild:
gate_clean(cleanArgs, tasks, name='CleanAfterEcjBuild', tags=[Tags.fullbuild])
else:
_warn_or_abort('JDT environment variable not set. Cannot execute BuildWithEcj task.', args.strict_mode)
with Task('BuildWithJavac', tasks, tags=[Tags.build, Tags.fullbuild], legacyTitles=['BuildJavaWithJavac']) as t:
if t:
defaultBuildArgs = ['-p']
if not args.no_warning_as_error:
defaultBuildArgs += ['--warning-as-error']
mx.command_function('build')(defaultBuildArgs + ['--force-javac'] + args.extra_build_args)
with Task('IDEConfigCheck', tasks, tags=[Tags.fullbuild]) as t:
if t:
if args.cleanIDE:
mx.command_function('ideclean')([])
mx.command_function('ideinit')([])
with Task('CodeFormatCheck', tasks, tags=[Tags.style]) 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, tags=[Tags.style]) as t:
if t and mx.command_function('checkstyle')(['--primary']) != 0:
t.abort('Checkstyle warnings were found')
with Task('SpotBugs', tasks, tags=[Tags.fullbuild]) as t:
if t and mx.command_function('spotbugs')([]) != 0:
t.abort('FindBugs warnings were found')
with Task('VerifyLibraryURLs', tasks, tags=[Tags.fullbuild]) as t:
if t:
mx.command_function('verifylibraryurls')([])
jacoco_exec = get_jacoco_dest_file()
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:
jacoco_args = [args.jacocout]
if args.jacoco_omit_excluded:
jacoco_args = ['--omit-excluded'] + jacoco_args
mx.command_function('jacocoreport')(jacoco_args)
_jacoco = 'off'
if args.jacoco_zip is not None:
mx.log('Creating JaCoCo report archive: {}'.format(args.jacoco_zip))
with zipfile.ZipFile(args.jacoco_zip, 'w', compression=zipfile.ZIP_DEFLATED) as zf:
zf.write(jacoco_exec, join(args.jacocout, jacoco_exec))
for root, _, files in os.walk(args.jacocout):
for f in files:
zf.write(os.path.join(root, f))
mx.log('Archiving done.')
def checkheaders(args):
"""check Java source headers against any required pattern"""
mx.log('The checkheaders command is obsolete. The checkstyle or checkcopyrights command performs\n'\
'the required checks depending on the mx configuration.')
return 0
JACOCO_EXEC = None
_jacoco = 'off'
_jacoco_includes = []
_jacoco_excludes = []
def add_jacoco_includes(patterns):
"""
Adds to the list of JaCoCo includes.
"""
global _jacoco_includes
global _jacoco_excludes
_jacoco_includes.extend(patterns)
# .* is explicit on include patterns so handle appropriately
for pattern in patterns:
if pattern.endswith('.*'):
_jacoco_excludes = [exclude for exclude in _jacoco_excludes if not exclude.startswith(pattern[:-2])]
else:
_jacoco_excludes = [exclude for exclude in _jacoco_excludes if exclude != pattern]
def add_jacoco_excludes(patterns):
"""
Adds to the list of JaCoCo excludes.
"""
global _jacoco_includes
global _jacoco_excludes
_jacoco_excludes.extend(patterns)
# .* is implicit on exclude patterns, but not include patterns
for pattern in patterns:
_jacoco_includes = [include for include in _jacoco_includes if not include.startswith(pattern)]
_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)
_jacoco_whitelisted_packages = []
def add_jacoco_whitelisted_packages(packages):
"""
Adds to the list of JaCoCo whitelisted packages.
"""
_jacoco_whitelisted_packages.extend(packages)
def _jacoco_is_package_whitelisted(package):
if not _jacoco_whitelisted_packages:
return True
return any(package.startswith(w) for w in _jacoco_whitelisted_packages)
def _jacoco_excludes_includes():
includes = list(_jacoco_includes)
baseExcludes = list(_jacoco_excludes)
for p in mx.projects():
if p.isJavaProject():
projsetting = getattr(p, 'jacoco', '')
if not _jacoco_is_package_whitelisted(p.name):
pass
elif projsetting == 'exclude':
baseExcludes.append(p.name)
elif projsetting == 'include':
includes.append(p.name + '.*')
if _jacoco_whitelisted_packages:
includes.extend((x + '.*' for x in _jacoco_whitelisted_packages))
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() and p.name not in baseExcludes and _jacoco_is_package_whitelisted(p.name):
excludes += _filter(
p.find_classes_with_annotations(None, _jacoco_excluded_annotations, includeInnerClasses=True,
includeGenSrc=True).keys())
excludes += _filter(p.find_classes_with_matching_source_line(None, lambda line: 'JaCoCo Exclude' in line,
includeInnerClasses=True,
includeGenSrc=True).keys())
excludes += [package + '.*' for package in baseExcludes]
return excludes, includes
def get_jacoco_dest_file():
return JACOCO_EXEC or mx.get_opts().jacoco_dest_file
def get_jacoco_agent_path(resolve):
return mx.library('JACOCOAGENT_0.8.7_CUSTOM', True).get_path(resolve)
def get_jacoco_agent_args(jacoco=None):
'''
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 is None:
jacoco = _jacoco
if jacoco in ('on', 'append'):
excludes, includes = _jacoco_excludes_includes()
with tempfile.NamedTemporaryFile(prefix="jacoco_excludes", mode="w", delete=False) as excludesfile:
# Make sure to remove temporary file when program exit
atexit.register(os.remove, excludesfile.name)
excludesfile.write(':'.join(excludes))
excludesfile.flush()
agentOptions = {
'append' : 'true' if jacoco == 'append' else 'false',
'inclbootstrapclasses' : 'true',
'includes' : ':'.join(includes),
'excludesfile' : excludesfile.name,
'destfile' : get_jacoco_dest_file(),
}
return ['-javaagent:' + get_jacoco_agent_path(True) + '=' + ','.join([k + '=' + v for k, v in agentOptions.items()])]
return None
def jacocoreport(args):
_jacocoreport(args)
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.
This function returns the included projects and excludes used for this report"""
dist_name = "MX_JACOCO_REPORT"
mx.command_function("build")(['--dependencies', dist_name])
dist = mx.distribution(dist_name)
jdk = mx.get_jdk(dist.javaCompliance)
parser = ArgumentParser(prog='mx jacocoreport')
parser.add_argument('--format', help='Export format (HTML or XML)', default='html', choices=['html', 'xml'])
parser.add_argument('--omit-excluded', action='store_true', help='omit excluded files from report')
parser.add_argument('output_directory', help='Output directory', default='coverage', nargs='?')
args = parser.parse_args(args)
# list of strings of the form "project-dir:binary-dir"
includedirs = []
includedprojects = []
for p in mx.projects():
projsetting = getattr(p, 'jacoco', '')
if projsetting in ('include', '') and _jacoco_is_package_whitelisted(p.name):
if isinstance(p, mx.ClasspathDependency):
if args.omit_excluded and p.is_test_project(): # skip test projects when omit-excluded
continue
source_dirs = []
if p.isJavaProject():
source_dirs += p.source_dirs() + [p.source_gen_dir()]
includedirs.append(":".join([p.dir, p.classpath_repr(jdk)] + source_dirs))
includedprojects.append(p.name)
def _run_reporter(extra_args=None):
mx.run_java(['-cp', mx.classpath([dist_name], jdk=jdk), '-jar', dist.path, '--in', get_jacoco_dest_file(), '--out',
args.output_directory, '--format', args.format] +
(extra_args or []) +
sorted(includedirs),
jdk=jdk, addDefaultArgs=False)
if not args.omit_excluded:
_run_reporter()
excludes = []
else:
with tempfile.NamedTemporaryFile(suffix="jacoco-report-exclude", mode="w") as fp:
excludes, _ = _jacoco_excludes_includes()
fp.writelines((e + "\n" for e in excludes))
fp.flush()
_run_reporter(['--exclude-file', fp.name])
return includedprojects, excludes
def _parse_java_properties(args):
prop_re = re.compile('-D(?P<key>[^=]+)=(?P<value>.*)')
remainder = []
java_properties = {}
for arg in args:
m = prop_re.match(arg)
if m:
java_properties[m.group('key')] = m.group('value')
else:
remainder.append(arg)
return java_properties, remainder
def _jacoco_excludes_includes_projects(limit_to_primary=False):
includes = []
excludes = []
projects = mx.projects(limit_to_primary=limit_to_primary)
for p in projects:
if p.isJavaProject():
projsetting = getattr(p, 'jacoco', '')
if not _jacoco_is_package_whitelisted(p.name):
excludes.append(p)
elif projsetting == 'exclude':
excludes.append(p)
else:
includes.append(p)
return excludes, includes
def _jacoco_exclude_classes(projects):
excludeClasses = {}
for p in projects:
r = p.find_classes_with_annotations(None, _jacoco_excluded_annotations, includeGenSrc=True)
excludeClasses.update(r)
r = p.find_classes_with_matching_source_line(None, lambda line: 'JaCoCo Exclude' in line, includeGenSrc=True)
excludeClasses.update(r)
return excludeClasses
def coverage_upload(args):
parser = ArgumentParser(prog='mx coverage-upload')
parser.add_argument('--upload-url', required=False, default=mx.get_env('COVERAGE_UPLOAD_URL'), help='Format is like rsync: user@host:/directory')
parser.add_argument('--build-name', required=False, default=mx.get_env('BUILD_NAME'))
parser.add_argument('--build-url', required=False, default=mx.get_env('BUILD_URL'))
parser.add_argument('--build-number', required=False, default=mx.get_env('BUILD_NUMBER'))
args, other_args = parser.parse_known_args(args)
if not args.upload_url:
parser.print_help()
return
remote_host, remote_basedir = args.upload_url.split(':')
if not remote_host:
mx.abort('Cannot determine remote host from {}'.format(args.upload_url))
primary = mx.primary_suite()
if not primary.vc:
mx.abort('coverage_upload requires the primary suite to be in a vcs repository')
info = primary.vc.parent_info(primary.dir)
rev = primary.vc.parent(primary.dir)
if len(remote_basedir) > 0 and not remote_basedir.endswith('/'):
remote_basedir += '/'
remote_dir = '{}_{}_{}'.format(primary.name, datetime.datetime.fromtimestamp(info['author-ts']).strftime('%Y-%m-%d_%H_%M'), rev[:7])
if args.build_name:
remote_dir += '_' + args.build_name
if args.build_number:
remote_dir += '_' + args.build_number
upload_dir = remote_basedir + remote_dir
includes, excludes = _jacocoreport(['--omit-excluded'] + other_args)
# Upload jar+sources
coverage_sources = 'java_sources.tar.gz'
coverage_binaries = 'java_binaries.tar.gz'
with mx.Archiver(os.path.realpath(coverage_sources), kind='tgz') as sources, mx.Archiver(os.path.realpath(coverage_binaries), kind='tgz') as binaries:
def _visit_deps(dep, edge):
if dep.isJavaProject() and not dep.is_test_project():
binaries.zf.add(dep.output_dir(), dep.name)
for d in dep.source_dirs():
sources.zf.add(d, dep.name)
if os.path.exists(dep.source_gen_dir()):
sources.zf.add(dep.source_gen_dir(), dep.name)
mx.walk_deps(mx.projects(), visit=_visit_deps)
files = [get_jacoco_dest_file(), 'coverage', coverage_sources, coverage_binaries]
print("Syncing {} to {}:{}".format(" ".join(files), remote_host, upload_dir))
mx.run([
'bash',
'-c',
r'tar -czf - {files} | ssh {remote} bash -c \'"mkdir -p {remotedir} && cd {remotedir} && cat | tar -x{verbose}z && chmod -R 755 ."\''
.format(
files=" ".join(files),
remote=remote_host,
remotedir=upload_dir,
verbose='v' if mx._opts.verbose else '')
])
def upload_string(content, path):
mx.run(['ssh', remote_host, 'bash', '-c', 'cat > "' + path + '"'], stdin=content)
upload_string(json.dumps({
'timestamp': time.time(),
'suite': primary.name,
'revision': rev,
'directory': remote_dir,
'build_name': args.build_name,
'build_url': args.build_url,
'jdk_version': str(mx.get_jdk().version),
'build_number': args.build_number,
'primary_info': info,
'excludes': [str(e) for e in excludes],
'includes': [str(i) for i in includes]}), upload_dir + '/description.json')
mx.run(['ssh', remote_host, 'bash', '-c', r'"(echo \[; for i in {remote_basedir}/*/description.json; do if \[ -s \$i \];then cat \$i; echo ,; fi done; echo null\]) |jq \"del(.. | .excludes?, .includes?)\" > {remote_basedir}/index.json"'.format(remote_basedir=remote_basedir)])
upload_string("""<html>
<script language="javascript">
function urlChange(url) {
if (url.pathname !== "blank") {
window.history.replaceState(null, null, url.pathname.replace("/coverage_upload/", "/coverage_upload/#"))
}
}
</script>
<frameset rows="40,*">
<frame id="navigation" src="navigation.html"/>
<frame id="content" src="" onload="urlChange(this.contentWindow.location);" />
</frameset>
</html>""", remote_basedir + '/index.html')
js_library_url = rewriteurl("https://ajax.googleapis.com/ajax/libs/angularjs/1.7.7/angular.js")
upload_string(r"""<html>
<head>
<script src="%js_library_url"></script>
<script language="javascript">
var App = angular.module('myApp', [])
.controller('IndexCtrl', function IndexCtrl($scope, $http) {
var hash = parent.window.location.hash;
if(hash) {
hash = hash.substring(1, hash.length); // remove leading hash
}
$http.get('index.json').then(function(response, status) {
var data = response.data.filter(x => x != null);
/*
#GR-17399
Filter build that are unique per suite with revision as key and merge builds.
*/
data = data
.filter(x => !x.hasOwnProperty('merge'))
.filter( // filter builds that are unique per suite with revision as key
x => !data
.filter(z => x != z && x.jdk_version == z.jdk_version && x.suite == z.suite) // exclude self build and build for other suites.
.map(z => z.revision) // map from array of build to array of revision
.includes(x.revision) // check if revision of x is index data.
).concat(data.filter(x => x.hasOwnProperty('merge'))); // concat unique build with merged build.
data.sort((l,r) => r.timestamp - l.timestamp);
if(data.length > 0) {