forked from catapult-project/catapult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_telemetry_build.py
executable file
·347 lines (311 loc) · 10.6 KB
/
generate_telemetry_build.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
#!/usr/bin/env python
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Generate BUILD.gn to define all data required to run telemetry test.
If a file/folder of large size is added to catapult but is not needed to run
telemetry test, then it should be added to the EXCLUDED_PATHS below and rerun
this script.
This script can also run with --check to see if it needs to rerun to update
BUILD.gn.
This script can also run with --chromium and rewrite the chromium file
//tools/perf/chrome_telemetry_build/BUILD.gn
This is for the purpose of running try jobs in chromium.
"""
import difflib
import logging
import os
import optparse
import sys
import subprocess
LICENSE = """# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
DO_NOT_EDIT_WARNING = """# This file is auto-generated from
# //third_party/catapult/generated_telemetry_build.py
# DO NOT EDIT!
"""
TELEMETRY_SUPPORT_GROUP_NAME = 'telemetry_chrome_test_support'
EXCLUDED_PATHS = [
{
# needed for --chromium option; can remove once this CL lands.
"path": "BUILD.gn",
},
{
"path": "common/node_runner",
},
{
"path": "docs",
},
{
"path": "experimental",
},
{
# needed for --chromium option; can remove once this CL lands.
"path": "generate_telemetry_build.py",
},
{
"path": "telemetry/telemetry/data",
},
{
"path": "telemetry/telemetry/bin",
},
{
"path": "telemetry/telemetry/internal/bin",
},
{
# needed for --check option
"path": "TEMP.gn",
},
{
"path": "third_party/google-endpoints",
},
{
"path": "third_party/Paste",
},
{
"path": "third_party/polymer2",
},
{
"path": "third_party/vinn/third_party/v8/linux/arm",
"condition": "is_chromeos",
},
{
"path": "third_party/vinn/third_party/v8/linux/mips",
"condition": "is_chromeos",
},
{
"path": "third_party/vinn/third_party/v8/linux/mips64",
"condition": "is_chromeos",
},
{
"path": "third_party/vinn/third_party/v8/linux/x86_64",
"condition": "is_linux || is_android",
},
{
"path": "third_party/vinn/third_party/v8/mac",
"condition": "is_mac",
},
{
"path": "third_party/vinn/third_party/v8/win",
"condition": "is_win",
},
{
"path": "tracing/test_data",
},
]
def GetUntrackedPaths():
"""Return directories/files in catapult/ that are not tracked by git."""
output = subprocess.check_output([
'git', 'ls-files', '--others', '--exclude-standard', '--directory',
'--full-name'])
paths = output.split('\n')
return [os.path.abspath(p) for p in paths if p]
def GetFileCondition(rel_path):
# Return 'true' if the file should be included; return 'false' if it should
# be excluded; return a condition string if it should only be included if
# the condition is true.
processed_rel_path = rel_path.replace('\\', '/')
for exclusion in EXCLUDED_PATHS:
assert 'path' in exclusion
if exclusion['path'] == processed_rel_path:
if 'condition' in exclusion:
return exclusion['condition']
else:
return 'false'
return 'true'
def GetDirCondition(rel_path):
# Return 'true' if the dir should be included; return 'false' if it should
# be excluded; return a condition string if it should only be included if
# the condition is true; return 'expand' if some files or sub-dirs under it
# are excluded or conditionally included, so the parser needs to go inside
# the dir and process further.
processed_rel_path = rel_path.replace('\\', '/')
for exclusion in EXCLUDED_PATHS:
assert 'path' in exclusion
if exclusion['path'] == processed_rel_path:
if 'condition' in exclusion:
return exclusion['condition']
else:
return 'false'
elif exclusion['path'].startswith(processed_rel_path + '/'):
return 'expand'
return 'true'
def WriteLists(lists, conditional_lists, build_file, path_prefix):
first_entry = True
for path_list in lists:
for path in path_list:
path = path.replace('\\', '/')
if path_prefix:
path = path_prefix + path
if first_entry:
build_file.write(' data += [\n')
first_entry = False
build_file.write(' "%s",\n' % path)
if not first_entry:
build_file.write(' ]\n\n')
for conditional_list in conditional_lists:
for entry in conditional_list:
assert 'path' in entry
assert 'condition' in entry
path = entry['path'].replace('\\', '/')
if path_prefix:
path = path_prefix + path
build_file.write(""" if (%s) {
data += [ "%s" ]
}
""" % (entry['condition'], path))
def ProcessDir(root_path, path, build_file, path_prefix):
# Write all dirs and files directly under |path| unless they are excluded
# or need to be processed further because some of their children are excldued
# or conditionally included.
# Return a list of dirs that needs to processed further.
logging.debug('GenerateList for ' + path)
entry_list = os.listdir(path)
entry_list.sort()
file_list = []
dir_list = []
conditional_list = []
expand_list = []
untracked_paths = GetUntrackedPaths()
for entry in entry_list:
full_path = os.path.join(path, entry)
rel_path = os.path.relpath(full_path, root_path)
if (any(full_path.startswith(p) for p in untracked_paths) or
entry.startswith('.') or entry.endswith('~') or
entry.endswith('.pyc') or entry.endswith('#')):
logging.debug('ignored %s', rel_path)
continue
if os.path.isfile(full_path):
condition = GetFileCondition(rel_path)
if condition == 'true':
file_list.append(rel_path)
elif condition == 'false':
logging.debug('excluded %s', rel_path)
continue
else:
conditional_list.append({
"condition": condition,
"path": rel_path,
});
elif os.path.isdir(full_path):
condition = GetDirCondition(rel_path)
if condition == 'true':
dir_list.append(rel_path + '/')
elif condition == 'false':
logging.debug('excluded %s', rel_path)
elif condition == 'expand':
expand_list.append(full_path)
else:
conditional_list.append({
"condition": condition,
"path": rel_path + '/',
});
else:
assert False
WriteLists([file_list, dir_list], [conditional_list],
build_file, path_prefix)
return expand_list
def WriteBuildFileHeader(build_file):
build_file.write(LICENSE)
build_file.write(DO_NOT_EDIT_WARNING)
build_file.write('import("//build/config/compiler/compiler.gni")\n\n')
def WriteBuildFileBody(build_file, root_path, path_prefix):
build_file.write("""group("%s") {
testonly = true
data = []
""" % TELEMETRY_SUPPORT_GROUP_NAME)
candidates = [root_path]
while len(candidates) > 0:
candidate = candidates.pop(0)
more = ProcessDir(root_path, candidate, build_file, path_prefix)
candidates.extend(more)
build_file.write("}")
def GenerateBuildFile(root_path, output_path, chromium):
CHROMIUM_GROUP = 'group("telemetry_chrome_test_without_chrome")'
CATAPULT_PREFIX = '//third_party/catapult'
CATAPULT_GROUP_NAME = CATAPULT_PREFIX + ':' + TELEMETRY_SUPPORT_GROUP_NAME
TELEMETRY_SUPPORT_GROUP = 'group("%s")' % TELEMETRY_SUPPORT_GROUP_NAME
if chromium:
build_file = open(output_path, 'r+')
contents = build_file.readlines()
build_file.seek(0)
remove_telemetry_support_group = False
for line in contents:
if TELEMETRY_SUPPORT_GROUP in line:
# --chromium has already run once, so remove the previously inserted
# TELEMETRY_SUPPORT_GROUP so we could add an updated one.
remove_telemetry_support_group = True
continue
if remove_telemetry_support_group:
if line == '}\n':
remove_telemetry_support_group = False
continue
if CHROMIUM_GROUP in line:
WriteBuildFileBody(build_file, root_path, CATAPULT_PREFIX + '/')
build_file.write('\n')
elif CATAPULT_GROUP_NAME in line:
line = line.replace(CATAPULT_GROUP_NAME,
':' + TELEMETRY_SUPPORT_GROUP_NAME)
build_file.write(line)
build_file.close()
else:
build_file = open(output_path, 'w')
WriteBuildFileHeader(build_file)
WriteBuildFileBody(build_file, root_path, None)
build_file.close()
def CheckForChanges():
# Return 0 if no changes are detected; return 1 otherwise.
root_path = os.path.dirname(os.path.realpath(__file__))
temp_path = os.path.join(root_path, "TEMP.gn")
GenerateBuildFile(root_path, temp_path, chromium=False)
ref_path = os.path.join(root_path, "BUILD.gn")
if not os.path.exists(ref_path):
logging.error("Can't localte BUILD.gn!")
return 1
temp_file = open(temp_path, 'r')
temp_content = temp_file.readlines()
temp_file.close()
os.remove(temp_path)
ref_file = open(ref_path, 'r')
ref_content = ref_file.readlines()
ref_file.close()
diff = difflib.unified_diff(temp_content, ref_content, fromfile=temp_path,
tofile=ref_path, lineterm='')
diff_data = []
for line in diff:
diff_data.append(line)
if len(diff_data) > 0:
logging.error('Diff found. Please rerun generate_telemetry_build.py.')
logging.debug('\n' + ''.join(diff_data))
return 1
logging.debug('No diff found. Everything is good.')
return 0
def main(argv):
parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False,
help="print out debug information")
parser.add_option("-c", "--check", action="store_true", default=False,
help="generate a temporary build file and compare if it "
"is the same as the current BUILD.gn")
parser.add_option("--chromium", action="store_true", default=False,
help="generate the build file into chromium workspace. "
"This is for the purpose of running try jobs in Chrome.")
(options, _) = parser.parse_args(args=argv)
if options.verbose:
logging.basicConfig(level=logging.DEBUG)
if options.check:
return CheckForChanges()
if options.chromium:
root_path = os.path.dirname(os.path.realpath(__file__))
output_path = os.path.join(
root_path, "../../tools/perf/chrome_telemetry_build/BUILD.gn")
GenerateBuildFile(root_path, output_path, chromium=True)
else:
root_path = os.path.dirname(os.path.realpath(__file__))
output_path = os.path.join(root_path, "BUILD.gn")
GenerateBuildFile(root_path, output_path, chromium=False)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))