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
|
#!/usr/bin/env python2
import datetime
import os
import sys
import json
import tempfile
import time
import dateutil.parser
from oauth2client.client import GoogleCredentials
from googleapiclient.discovery import build as gcb_build
from google.cloud import storage
import build_and_run_coverage
import build_project
STATUS_BUCKET = 'oss-fuzz-build-logs'
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
BADGE_DIR = 'badges'
RETRY_COUNT = 3
RETRY_WAIT = 5
MAX_BUILD_RESULTS = 2000
BUILDS_PAGE_SIZE = 256
BADGE_IMAGE_TYPES = {'svg': 'image/svg+xml', 'png': 'image/png'}
_client = None
def _get_storage_client():
"""Return storage client."""
global _client
if not _client:
_client = storage.Client()
return _client
def usage():
sys.stderr.write('Usage: ' + sys.argv[0] + ' <projects_dir>\n')
exit(1)
def scan_project_names(projects_dir):
projects = []
for root, dirs, files in os.walk(projects_dir):
for f in files:
if f == 'Dockerfile':
projects.append(os.path.basename(root))
return sorted(projects)
def upload_status(successes, failures, status_filename):
"""Upload main status page."""
data = {
'projects': failures + successes,
'failures': failures,
'successes': successes,
'last_updated': datetime.datetime.utcnow().ctime()
}
bucket = _get_storage_client().get_bucket(STATUS_BUCKET)
blob = bucket.blob(status_filename)
blob.cache_control = 'no-cache'
blob.upload_from_string(json.dumps(data), content_type='application/json')
def is_build_successful(build):
return build['status'] == 'SUCCESS'
def find_last_build(builds, project, build_tag_suffix):
DELAY_MINUTES = 40
tag = project + '-' + build_tag_suffix
builds = builds.get(tag)
if not builds:
print >> sys.stderr, 'Failed to find builds with tag', tag
return None
for build in builds:
if build['status'] == 'WORKING':
continue
if tag not in build['tags']:
continue
if not 'finishTime' in build:
continue
finish_time = dateutil.parser.parse(build['finishTime'], ignoretz=True)
if (datetime.datetime.utcnow() - finish_time >=
datetime.timedelta(minutes=DELAY_MINUTES)):
status_bucket = _get_storage_client().get_bucket(STATUS_BUCKET)
gcb_bucket = _get_storage_client().get_bucket(
build_project.GCB_LOGS_BUCKET)
log_name = 'log-{0}.txt'.format(build['id'])
log = gcb_bucket.blob(log_name)
dest_log = status_bucket.blob(log_name)
with tempfile.NamedTemporaryFile() as f:
log.download_to_filename(f.name)
dest_log.upload_from_filename(f.name, content_type='text/plain')
return build
return None
def execute_with_retries(request):
for i in xrange(RETRY_COUNT + 1):
try:
return request.execute()
except Exception as e:
print('request failed with {0}, retrying...'.format(str(e)))
if i < RETRY_COUNT:
time.sleep(RETRY_WAIT)
continue
raise
def get_builds(cloudbuild):
"""Get a batch of the latest builds (up to MAX_BUILD_RESULTS), grouped by
tag."""
ungrouped_builds = []
next_page_token = None
while True:
page_size = min(BUILDS_PAGE_SIZE, MAX_BUILD_RESULTS - len(ungrouped_builds))
response = execute_with_retries(cloudbuild.projects().builds().list(
projectId='oss-fuzz', pageSize=page_size, pageToken=next_page_token))
if not 'builds' in response:
print >> sys.stderr, 'Invalid response from builds list:', response
return None
ungrouped_builds.extend(response['builds'])
if len(ungrouped_builds) >= MAX_BUILD_RESULTS:
break
next_page_token = response.get('nextPageToken')
builds = {}
for build in ungrouped_builds:
for tag in build['tags']:
builds.setdefault(tag, []).append(build)
return builds
def update_build_status(builds, projects, build_tag_suffix, status_filename):
successes = []
failures = []
for project in projects:
print project
last_build = find_last_build(builds, project, build_tag_suffix)
if not last_build:
print >> sys.stderr, 'Failed to get build for', project
continue
print last_build['startTime'], last_build['status'], last_build['id']
if is_build_successful(last_build):
successes.append({
'name': project,
'build_id': last_build['id'],
'finish_time': last_build['finishTime'],
'success': True,
})
else:
failures.append({
'name': project,
'build_id': last_build['id'],
'finish_time': last_build['finishTime'],
'success': False,
})
upload_status(successes, failures, status_filename)
def update_build_badges(builds, projects, build_tag, coverage_tag):
for project in projects:
last_build = find_last_build(builds, project, build_tag)
last_coverage_build = find_last_build(builds, project, coverage_tag)
if not last_build or not last_coverage_build:
continue
badge = 'building'
if not is_build_successful(last_coverage_build):
badge = 'coverage_failing'
if not is_build_successful(last_build):
badge = 'failing'
print("[badge] {}: {}".format(project, badge))
for extension, mime_type in BADGE_IMAGE_TYPES.items():
badge_name = '{badge}.{extension}'.format(
badge=badge, extension=extension)
# Retrieve the image relative to this script's location
badge_file = os.path.join(SCRIPT_DIR, 'badge_images', badge_name)
# The uploaded blob name should look like `badges/project.png`
blob_name = '{badge_dir}/{project_name}.{extension}'.format(
badge_dir=BADGE_DIR, project_name=project, extension=extension)
status_bucket = _get_storage_client().get_bucket(STATUS_BUCKET)
badge_blob = status_bucket.blob(blob_name)
badge_blob.upload_from_filename(badge_file, content_type=mime_type)
def main():
if len(sys.argv) != 2:
usage()
projects_dir = sys.argv[1]
projects = scan_project_names(projects_dir)
credentials = GoogleCredentials.get_application_default()
cloudbuild = gcb_build('cloudbuild', 'v1', credentials=credentials)
builds = get_builds(cloudbuild)
update_build_status(
builds,
projects,
build_project.FUZZING_BUILD_TAG,
status_filename='status.json')
update_build_status(
builds,
projects,
build_and_run_coverage.COVERAGE_BUILD_TAG,
status_filename='status-coverage.json')
update_build_badges(
builds,
projects,
build_tag=build_project.FUZZING_BUILD_TAG,
coverage_tag=build_and_run_coverage.COVERAGE_BUILD_TAG)
if __name__ == '__main__':
main()
|