Skip to content

Commit 56dc415

Browse files
author
Bastien Abadie
committed
report: Use taskcluster client library, refs #386
1 parent 768dc93 commit 56dc415

File tree

3 files changed

+63
-72
lines changed

3 files changed

+63
-72
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
3+
from taskcluster.helper import TaskclusterConfig
4+
5+
taskcluster = TaskclusterConfig("https://firefox-ci-tc.services.mozilla.com")

report/firefox_code_coverage/codecoverage.py

Lines changed: 58 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@
1212
import time
1313
import warnings
1414

15-
try:
16-
from urllib.parse import urlencode
17-
from urllib.request import Request, urlopen, urlretrieve
18-
except ImportError:
19-
from urllib import urlencode, urlretrieve
20-
from urllib2 import Request, urlopen
15+
import requests
2116

17+
from firefox_code_coverage import taskcluster
2218

2319
FINISHED_STATUSES = ["completed", "failed", "exception"]
2420
ALL_STATUSES = FINISHED_STATUSES + ["unscheduled", "pending", "running"]
@@ -28,91 +24,82 @@
2824
GRCOV_ARTIFACT = "public/build/grcov.tar.xz"
2925

3026

31-
def get_json(url, params=None, headers={}):
32-
if params is not None:
33-
url += "?" + urlencode(params)
34-
35-
request = Request(url, headers=headers)
36-
r = urlopen(request).read().decode("utf-8")
37-
38-
return json.loads(r)
39-
40-
4127
def is_taskcluster_loaner():
4228
return "TASKCLUSTER_INTERACTIVE" in os.environ
4329

4430

4531
def get_task(branch, revision):
46-
task = get_json(
47-
f"https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/gecko.v2.{branch}.revision.{revision}.firefox.decision"
48-
)
32+
index = taskcluster.get_service("index")
33+
task = index.findTask(f"gecko.v2.{branch}.revision.{revision}.firefox.decision")
4934
return task["taskId"]
5035

5136

5237
def get_last_task():
53-
revision = get_json(
38+
resp = requests.get(
5439
"https://api.coverage.moz.tools/v2/latest?repository=mozilla-central"
55-
)[0]["revision"]
40+
)
41+
resp.raise_for_status()
42+
data = resp.json()
43+
revision = data[0]["revision"]
5644
return get_task("mozilla-central", revision)
5745

5846

5947
def get_task_details(task_id):
60-
task_details = get_json(
61-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task/" + task_id
62-
)
63-
return task_details
48+
queue = taskcluster.get_service("queue")
49+
return queue.task(task_id)
6450

6551

6652
def get_task_artifacts(task_id):
67-
artifacts = get_json(
68-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task/"
69-
+ task_id
70-
+ "/artifacts"
71-
)
72-
return artifacts["artifacts"]
53+
queue = taskcluster.get_service("queue")
54+
response = queue.listLatestArtifacts(task_id)
55+
return response["artifacts"]
7356

7457

7558
def get_tasks_in_group(group_id):
76-
reply = get_json(
77-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task-group/"
78-
+ group_id
79-
+ "/list",
80-
{"limit": "200"},
81-
)
82-
tasks = reply["tasks"]
83-
while "continuationToken" in reply:
84-
reply = get_json(
85-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task-group/"
86-
+ group_id
87-
+ "/list",
88-
{"limit": "200", "continuationToken": reply["continuationToken"]},
89-
)
90-
tasks += reply["tasks"]
59+
tasks = []
60+
61+
def _save_tasks(response):
62+
tasks.extend(response["tasks"])
63+
64+
queue = taskcluster.get_service("queue")
65+
queue.listTaskGroup(group_id, paginationHandler=_save_tasks)
66+
9167
return tasks
9268

9369

70+
def download_binary(url, path):
71+
"""Download a binary file from an url"""
72+
while True:
73+
try:
74+
artifact = requests.get(url, stream=True)
75+
artifact.raise_for_status()
76+
77+
with open(path, "wb") as f:
78+
for chunk in artifact.iter_content(chunk_size=8192):
79+
f.write(chunk)
80+
break
81+
except: # noqa: E722
82+
try:
83+
os.remove(path)
84+
except OSError:
85+
pass
86+
87+
time.sleep(7)
88+
89+
9490
def download_artifact(task_id, artifact, artifacts_path):
9591
fname = os.path.join(
9692
artifacts_path, task_id + "_" + os.path.basename(artifact["name"])
9793
)
94+
95+
# As recommended by Taskcluster doc, use requests to download
96+
# from the artifact public url instead of relying on the client method
97+
queue = taskcluster.get_service("queue")
98+
url = queue.buildUrl("getLatestArtifact", task_id, artifact["name"])
99+
98100
if not os.path.exists(fname):
99-
while True:
100-
try:
101-
urlretrieve(
102-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task/"
103-
+ task_id
104-
+ "/artifacts/"
105-
+ artifact["name"],
106-
fname,
107-
)
108-
break
109-
except: # noqa: E722
110-
try:
111-
os.remove(fname)
112-
except OSError:
113-
pass
114-
115-
time.sleep(7)
101+
download_binary(url, fname)
102+
116103
return fname
117104

118105

@@ -145,11 +132,8 @@ def get_platform(task_name):
145132

146133

147134
def get_task_status(task_id):
148-
status = get_json(
149-
"https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task/{}/status".format(
150-
task_id
151-
)
152-
)
135+
queue = taskcluster.get_service("queue")
136+
status = queue.status(task_id)
153137
return status["status"]["state"]
154138

155139

@@ -316,10 +300,9 @@ def download_grcov():
316300

317301
dest = tempfile.mkdtemp(suffix="grcov")
318302
archive = os.path.join(dest, "grcov.tar.xz")
319-
url = "https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/{index}/artifacts/{artifact}".format(
320-
index=GRCOV_INDEX, artifact=GRCOV_ARTIFACT
321-
)
322-
urlretrieve(url, archive)
303+
index = taskcluster.get_service("index")
304+
url = index.buildUrl("findArtifactFromTask", GRCOV_INDEX, GRCOV_ARTIFACT)
305+
download_binary(url, archive)
323306

324307
# Extract archive in temp
325308
tar = tarfile.open(archive, "r:xz")
@@ -436,6 +419,9 @@ def main():
436419
parser.print_help()
437420
return
438421

422+
# Start by running automatic auth on Taskcluster client
423+
taskcluster.auth()
424+
439425
if args.branch and args.commit:
440426
task_id = get_task(args.branch, args.commit)
441427
else:

report/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
taskcluster==24.2.0

0 commit comments

Comments
 (0)