| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env 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 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 import hashlib | 23 import hashlib |
| 24 from HTMLParser import HTMLParser | 24 from HTMLParser import HTMLParser |
| 25 import os | 25 import os |
| 26 import re | 26 import re |
| 27 import sys | 27 import sys |
| 28 | 28 |
| 29 sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'tools', 'python')) | 29 sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'tools', 'python')) |
| 30 from google import path_utils | 30 from google import path_utils |
| 31 | 31 |
| 32 # Files that are known to use UserMetrics::RecordComputedAction(), which means | 32 # Files that are known to use content::RecordComputedAction(), which means |
| 33 # they require special handling code in this script. | 33 # they require special handling code in this script. |
| 34 # To add a new file, add it to this list and add the appropriate logic to | 34 # To add a new file, add it to this list and add the appropriate logic to |
| 35 # generate the known actions to AddComputedActions() below. | 35 # generate the known actions to AddComputedActions() below. |
| 36 KNOWN_COMPUTED_USERS = ( | 36 KNOWN_COMPUTED_USERS = ( |
| 37 'back_forward_menu_model.cc', | 37 'back_forward_menu_model.cc', |
| 38 'options_page_view.cc', | 38 'options_page_view.cc', |
| 39 'render_view_host.cc', # called using webkit identifiers | 39 'render_view_host.cc', # called using webkit identifiers |
| 40 'user_metrics.cc', # method definition | 40 'user_metrics.cc', # method definition |
| 41 'new_tab_ui.cc', # most visited clicks 1-9 | 41 'new_tab_ui.cc', # most visited clicks 1-9 |
| 42 'extension_metrics_module.cc', # extensions hook for user metrics | 42 'extension_metrics_module.cc', # extensions hook for user metrics |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 Arguments: | 240 Arguments: |
| 241 path: path to the file | 241 path: path to the file |
| 242 actions: set of actions to add to | 242 actions: set of actions to add to |
| 243 """ | 243 """ |
| 244 global number_of_files_total | 244 global number_of_files_total |
| 245 number_of_files_total = number_of_files_total + 1 | 245 number_of_files_total = number_of_files_total + 1 |
| 246 # we look for the UserMetricsAction structure constructor | 246 # we look for the UserMetricsAction structure constructor |
| 247 # this should be on one line | 247 # this should be on one line |
| 248 action_re = re.compile(r'[^a-zA-Z]UserMetricsAction\("([^"]*)') | 248 action_re = re.compile(r'[^a-zA-Z]UserMetricsAction\("([^"]*)') |
| 249 malformed_action_re = re.compile(r'[^a-zA-Z]UserMetricsAction\([^"]') | 249 malformed_action_re = re.compile(r'[^a-zA-Z]UserMetricsAction\([^"]') |
| 250 computed_action_re = re.compile(r'UserMetrics::RecordComputedAction') | 250 computed_action_re = re.compile(r'RecordComputedAction') |
| 251 line_number = 0 | 251 line_number = 0 |
| 252 for line in open(path): | 252 for line in open(path): |
| 253 line_number = line_number + 1 | 253 line_number = line_number + 1 |
| 254 match = action_re.search(line) | 254 match = action_re.search(line) |
| 255 if match: # Plain call to RecordAction | 255 if match: # Plain call to RecordAction |
| 256 actions.add(match.group(1)) | 256 actions.add(match.group(1)) |
| 257 elif malformed_action_re.search(line): | 257 elif malformed_action_re.search(line): |
| 258 # Warn if this line is using RecordAction incorrectly. | 258 # Warn if this line is using RecordAction incorrectly. |
| 259 print >>sys.stderr, ('WARNING: %s has malformed call to RecordAction' | 259 print >>sys.stderr, ('WARNING: %s has malformed call to RecordAction' |
| 260 ' at %d' % (path, line_number)) | 260 ' at %d' % (path, line_number)) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 else: | 304 else: |
| 305 self.actions.add(attrs['metric']) | 305 self.actions.add(attrs['metric']) |
| 306 | 306 |
| 307 def GrepForWebUIActions(path, actions): | 307 def GrepForWebUIActions(path, actions): |
| 308 """Grep a WebUI source file for elements with associated metrics. | 308 """Grep a WebUI source file for elements with associated metrics. |
| 309 | 309 |
| 310 Arguments: | 310 Arguments: |
| 311 path: path to the file | 311 path: path to the file |
| 312 actions: set of actions to add to | 312 actions: set of actions to add to |
| 313 """ | 313 """ |
| 314 parser = WebUIActionsParser(actions) | 314 try: |
| 315 parser.feed(open(path).read()) | 315 parser = WebUIActionsParser(actions) |
| 316 parser.close() | 316 parser.feed(open(path).read()) |
| 317 except Exception, e: |
| 318 print "Error encountered for path %s" % path |
| 319 raise e |
| 320 finally: |
| 321 parser.close() |
| 317 | 322 |
| 318 def WalkDirectory(root_path, actions, extensions, callback): | 323 def WalkDirectory(root_path, actions, extensions, callback): |
| 319 for path, dirs, files in os.walk(root_path): | 324 for path, dirs, files in os.walk(root_path): |
| 320 if '.svn' in dirs: | 325 if '.svn' in dirs: |
| 321 dirs.remove('.svn') | 326 dirs.remove('.svn') |
| 327 if '.git' in dirs: |
| 328 dirs.remove('.git') |
| 322 for file in files: | 329 for file in files: |
| 323 ext = os.path.splitext(file)[1] | 330 ext = os.path.splitext(file)[1] |
| 324 if ext in extensions: | 331 if ext in extensions: |
| 325 callback(os.path.join(path, file), actions) | 332 callback(os.path.join(path, file), actions) |
| 326 | 333 |
| 327 def GrepForRendererActions(path, actions): | 334 def GrepForRendererActions(path, actions): |
| 328 """Grep a source file for calls to RenderThread::RecordUserMetrics. | 335 """Grep a source file for calls to RenderThread::RecordUserMetrics. |
| 329 | 336 |
| 330 Arguments: | 337 Arguments: |
| 331 path: path to the file | 338 path: path to the file |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 else: | 446 else: |
| 440 print action | 447 print action |
| 441 | 448 |
| 442 if hash_output: | 449 if hash_output: |
| 443 print "Done. Do not forget to add chromeactions.txt to your changelist" | 450 print "Done. Do not forget to add chromeactions.txt to your changelist" |
| 444 return 0 | 451 return 0 |
| 445 | 452 |
| 446 | 453 |
| 447 if '__main__' == __name__: | 454 if '__main__' == __name__: |
| 448 sys.exit(main(sys.argv)) | 455 sys.exit(main(sys.argv)) |
| OLD | NEW |