OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Extract UserMetrics "actions" strings from the Chrome source. | 6 """Extract UserMetrics "actions" strings from the Chrome source. |
7 | 7 |
8 This program generates the list of known actions we expect to see in the | 8 This program generates the list of known actions we expect to see in the |
9 user behavior logs. It walks the Chrome source, looking for calls to | 9 user behavior logs. It walks the Chrome source, looking for calls to |
10 UserMetrics functions, extracting actions and warning on improper calls, | 10 UserMetrics functions, extracting actions and warning on improper calls, |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 298 |
299 def WalkDirectory(root_path, actions, extensions, callback): | 299 def WalkDirectory(root_path, actions, extensions, callback): |
300 for path, dirs, files in os.walk(root_path): | 300 for path, dirs, files in os.walk(root_path): |
301 if '.svn' in dirs: | 301 if '.svn' in dirs: |
302 dirs.remove('.svn') | 302 dirs.remove('.svn') |
303 for file in files: | 303 for file in files: |
304 ext = os.path.splitext(file)[1] | 304 ext = os.path.splitext(file)[1] |
305 if ext in extensions: | 305 if ext in extensions: |
306 callback(os.path.join(path, file), actions) | 306 callback(os.path.join(path, file), actions) |
307 | 307 |
| 308 def GrepForMsgActions(path, actions): |
| 309 """Grep a source file for ViewHostMsg_UserMetricsRecordAction. |
| 310 |
| 311 Arguments: |
| 312 path: path to the file |
| 313 actions: set of actions to add to |
| 314 """ |
| 315 # We look for the ViewHostMsg_UserMetricsRecordAction constructor. |
| 316 # This should be on one line. |
| 317 action_re = re.compile( |
| 318 r'[^a-zA-Z]ViewHostMsg_UserMetricsRecordAction\("([^"]*)') |
| 319 line_number = 0 |
| 320 for line in open(path): |
| 321 match = action_re.search(line) |
| 322 if match: # Plain call to RecordAction |
| 323 actions.add(match.group(1)) |
| 324 |
308 def AddLiteralActions(actions): | 325 def AddLiteralActions(actions): |
309 """Add literal actions specified via calls to UserMetrics functions. | 326 """Add literal actions specified via calls to UserMetrics functions. |
310 | 327 |
311 Arguments: | 328 Arguments: |
312 actions: set of actions to add to. | 329 actions: set of actions to add to. |
313 """ | 330 """ |
314 EXTENSIONS = ('.cc', '.mm', '.c', '.m') | 331 EXTENSIONS = ('.cc', '.mm', '.c', '.m') |
315 | 332 |
316 # Walk the source tree to process all .cc files. | 333 # Walk the source tree to process all .cc files. |
317 chrome_root = os.path.join(path_utils.ScriptDir(), '..') | 334 chrome_root = os.path.join(path_utils.ScriptDir(), '..') |
318 WalkDirectory(chrome_root, actions, EXTENSIONS, GrepForActions) | 335 WalkDirectory(chrome_root, actions, EXTENSIONS, GrepForActions) |
319 content_root = os.path.join(path_utils.ScriptDir(), '..', '..', 'content') | 336 content_root = os.path.join(path_utils.ScriptDir(), '..', '..', 'content') |
320 WalkDirectory(content_root, actions, EXTENSIONS, GrepForActions) | 337 WalkDirectory(content_root, actions, EXTENSIONS, GrepForActions) |
321 webkit_root = os.path.join(path_utils.ScriptDir(), '..', '..', 'webkit') | 338 webkit_root = os.path.join(path_utils.ScriptDir(), '..', '..', 'webkit') |
322 WalkDirectory(os.path.join(webkit_root, 'glue'), actions, EXTENSIONS, | 339 WalkDirectory(os.path.join(webkit_root, 'glue'), actions, EXTENSIONS, |
323 GrepForActions) | 340 GrepForActions) |
324 WalkDirectory(os.path.join(webkit_root, 'port'), actions, EXTENSIONS, | 341 WalkDirectory(os.path.join(webkit_root, 'port'), actions, EXTENSIONS, |
325 GrepForActions) | 342 GrepForActions) |
326 | 343 |
327 def AddWebUIActions(actions): | 344 def AddWebUIActions(actions): |
328 """Add user actions defined in WebUI files. | 345 """Add user actions defined in WebUI files. |
329 | 346 |
330 Arguments: | 347 Arguments: |
331 actions: set of actions to add to. | 348 actions: set of actions to add to. |
332 """ | 349 """ |
333 resources_root = os.path.join(path_utils.ScriptDir(), '..', 'browser', | 350 resources_root = os.path.join(path_utils.ScriptDir(), '..', 'browser', |
334 'resources') | 351 'resources') |
335 WalkDirectory(resources_root, actions, ('.html'), GrepForWebUIActions) | 352 WalkDirectory(resources_root, actions, ('.html'), GrepForWebUIActions) |
336 | 353 |
| 354 def AddMsgActions(actions): |
| 355 """Add user actions sent via ViewHostMsg_UserMetricsRecordAction. |
| 356 |
| 357 Arguments: |
| 358 actions: set of actions to add to. |
| 359 """ |
| 360 EXTENSIONS = ('.cc', '.mm', '.c', '.m') |
| 361 |
| 362 chrome_renderer_root = os.path.join(path_utils.ScriptDir(), '..', 'renderer') |
| 363 content_renderer_root = os.path.join(path_utils.ScriptDir(), '..', '..', |
| 364 'content', 'renderer') |
| 365 WalkDirectory(chrome_renderer_root, actions, EXTENSIONS, GrepForMsgActions) |
| 366 WalkDirectory(content_renderer_root, actions, EXTENSIONS, GrepForMsgActions) |
| 367 |
337 def main(argv): | 368 def main(argv): |
338 if '--hash' in argv: | 369 if '--hash' in argv: |
339 hash_output = True | 370 hash_output = True |
340 else: | 371 else: |
341 hash_output = False | 372 hash_output = False |
342 print >>sys.stderr, "WARNING: If you added new UMA tags, you must" + \ | 373 print >>sys.stderr, "WARNING: If you added new UMA tags, you must" + \ |
343 " use the --hash option to update chromeactions.txt." | 374 " use the --hash option to update chromeactions.txt." |
344 # if we do a hash output, we want to only append NEW actions, and we know | 375 # if we do a hash output, we want to only append NEW actions, and we know |
345 # the file we want to work on | 376 # the file we want to work on |
346 actions = set() | 377 actions = set() |
347 | 378 |
348 chromeactions_path = os.path.join(path_utils.ScriptDir(), "chromeactions.txt") | 379 chromeactions_path = os.path.join(path_utils.ScriptDir(), "chromeactions.txt") |
349 | 380 |
350 if hash_output: | 381 if hash_output: |
351 f = open(chromeactions_path) | 382 f = open(chromeactions_path) |
352 for line in f: | 383 for line in f: |
353 part = line.rpartition("\t") | 384 part = line.rpartition("\t") |
354 part = part[2].strip() | 385 part = part[2].strip() |
355 actions.add(part) | 386 actions.add(part) |
356 f.close() | 387 f.close() |
357 | 388 |
358 | 389 |
359 AddComputedActions(actions) | 390 AddComputedActions(actions) |
360 # TODO(fmantek): bring back webkit editor actions. | 391 # TODO(fmantek): bring back webkit editor actions. |
361 # AddWebKitEditorActions(actions) | 392 # AddWebKitEditorActions(actions) |
362 AddAboutFlagsActions(actions) | 393 AddAboutFlagsActions(actions) |
363 AddWebUIActions(actions) | 394 AddWebUIActions(actions) |
| 395 AddMsgActions(actions) |
364 | 396 |
365 AddLiteralActions(actions) | 397 AddLiteralActions(actions) |
366 | 398 |
367 # print "Scanned {0} number of files".format(number_of_files_total) | 399 # print "Scanned {0} number of files".format(number_of_files_total) |
368 # print "Found {0} entries".format(len(actions)) | 400 # print "Found {0} entries".format(len(actions)) |
369 | 401 |
370 AddClosedSourceActions(actions) | 402 AddClosedSourceActions(actions) |
371 AddChromeOSActions(actions) | 403 AddChromeOSActions(actions) |
372 | 404 |
373 if hash_output: | 405 if hash_output: |
374 f = open(chromeactions_path, "w") | 406 f = open(chromeactions_path, "w") |
375 | 407 |
376 | 408 |
377 # Print out the actions as a sorted list. | 409 # Print out the actions as a sorted list. |
378 for action in sorted(actions): | 410 for action in sorted(actions): |
379 if hash_output: | 411 if hash_output: |
380 hash = hashlib.md5() | 412 hash = hashlib.md5() |
381 hash.update(action) | 413 hash.update(action) |
382 print >>f, '0x%s\t%s' % (hash.hexdigest()[:16], action) | 414 print >>f, '0x%s\t%s' % (hash.hexdigest()[:16], action) |
383 else: | 415 else: |
384 print action | 416 print action |
385 | 417 |
386 if hash_output: | 418 if hash_output: |
387 print "Done. Do not forget to add chromeactions.txt to your changelist" | 419 print "Done. Do not forget to add chromeactions.txt to your changelist" |
388 | 420 |
389 if '__main__' == __name__: | 421 if '__main__' == __name__: |
390 main(sys.argv) | 422 main(sys.argv) |
OLD | NEW |