Reland #2: Switch the default build configuration to use the 10.10 SDK.
[chromium-blink-merge.git] / tools / metrics / actions / extract_actions.py
blob218c8f2afd2e26b6d7e9d345c651543859422a11
1 #!/usr/bin/env python
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Extract UserMetrics "actions" strings from the Chrome source.
9 This program generates the list of known actions we expect to see in the
10 user behavior logs. It walks the Chrome source, looking for calls to
11 UserMetrics functions, extracting actions and warning on improper calls,
12 as well as generating the lists of possible actions in situations where
13 there are many possible actions.
15 See also:
16 base/metrics/user_metrics.h
18 After extracting all actions, the content will go through a pretty print
19 function to make sure it's well formatted. If the file content needs to be
20 changed, a window will be prompted asking for user's consent. The old version
21 will also be saved in a backup file.
22 """
24 __author__ = 'evanm (Evan Martin)'
26 from HTMLParser import HTMLParser
27 import logging
28 import os
29 import re
30 import shutil
31 import sys
32 from xml.dom import minidom
34 import print_style
36 sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'python'))
37 from google import path_utils
39 # Import the metrics/common module for pretty print xml.
40 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
41 import presubmit_util
42 import diff_util
43 import pretty_print_xml
45 USER_METRICS_ACTION_RE = re.compile(r"""
46 [^a-zA-Z] # Preceded by a non-alphabetical character.
47 (?: # Begin non-capturing group.
48 UserMetricsAction # C++ / Objective C function name.
49 | # or...
50 RecordUserAction\.record # Java function name.
51 ) # End non-capturing group.
52 \( # Opening parenthesis.
53 \s* # Any amount of whitespace, including new lines.
54 (.+?) # A sequence of characters for the param.
55 \) # Closing parenthesis.
56 """,
57 re.VERBOSE | re.DOTALL # Verbose syntax and makes . also match new lines.
59 COMPUTED_ACTION_RE = re.compile(r'RecordComputedAction')
60 QUOTED_STRING_RE = re.compile(r'\"(.+?)\"')
62 # Files that are known to use content::RecordComputedAction(), which means
63 # they require special handling code in this script.
64 # To add a new file, add it to this list and add the appropriate logic to
65 # generate the known actions to AddComputedActions() below.
66 KNOWN_COMPUTED_USERS = (
67 'back_forward_menu_model.cc',
68 'options_page_view.cc',
69 'render_view_host.cc', # called using webkit identifiers
70 'user_metrics.cc', # method definition
71 'new_tab_ui.cc', # most visited clicks 1-9
72 'extension_metrics_module.cc', # extensions hook for user metrics
73 'language_options_handler_common.cc', # languages and input methods in CrOS
74 'cros_language_options_handler.cc', # languages and input methods in CrOS
75 'about_flags.cc', # do not generate a warning; see AddAboutFlagsActions()
76 'external_metrics.cc', # see AddChromeOSActions()
77 'core_options_handler.cc', # see AddWebUIActions()
78 'browser_render_process_host.cc', # see AddRendererActions()
79 'render_thread_impl.cc', # impl of RenderThread::RecordComputedAction()
80 'render_process_host_impl.cc', # browser side impl for
81 # RenderThread::RecordComputedAction()
82 'mock_render_thread.cc', # mock of RenderThread::RecordComputedAction()
83 'ppb_pdf_impl.cc', # see AddClosedSourceActions()
84 'pepper_pdf_host.cc', # see AddClosedSourceActions()
85 'record_user_action.cc', # see RecordUserAction.java
88 # Language codes used in Chrome. The list should be updated when a new
89 # language is added to app/l10n_util.cc, as follows:
91 # % (cat app/l10n_util.cc | \
92 # perl -n0e 'print $1 if /kAcceptLanguageList.*?\{(.*?)\}/s' | \
93 # perl -nle 'print $1, if /"(.*)"/'; echo 'es-419') | \
94 # sort | perl -pe "s/(.*)\n/'\$1', /" | \
95 # fold -w75 -s | perl -pe 's/^/ /;s/ $//'; echo
97 # The script extracts language codes from kAcceptLanguageList, but es-419
98 # (Spanish in Latin America) is an exception.
99 LANGUAGE_CODES = (
100 'af', 'am', 'ar', 'az', 'be', 'bg', 'bh', 'bn', 'br', 'bs', 'ca', 'co',
101 'cs', 'cy', 'da', 'de', 'de-AT', 'de-CH', 'de-DE', 'el', 'en', 'en-AU',
102 'en-CA', 'en-GB', 'en-NZ', 'en-US', 'en-ZA', 'eo', 'es', 'es-419', 'et',
103 'eu', 'fa', 'fi', 'fil', 'fo', 'fr', 'fr-CA', 'fr-CH', 'fr-FR', 'fy',
104 'ga', 'gd', 'gl', 'gn', 'gu', 'ha', 'haw', 'he', 'hi', 'hr', 'hu', 'hy',
105 'ia', 'id', 'is', 'it', 'it-CH', 'it-IT', 'ja', 'jw', 'ka', 'kk', 'km',
106 'kn', 'ko', 'ku', 'ky', 'la', 'ln', 'lo', 'lt', 'lv', 'mk', 'ml', 'mn',
107 'mo', 'mr', 'ms', 'mt', 'nb', 'ne', 'nl', 'nn', 'no', 'oc', 'om', 'or',
108 'pa', 'pl', 'ps', 'pt', 'pt-BR', 'pt-PT', 'qu', 'rm', 'ro', 'ru', 'sd',
109 'sh', 'si', 'sk', 'sl', 'sn', 'so', 'sq', 'sr', 'st', 'su', 'sv', 'sw',
110 'ta', 'te', 'tg', 'th', 'ti', 'tk', 'to', 'tr', 'tt', 'tw', 'ug', 'uk',
111 'ur', 'uz', 'vi', 'xh', 'yi', 'yo', 'zh', 'zh-CN', 'zh-TW', 'zu',
114 # Input method IDs used in Chrome OS. The list should be updated when a
115 # new input method is added to
116 # chromeos/ime/input_methods.txt in the Chrome tree, as
117 # follows:
119 # % sort chromeos/ime/input_methods.txt | \
120 # perl -ne "print \"'\$1', \" if /^([^#]+?)\s/" | \
121 # fold -w75 -s | perl -pe 's/^/ /;s/ $//'; echo
123 # The script extracts input method IDs from input_methods.txt.
124 INPUT_METHOD_IDS = (
125 'xkb:am:phonetic:arm', 'xkb:be::fra', 'xkb:be::ger', 'xkb:be::nld',
126 'xkb:bg::bul', 'xkb:bg:phonetic:bul', 'xkb:br::por', 'xkb:by::bel',
127 'xkb:ca::fra', 'xkb:ca:eng:eng', 'xkb:ca:multix:fra', 'xkb:ch::ger',
128 'xkb:ch:fr:fra', 'xkb:cz::cze', 'xkb:cz:qwerty:cze', 'xkb:de::ger',
129 'xkb:de:neo:ger', 'xkb:dk::dan', 'xkb:ee::est', 'xkb:es::spa',
130 'xkb:es:cat:cat', 'xkb:fi::fin', 'xkb:fr::fra', 'xkb:gb:dvorak:eng',
131 'xkb:gb:extd:eng', 'xkb:ge::geo', 'xkb:gr::gre', 'xkb:hr::scr',
132 'xkb:hu::hun', 'xkb:il::heb', 'xkb:is::ice', 'xkb:it::ita', 'xkb:jp::jpn',
133 'xkb:latam::spa', 'xkb:lt::lit', 'xkb:lv:apostrophe:lav', 'xkb:mn::mon',
134 'xkb:no::nob', 'xkb:pl::pol', 'xkb:pt::por', 'xkb:ro::rum', 'xkb:rs::srp',
135 'xkb:ru::rus', 'xkb:ru:phonetic:rus', 'xkb:se::swe', 'xkb:si::slv',
136 'xkb:sk::slo', 'xkb:tr::tur', 'xkb:ua::ukr', 'xkb:us::eng',
137 'xkb:us:altgr-intl:eng', 'xkb:us:colemak:eng', 'xkb:us:dvorak:eng',
138 'xkb:us:intl:eng',
141 # The path to the root of the repository.
142 REPOSITORY_ROOT = os.path.join(path_utils.ScriptDir(), '..', '..', '..')
144 number_of_files_total = 0
146 # Tags that need to be inserted to each 'action' tag and their default content.
147 TAGS = {'description': 'Please enter the description of the metric.',
148 'owner': ('Please list the metric\'s owners. Add more owner tags as '
149 'needed.')}
152 def AddComputedActions(actions):
153 """Add computed actions to the actions list.
155 Arguments:
156 actions: set of actions to add to.
159 # Actions for back_forward_menu_model.cc.
160 for dir in ('BackMenu_', 'ForwardMenu_'):
161 actions.add(dir + 'ShowFullHistory')
162 actions.add(dir + 'Popup')
163 for i in range(1, 20):
164 actions.add(dir + 'HistoryClick' + str(i))
165 actions.add(dir + 'ChapterClick' + str(i))
167 # Actions for new_tab_ui.cc.
168 for i in range(1, 10):
169 actions.add('MostVisited%d' % i)
171 # Actions for language_options_handler.cc (Chrome OS specific).
172 for input_method_id in INPUT_METHOD_IDS:
173 actions.add('LanguageOptions_DisableInputMethod_%s' % input_method_id)
174 actions.add('LanguageOptions_EnableInputMethod_%s' % input_method_id)
175 for language_code in LANGUAGE_CODES:
176 actions.add('LanguageOptions_UiLanguageChange_%s' % language_code)
177 actions.add('LanguageOptions_SpellCheckLanguageChange_%s' % language_code)
179 def AddWebKitEditorActions(actions):
180 """Add editor actions from editor_client_impl.cc.
182 Arguments:
183 actions: set of actions to add to.
185 action_re = re.compile(r'''\{ [\w']+, +\w+, +"(.*)" +\},''')
187 editor_file = os.path.join(REPOSITORY_ROOT, 'webkit', 'api', 'src',
188 'EditorClientImpl.cc')
189 for line in open(editor_file):
190 match = action_re.search(line)
191 if match: # Plain call to RecordAction
192 actions.add(match.group(1))
194 def AddClosedSourceActions(actions):
195 """Add actions that are in code which is not checked out by default
197 Arguments
198 actions: set of actions to add to.
200 actions.add('PDF.FitToHeightButton')
201 actions.add('PDF.FitToWidthButton')
202 actions.add('PDF.LoadFailure')
203 actions.add('PDF.LoadSuccess')
204 actions.add('PDF.PreviewDocumentLoadFailure')
205 actions.add('PDF.PrintButton')
206 actions.add('PDF.PrintPage')
207 actions.add('PDF.SaveButton')
208 actions.add('PDF.ZoomFromBrowser')
209 actions.add('PDF.ZoomInButton')
210 actions.add('PDF.ZoomOutButton')
211 actions.add('PDF_Unsupported_3D')
212 actions.add('PDF_Unsupported_Attachment')
213 actions.add('PDF_Unsupported_Bookmarks')
214 actions.add('PDF_Unsupported_Digital_Signature')
215 actions.add('PDF_Unsupported_Movie')
216 actions.add('PDF_Unsupported_Portfolios_Packages')
217 actions.add('PDF_Unsupported_Rights_Management')
218 actions.add('PDF_Unsupported_Screen')
219 actions.add('PDF_Unsupported_Shared_Form')
220 actions.add('PDF_Unsupported_Shared_Review')
221 actions.add('PDF_Unsupported_Sound')
222 actions.add('PDF_Unsupported_XFA')
224 def AddAboutFlagsActions(actions):
225 """This parses the experimental feature flags for UMA actions.
227 Arguments:
228 actions: set of actions to add to.
230 about_flags = os.path.join(REPOSITORY_ROOT, 'chrome', 'browser',
231 'about_flags.cc')
232 flag_name_re = re.compile(r'\s*"([0-9a-zA-Z\-_]+)",\s*// FLAGS:RECORD_UMA')
233 for line in open(about_flags):
234 match = flag_name_re.search(line)
235 if match:
236 actions.add("AboutFlags_" + match.group(1))
237 # If the line contains the marker but was not matched by the regex, put up
238 # an error if the line is not a comment.
239 elif 'FLAGS:RECORD_UMA' in line and line[0:2] != '//':
240 print >>sys.stderr, 'WARNING: This line is marked for recording ' + \
241 'about:flags metrics, but is not in the proper format:\n' + line
243 def AddBookmarkManagerActions(actions):
244 """Add actions that are used by BookmarkManager.
246 Arguments
247 actions: set of actions to add to.
249 actions.add('BookmarkManager_Command_AddPage')
250 actions.add('BookmarkManager_Command_Copy')
251 actions.add('BookmarkManager_Command_Cut')
252 actions.add('BookmarkManager_Command_Delete')
253 actions.add('BookmarkManager_Command_Edit')
254 actions.add('BookmarkManager_Command_Export')
255 actions.add('BookmarkManager_Command_Import')
256 actions.add('BookmarkManager_Command_NewFolder')
257 actions.add('BookmarkManager_Command_OpenIncognito')
258 actions.add('BookmarkManager_Command_OpenInNewTab')
259 actions.add('BookmarkManager_Command_OpenInNewWindow')
260 actions.add('BookmarkManager_Command_OpenInSame')
261 actions.add('BookmarkManager_Command_Paste')
262 actions.add('BookmarkManager_Command_ShowInFolder')
263 actions.add('BookmarkManager_Command_Sort')
264 actions.add('BookmarkManager_Command_UndoDelete')
265 actions.add('BookmarkManager_Command_UndoGlobal')
266 actions.add('BookmarkManager_Command_UndoNone')
268 actions.add('BookmarkManager_NavigateTo_BookmarkBar')
269 actions.add('BookmarkManager_NavigateTo_Mobile')
270 actions.add('BookmarkManager_NavigateTo_Other')
271 actions.add('BookmarkManager_NavigateTo_Recent')
272 actions.add('BookmarkManager_NavigateTo_Search')
273 actions.add('BookmarkManager_NavigateTo_SubFolder')
275 def AddChromeOSActions(actions):
276 """Add actions reported by non-Chrome processes in Chrome OS.
278 Arguments:
279 actions: set of actions to add to.
281 # Actions sent by Chrome OS update engine.
282 actions.add('Updater.ServerCertificateChanged')
283 actions.add('Updater.ServerCertificateFailed')
285 # Actions sent by Chrome OS cryptohome.
286 actions.add('Cryptohome.PKCS11InitFail')
288 def AddExtensionActions(actions):
289 """Add actions reported by extensions via chrome.metricsPrivate API.
291 Arguments:
292 actions: set of actions to add to.
294 # Actions sent by Chrome OS File Browser.
295 actions.add('FileBrowser.CreateNewFolder')
296 actions.add('FileBrowser.PhotoEditor.Edit')
297 actions.add('FileBrowser.PhotoEditor.View')
298 actions.add('FileBrowser.SuggestApps.ShowDialog')
300 # Actions sent by Google Now client.
301 actions.add('GoogleNow.MessageClicked')
302 actions.add('GoogleNow.ButtonClicked0')
303 actions.add('GoogleNow.ButtonClicked1')
304 actions.add('GoogleNow.Dismissed')
306 # Actions sent by Chrome Connectivity Diagnostics.
307 actions.add('ConnectivityDiagnostics.LaunchSource.OfflineChromeOS')
308 actions.add('ConnectivityDiagnostics.LaunchSource.WebStore')
309 actions.add('ConnectivityDiagnostics.UA.LogsShown')
310 actions.add('ConnectivityDiagnostics.UA.PassingTestsShown')
311 actions.add('ConnectivityDiagnostics.UA.SettingsShown')
312 actions.add('ConnectivityDiagnostics.UA.TestResultExpanded')
313 actions.add('ConnectivityDiagnostics.UA.TestSuiteRun')
315 # Actions sent by 'Ok Google' Hotwording.
316 actions.add('Hotword.HotwordTrigger')
319 class InvalidStatementException(Exception):
320 """Indicates an invalid statement was found."""
323 class ActionNameFinder:
324 """Helper class to find action names in source code file."""
326 def __init__(self, path, contents):
327 self.__path = path
328 self.__pos = 0
329 self.__contents = contents
331 def FindNextAction(self):
332 """Finds the next action name in the file.
334 Returns:
335 The name of the action found or None if there are no more actions.
336 Raises:
337 InvalidStatementException if the next action statement is invalid
338 and could not be parsed. There may still be more actions in the file,
339 so FindNextAction() can continue to be called to find following ones.
341 match = USER_METRICS_ACTION_RE.search(self.__contents, pos=self.__pos)
342 if not match:
343 return None
344 match_start = match.start()
345 self.__pos = match.end()
346 match = QUOTED_STRING_RE.match(match.group(1))
347 if not match:
348 self._RaiseException(match_start, self.__pos)
349 return match.group(1)
351 def _RaiseException(self, match_start, match_end):
352 """Raises an InvalidStatementException for the specified code range."""
353 line_number = self.__contents.count('\n', 0, match_start) + 1
354 # Add 1 to |match_start| since the RE checks the preceding character.
355 statement = self.__contents[match_start + 1:match_end]
356 raise InvalidStatementException(
357 '%s uses UserMetricsAction incorrectly on line %d:\n%s' %
358 (self.__path, line_number, statement))
361 def GrepForActions(path, actions):
362 """Grep a source file for calls to UserMetrics functions.
364 Arguments:
365 path: path to the file
366 actions: set of actions to add to
368 global number_of_files_total
369 number_of_files_total = number_of_files_total + 1
371 finder = ActionNameFinder(path, open(path).read())
372 while True:
373 try:
374 action_name = finder.FindNextAction()
375 if not action_name:
376 break
377 actions.add(action_name)
378 except InvalidStatementException, e:
379 logging.warning(str(e))
381 line_number = 0
382 for line in open(path):
383 line_number = line_number + 1
384 if COMPUTED_ACTION_RE.search(line):
385 # Warn if this file shouldn't be calling RecordComputedAction.
386 if os.path.basename(path) not in KNOWN_COMPUTED_USERS:
387 logging.warning('%s has RecordComputedAction statement on line %d' %
388 (path, line_number))
390 class WebUIActionsParser(HTMLParser):
391 """Parses an HTML file, looking for all tags with a 'metric' attribute.
392 Adds user actions corresponding to any metrics found.
394 Arguments:
395 actions: set of actions to add to
397 def __init__(self, actions):
398 HTMLParser.__init__(self)
399 self.actions = actions
401 def handle_starttag(self, tag, attrs):
402 # We only care to examine tags that have a 'metric' attribute.
403 attrs = dict(attrs)
404 if not 'metric' in attrs:
405 return
407 # Boolean metrics have two corresponding actions. All other metrics have
408 # just one corresponding action. By default, we check the 'dataType'
409 # attribute.
410 is_boolean = ('dataType' in attrs and attrs['dataType'] == 'boolean')
411 if 'type' in attrs and attrs['type'] in ('checkbox', 'radio'):
412 if attrs['type'] == 'checkbox':
413 is_boolean = True
414 else:
415 # Radio buttons are boolean if and only if their values are 'true' or
416 # 'false'.
417 assert(attrs['type'] == 'radio')
418 if 'value' in attrs and attrs['value'] in ['true', 'false']:
419 is_boolean = True
421 if is_boolean:
422 self.actions.add(attrs['metric'] + '_Enable')
423 self.actions.add(attrs['metric'] + '_Disable')
424 else:
425 self.actions.add(attrs['metric'])
427 def GrepForWebUIActions(path, actions):
428 """Grep a WebUI source file for elements with associated metrics.
430 Arguments:
431 path: path to the file
432 actions: set of actions to add to
434 close_called = False
435 try:
436 parser = WebUIActionsParser(actions)
437 parser.feed(open(path).read())
438 # An exception can be thrown by parser.close(), so do it in the try to
439 # ensure the path of the file being parsed gets printed if that happens.
440 close_called = True
441 parser.close()
442 except Exception, e:
443 print "Error encountered for path %s" % path
444 raise e
445 finally:
446 if not close_called:
447 parser.close()
449 def WalkDirectory(root_path, actions, extensions, callback):
450 for path, dirs, files in os.walk(root_path):
451 if '.svn' in dirs:
452 dirs.remove('.svn')
453 if '.git' in dirs:
454 dirs.remove('.git')
455 for file in files:
456 ext = os.path.splitext(file)[1]
457 if ext in extensions:
458 callback(os.path.join(path, file), actions)
460 def AddLiteralActions(actions):
461 """Add literal actions specified via calls to UserMetrics functions.
463 Arguments:
464 actions: set of actions to add to.
466 EXTENSIONS = ('.cc', '.mm', '.c', '.m', '.java')
468 # Walk the source tree to process all files.
469 ash_root = os.path.normpath(os.path.join(REPOSITORY_ROOT, 'ash'))
470 WalkDirectory(ash_root, actions, EXTENSIONS, GrepForActions)
471 chrome_root = os.path.normpath(os.path.join(REPOSITORY_ROOT, 'chrome'))
472 WalkDirectory(chrome_root, actions, EXTENSIONS, GrepForActions)
473 content_root = os.path.normpath(os.path.join(REPOSITORY_ROOT, 'content'))
474 WalkDirectory(content_root, actions, EXTENSIONS, GrepForActions)
475 components_root = os.path.normpath(os.path.join(REPOSITORY_ROOT,
476 'components'))
477 WalkDirectory(components_root, actions, EXTENSIONS, GrepForActions)
478 net_root = os.path.normpath(os.path.join(REPOSITORY_ROOT, 'net'))
479 WalkDirectory(net_root, actions, EXTENSIONS, GrepForActions)
480 webkit_root = os.path.normpath(os.path.join(REPOSITORY_ROOT, 'webkit'))
481 WalkDirectory(os.path.join(webkit_root, 'glue'), actions, EXTENSIONS,
482 GrepForActions)
483 WalkDirectory(os.path.join(webkit_root, 'port'), actions, EXTENSIONS,
484 GrepForActions)
486 def AddWebUIActions(actions):
487 """Add user actions defined in WebUI files.
489 Arguments:
490 actions: set of actions to add to.
492 resources_root = os.path.join(REPOSITORY_ROOT, 'chrome', 'browser',
493 'resources')
494 WalkDirectory(resources_root, actions, ('.html'), GrepForWebUIActions)
496 def AddHistoryPageActions(actions):
497 """Add actions that are used in History page.
499 Arguments
500 actions: set of actions to add to.
502 actions.add('HistoryPage_BookmarkStarClicked')
503 actions.add('HistoryPage_EntryMenuRemoveFromHistory')
504 actions.add('HistoryPage_EntryLinkClick')
505 actions.add('HistoryPage_EntryLinkRightClick')
506 actions.add('HistoryPage_SearchResultClick')
507 actions.add('HistoryPage_EntryMenuShowMoreFromSite')
508 actions.add('HistoryPage_NewestHistoryClick')
509 actions.add('HistoryPage_NewerHistoryClick')
510 actions.add('HistoryPage_OlderHistoryClick')
511 actions.add('HistoryPage_Search')
512 actions.add('HistoryPage_InitClearBrowsingData')
513 actions.add('HistoryPage_RemoveSelected')
514 actions.add('HistoryPage_SearchResultRemove')
515 actions.add('HistoryPage_ConfirmRemoveSelected')
516 actions.add('HistoryPage_CancelRemoveSelected')
518 def AddAutomaticResetBannerActions(actions):
519 """Add actions that are used for the automatic profile settings reset banners
520 in chrome://settings.
522 Arguments
523 actions: set of actions to add to.
525 # These actions relate to the the automatic settings reset banner shown as
526 # a result of the reset prompt.
527 actions.add('AutomaticReset_WebUIBanner_BannerShown')
528 actions.add('AutomaticReset_WebUIBanner_ManuallyClosed')
529 actions.add('AutomaticReset_WebUIBanner_ResetClicked')
531 # These actions relate to the the automatic settings reset banner shown as
532 # a result of settings hardening.
533 actions.add('AutomaticSettingsReset_WebUIBanner_BannerShown')
534 actions.add('AutomaticSettingsReset_WebUIBanner_ManuallyClosed')
535 actions.add('AutomaticSettingsReset_WebUIBanner_LearnMoreClicked')
536 actions.add('AutomaticSettingsReset_WebUIBanner_ResetClicked')
539 class Error(Exception):
540 pass
543 def _ExtractText(parent_dom, tag_name):
544 """Extract the text enclosed by |tag_name| under |parent_dom|
546 Args:
547 parent_dom: The parent Element under which text node is searched for.
548 tag_name: The name of the tag which contains a text node.
550 Returns:
551 A (list of) string enclosed by |tag_name| under |parent_dom|.
553 texts = []
554 for child_dom in parent_dom.getElementsByTagName(tag_name):
555 text_dom = child_dom.childNodes
556 if text_dom.length != 1:
557 raise Error('More than 1 child node exists under %s' % tag_name)
558 if text_dom[0].nodeType != minidom.Node.TEXT_NODE:
559 raise Error('%s\'s child node is not a text node.' % tag_name)
560 texts.append(text_dom[0].data)
561 return texts
564 class Action(object):
565 def __init__(self, name, description, owners,
566 not_user_triggered=False, obsolete=None):
567 self.name = name
568 self.description = description
569 self.owners = owners
570 self.not_user_triggered = not_user_triggered
571 self.obsolete = obsolete
574 def ParseActionFile(file_content):
575 """Parse the XML data currently stored in the file.
577 Args:
578 file_content: a string containing the action XML file content.
580 Returns:
581 (actions, actions_dict) actions is a set with all user actions' names.
582 actions_dict is a dict from user action name to Action object.
584 dom = minidom.parseString(file_content)
586 comment_nodes = []
587 # Get top-level comments. It is assumed that all comments are placed before
588 # <acionts> tag. Therefore the loop will stop if it encounters a non-comment
589 # node.
590 for node in dom.childNodes:
591 if node.nodeType == minidom.Node.COMMENT_NODE:
592 comment_nodes.append(node)
593 else:
594 break
596 actions = set()
597 actions_dict = {}
598 # Get each user action data.
599 for action_dom in dom.getElementsByTagName('action'):
600 action_name = action_dom.getAttribute('name')
601 not_user_triggered = bool(action_dom.getAttribute('not_user_triggered'))
602 actions.add(action_name)
604 owners = _ExtractText(action_dom, 'owner')
605 # There is only one description for each user action. Get the first element
606 # of the returned list.
607 description_list = _ExtractText(action_dom, 'description')
608 if len(description_list) > 1:
609 logging.error('user actions "%s" has more than one descriptions. Exactly '
610 'one description is needed for each user action. Please '
611 'fix.', action_name)
612 sys.exit(1)
613 description = description_list[0] if description_list else None
614 # There is at most one obsolete tag for each user action.
615 obsolete_list = _ExtractText(action_dom, 'obsolete')
616 if len(obsolete_list) > 1:
617 logging.error('user actions "%s" has more than one obsolete tag. At most '
618 'one obsolete tag can be added for each user action. Please'
619 ' fix.', action_name)
620 sys.exit(1)
621 obsolete = obsolete_list[0] if obsolete_list else None
622 actions_dict[action_name] = Action(action_name, description, owners,
623 not_user_triggered, obsolete)
624 return actions, actions_dict, comment_nodes
627 def _CreateActionTag(doc, action_name, action_object):
628 """Create a new action tag.
630 Format of an action tag:
631 <action name="name" not_user_triggered="true">
632 <owner>Owner</owner>
633 <description>Description.</description>
634 <obsolete>Deprecated.</obsolete>
635 </action>
637 not_user_triggered is an optional attribute. If set, it implies that the
638 belonging action is not a user action. A user action is an action that
639 is logged exactly once right after a user has made an action.
641 <obsolete> is an optional tag. It's added to actions that are no longer used
642 any more.
644 If action_name is in actions_dict, the values to be inserted are based on the
645 corresponding Action object. If action_name is not in actions_dict, the
646 default value from TAGS is used.
648 Args:
649 doc: The document under which the new action tag is created.
650 action_name: The name of an action.
651 action_object: An action object representing the data to be inserted.
653 Returns:
654 An action tag Element with proper children elements.
656 action_dom = doc.createElement('action')
657 action_dom.setAttribute('name', action_name)
659 # Add not_user_triggered attribute.
660 if action_object and action_object.not_user_triggered:
661 action_dom.setAttribute('not_user_triggered', 'true')
663 # Create owner tag.
664 if action_object and action_object.owners:
665 # If owners for this action is not None, use the stored value. Otherwise,
666 # use the default value.
667 for owner in action_object.owners:
668 owner_dom = doc.createElement('owner')
669 owner_dom.appendChild(doc.createTextNode(owner))
670 action_dom.appendChild(owner_dom)
671 else:
672 # Use default value.
673 owner_dom = doc.createElement('owner')
674 owner_dom.appendChild(doc.createTextNode(TAGS.get('owner', '')))
675 action_dom.appendChild(owner_dom)
677 # Create description tag.
678 description_dom = doc.createElement('description')
679 action_dom.appendChild(description_dom)
680 if action_object and action_object.description:
681 # If description for this action is not None, use the store value.
682 # Otherwise, use the default value.
683 description_dom.appendChild(doc.createTextNode(
684 action_object.description))
685 else:
686 description_dom.appendChild(doc.createTextNode(
687 TAGS.get('description', '')))
689 # Create obsolete tag.
690 if action_object and action_object.obsolete:
691 obsolete_dom = doc.createElement('obsolete')
692 action_dom.appendChild(obsolete_dom)
693 obsolete_dom.appendChild(doc.createTextNode(
694 action_object.obsolete))
696 return action_dom
699 def PrettyPrint(actions, actions_dict, comment_nodes=[]):
700 """Given a list of action data, create a well-printed minidom document.
702 Args:
703 actions: A list of action names.
704 actions_dict: A mappting from action name to Action object.
706 Returns:
707 A well-printed minidom document that represents the input action data.
709 doc = minidom.Document()
711 # Attach top-level comments.
712 for node in comment_nodes:
713 doc.appendChild(node)
715 actions_element = doc.createElement('actions')
716 doc.appendChild(actions_element)
718 # Attach action node based on updated |actions|.
719 for action in sorted(actions):
720 actions_element.appendChild(
721 _CreateActionTag(doc, action, actions_dict.get(action, None)))
723 return print_style.GetPrintStyle().PrettyPrintNode(doc)
726 def UpdateXml(original_xml):
727 actions, actions_dict, comment_nodes = ParseActionFile(original_xml)
729 AddComputedActions(actions)
730 # TODO(fmantek): bring back webkit editor actions.
731 # AddWebKitEditorActions(actions)
732 AddAboutFlagsActions(actions)
733 AddWebUIActions(actions)
735 AddLiteralActions(actions)
737 # print "Scanned {0} number of files".format(number_of_files_total)
738 # print "Found {0} entries".format(len(actions))
740 AddAutomaticResetBannerActions(actions)
741 AddBookmarkManagerActions(actions)
742 AddChromeOSActions(actions)
743 AddClosedSourceActions(actions)
744 AddExtensionActions(actions)
745 AddHistoryPageActions(actions)
747 return PrettyPrint(actions, actions_dict, comment_nodes)
750 def main(argv):
751 presubmit_util.DoPresubmitMain(argv, 'actions.xml', 'actions.old.xml',
752 'extract_actions.py', UpdateXml)
754 if '__main__' == __name__:
755 sys.exit(main(sys.argv))