forked from mozilla/code-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodecoverage.py
More file actions
461 lines (379 loc) · 13.4 KB
/
Copy pathcodecoverage.py
File metadata and controls
461 lines (379 loc) · 13.4 KB
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
# -*- coding: utf-8 -*-
import argparse
import errno
import json
import os
import subprocess
import sys
import tarfile
import time
import warnings
try:
from urllib.parse import urlencode
from urllib.request import Request, urlopen, urlretrieve
except ImportError:
from urllib import urlencode, urlretrieve
from urllib2 import Request, urlopen
TEST_PLATFORMS = ["test-linux64-ccov/debug", "test-windows10-64-ccov/debug"]
FINISHED_STATUSES = ["completed", "failed", "exception"]
ALL_STATUSES = FINISHED_STATUSES + ["unscheduled", "pending", "running"]
STATUS_VALUE = {"exception": 1, "failed": 2, "completed": 3}
def get_json(url, params=None, headers={}):
if params is not None:
url += "?" + urlencode(params)
request = Request(url, headers=headers)
r = urlopen(request).read().decode("utf-8")
return json.loads(r)
def is_taskcluster_loaner():
return "TASKCLUSTER_INTERACTIVE" in os.environ
def get_last_task():
last_task = get_json(
"https://index.taskcluster.net/v1/task/gecko.v2.mozilla-central.latest.firefox.linux64-ccov-debug"
)
return last_task["taskId"]
def get_task(branch, revision):
task = get_json(
"https://index.taskcluster.net/v1/task/gecko.v2.%s.revision.%s.firefox.linux64-ccov-debug"
% (branch, revision)
)
return task["taskId"]
def get_task_details(task_id):
task_details = get_json("https://queue.taskcluster.net/v1/task/" + task_id)
return task_details
def get_task_artifacts(task_id):
artifacts = get_json(
"https://queue.taskcluster.net/v1/task/" + task_id + "/artifacts"
)
return artifacts["artifacts"]
def get_tasks_in_group(group_id):
reply = get_json(
"https://queue.taskcluster.net/v1/task-group/" + group_id + "/list",
{"limit": "200"},
)
tasks = reply["tasks"]
while "continuationToken" in reply:
reply = get_json(
"https://queue.taskcluster.net/v1/task-group/" + group_id + "/list",
{"limit": "200", "continuationToken": reply["continuationToken"]},
)
tasks += reply["tasks"]
return tasks
def download_artifact(task_id, artifact, artifacts_path):
fname = os.path.join(
artifacts_path, task_id + "_" + os.path.basename(artifact["name"])
)
if not os.path.exists(fname):
while True:
try:
urlretrieve(
"https://queue.taskcluster.net/v1/task/"
+ task_id
+ "/artifacts/"
+ artifact["name"],
fname,
)
break
except: # noqa: E722
try:
os.remove(fname)
except OSError:
pass
time.sleep(7)
return fname
def get_chunk(task_name):
for t in TEST_PLATFORMS:
if task_name.startswith(t):
task_name = task_name[len(t) + 1 :]
break
return "-".join([p for p in task_name.split("-") if p != "e10s"])
def get_suite(task_name):
return "-".join([p for p in get_chunk(task_name).split("-") if not p.isdigit()])
def get_platform(task_name):
if "linux" in task_name:
return "linux"
elif "windows" in task_name:
return "windows"
else:
raise Exception("Unknown platform")
def get_task_status(task_id):
status = get_json("https://queue.taskcluster.net/v1/task/{}/status".format(task_id))
return status["status"]["state"]
def download_coverage_artifacts(
build_task_id, suites, platforms, artifacts_path, suites_to_ignore=["talos", "awsy"]
):
try:
os.mkdir(artifacts_path)
except OSError as e:
if e.errno != errno.EEXIST:
raise e
task_data = get_task_details(build_task_id)
# Returns True if the task is a test-related task.
def _is_test_task(t):
return any(
t["task"]["metadata"]["name"].startswith(tp) for tp in TEST_PLATFORMS
)
# Returns True if the task is part of one of the suites chosen by the user.
def _is_in_suites_task(t):
suite_name = get_suite(t["task"]["metadata"]["name"])
return (
suites is None
or suite_name in suites
and suite_name not in suites_to_ignore
)
def _is_in_platforms_task(t):
platform = get_platform(t["task"]["metadata"]["name"])
return platforms is None or platform in platforms
test_tasks = [
t
for t in get_tasks_in_group(task_data["taskGroupId"])
if _is_test_task(t) and _is_in_suites_task(t) and _is_in_platforms_task(t)
]
if suites is not None:
for suite in suites:
if not any(suite in t["task"]["metadata"]["name"] for t in test_tasks):
warnings.warn("Suite %s not found" % suite)
download_tasks = {}
for test_task in test_tasks:
status = test_task["status"]["state"]
assert status in ALL_STATUSES, "State '{}' not recognized".format(status)
while status not in FINISHED_STATUSES:
sys.stdout.write(
"\rWaiting for task {} to finish...".format(
test_task["status"]["taskId"]
)
)
sys.stdout.flush()
time.sleep(60)
status = get_task_status(test_task["status"]["taskId"])
assert status in ALL_STATUSES
chunk_name = get_chunk(test_task["task"]["metadata"]["name"])
platform_name = get_platform(test_task["task"]["metadata"]["name"])
if (chunk_name, platform_name) not in download_tasks:
download_tasks[(chunk_name, platform_name)] = test_task
else:
prev_task = download_tasks[(chunk_name, platform_name)]
if STATUS_VALUE[status] > STATUS_VALUE[prev_task["status"]["state"]]:
download_tasks[(chunk_name, platform_name)] = test_task
artifact_paths = []
for i, test_task in enumerate(download_tasks.values()):
sys.stdout.write(
"\rDownloading artifacts from {}/{} test task...".format(i, len(test_tasks))
)
sys.stdout.flush()
artifacts = get_task_artifacts(test_task["status"]["taskId"])
for artifact in artifacts:
if any(
a in artifact["name"]
for a in ["code-coverage-grcov.zip", "code-coverage-jsvm.zip"]
):
artifact_paths.append(
download_artifact(
test_task["status"]["taskId"], artifact, artifacts_path
)
)
print("")
return artifact_paths
def generate_report(grcov_path, output_format, output_path, artifact_paths):
mod_env = os.environ.copy()
if is_taskcluster_loaner():
one_click_loaner_gcc = "/home/worker/workspace/build/src/gcc/bin"
i = 0
while (
not os.path.isdir(one_click_loaner_gcc)
or len(os.listdir(one_click_loaner_gcc)) == 0
):
print("Waiting one-click loaner to be ready... " + str(i))
i += 1
time.sleep(60)
mod_env["PATH"] = one_click_loaner_gcc + ":" + mod_env["PATH"]
fout = open(output_path, "w")
cmd = [grcov_path, "-t", output_format, "-p", "/home/worker/workspace/build/src/"]
if output_format in ["coveralls", "coveralls+"]:
cmd += ["--token", "UNUSED", "--commit-sha", "UNUSED"]
cmd.extend(artifact_paths)
proc = subprocess.Popen(cmd, stdout=fout, stderr=subprocess.PIPE, env=mod_env)
i = 0
while proc.poll() is None:
if i % 60 == 0:
sys.stdout.write("\rRunning grcov... {} seconds".format(i))
sys.stdout.flush()
i += 1
time.sleep(1)
print("")
if proc.poll() != 0:
raise Exception("Error while running grcov: {}\n".format(proc.stderr.read()))
def generate_html_report(
src_dir,
info_file=os.path.join(os.getcwd(), "output.info"),
output_dir=os.path.join(os.getcwd(), "report"),
silent=False,
style_file=None,
):
cwd = os.getcwd()
os.chdir(src_dir)
with open(os.devnull, "w") as fnull:
command = [
os.path.join(cwd, "lcov-bin/usr/local/bin/genhtml"),
"-o",
output_dir,
"--show-details",
"--highlight",
"--ignore-errors",
"source",
"--legend",
info_file,
]
if style_file is not None:
command += ["--css-file", style_file]
ret = subprocess.call(
command, stdout=fnull if silent else None, stderr=fnull if silent else None
)
if ret != 0:
raise Exception("Error while running genhtml.")
os.chdir(cwd)
def download_grcov():
headers = {}
if "GITHUB_ACCESS_TOKEN" in os.environ:
headers["Authorization"] = "token {}".format(os.environ["GITHUB_ACCESS_TOKEN"])
r = get_json(
"https://api.github.com/repos/marco-c/grcov/releases/latest", headers=headers
)
latest_tag = r["tag_name"]
if os.path.exists("grcov") and os.path.exists("grcov_ver"):
with open("grcov_ver", "r") as f:
installed_ver = f.read()
if installed_ver == latest_tag:
return
urlretrieve(
"https://github.com/marco-c/grcov/releases/download/%s/grcov-linux-x86_64.tar.bz2"
% latest_tag,
"grcov.tar.bz2",
)
tar = tarfile.open("grcov.tar.bz2", "r:bz2")
tar.extractall()
tar.close()
os.remove("grcov.tar.bz2")
with open("grcov_ver", "w") as f:
f.write(latest_tag)
def download_genhtml():
if os.path.isdir("lcov"):
os.chdir("lcov")
subprocess.check_call(["git", "pull"])
else:
subprocess.check_call(
["git", "clone", "https://github.com/linux-test-project/lcov.git"]
)
os.chdir("lcov")
subprocess.check_call(["make", "install", "DESTDIR=../lcov-bin"])
os.chdir("..")
def main():
parser = argparse.ArgumentParser()
if is_taskcluster_loaner():
nargs = "?"
default_src_dir = "/home/worker/workspace/build/src/"
default_branch = os.environ["MH_BRANCH"]
default_commit = os.environ["GECKO_HEAD_REV"]
else:
nargs = None
default_src_dir = None
default_branch = None
default_commit = None
parser.add_argument(
"src_dir",
action="store",
nargs=nargs,
default=default_src_dir,
help="Path to the source directory",
)
parser.add_argument(
"branch",
action="store",
nargs="?",
default=default_branch,
help="Branch on which jobs ran",
)
parser.add_argument(
"commit",
action="store",
nargs="?",
default=default_commit,
help="Commit hash for push",
)
parser.add_argument("--grcov", action="store", nargs="?", help="Path to grcov")
parser.add_argument(
"--with-artifacts",
action="store",
nargs="?",
default="ccov-artifacts",
help="Path to already downloaded coverage files",
)
parser.add_argument(
"--platform",
action="store",
nargs="+",
help='List of platforms to include (by default they are all included). E.g. "linux", "windows", etc.',
)
parser.add_argument(
"--suite",
action="store",
nargs="+",
help='List of test suites to include (by default they are all included). E.g. "mochitest", "mochitest-chrome", "gtest", etc.',
)
parser.add_argument(
"--ignore",
action="store",
nargs="+",
help='List of test suites to ignore (by default "talos" and "awsy"). E.g. "mochitest", "mochitest-chrome", "gtest", etc.',
)
parser.add_argument(
"--stats",
action="store_true",
help="Only generate high-level stats, not a full HTML report",
)
args = parser.parse_args()
if (args.branch is None) != (args.commit is None):
parser.print_help()
return
if args.branch and args.commit:
task_id = get_task(args.branch, args.commit)
else:
task_id = get_last_task()
if args.ignore is None:
artifact_paths = download_coverage_artifacts(
task_id, args.suite, args.platform, args.with_artifacts
)
else:
artifact_paths = download_coverage_artifacts(
task_id, args.suite, args.platform, args.with_artifacts, args.ignore
)
if args.grcov:
grcov_path = args.grcov
else:
download_grcov()
grcov_path = "./grcov"
if args.stats:
generate_report(grcov_path, "coveralls", "output.json", artifact_paths)
with open("output.json", "r") as f:
report = json.load(f)
total_lines = 0
total_lines_covered = 0
for sf in report["source_files"]:
for c in sf["coverage"]:
if c is None:
continue
total_lines += 1
if c > 0:
total_lines_covered += 1
print("Coverable lines: {}".format(total_lines))
print("Covered lines: {}".format(total_lines_covered))
print(
"Coverage percentage: {}".format(
float(total_lines_covered) / float(total_lines)
)
)
else:
generate_report(grcov_path, "lcov", "output.info", artifact_paths)
download_genhtml()
generate_html_report(os.path.abspath(args.src_dir))
if __name__ == "__main__":
main()