def CollectTranslatedStrings():
    """Collects all the translations for all the strings specified by kStringIds.
  Returns a list of tuples of (string_id, language, translated string). The
  list is sorted by language codes."""
    kGeneratedResourcesPath = os.path.join(path_utils.ScriptDir(), '..', '..',
                                           '..',
                                           'app/google_chrome_strings.grd')
    kTranslationDirectory = os.path.join(path_utils.ScriptDir(), '..', '..',
                                         '..', 'app', 'resources')
    kTranslationFiles = glob.glob(
        os.path.join(kTranslationDirectory, 'google_chrome_strings*.xtb'))

    # Get the strings out of generated_resources.grd.
    dom = minidom.parse(kGeneratedResourcesPath)
    # message_nodes is a list of message dom nodes corresponding to the string
    # ids we care about.  We want to make sure that this list is in the same
    # order as kStringIds so we can associate them together.
    message_nodes = []
    all_message_nodes = dom.getElementsByTagName('message')
    for string_id in kStringIds:
        message_nodes.append([
            x for x in all_message_nodes if x.getAttribute('name') == string_id
        ][0])
    message_texts = [
        node.firstChild.nodeValue.strip() for node in message_nodes
    ]

    # The fingerprint of the string is the message ID in the translation files
    # (xtb files).
    translation_ids = [str(FP.FingerPrint(text)) for text in message_texts]

    # Manually put _EN_US in the list of translated strings because it doesn't
    # have a .xtb file.
    translated_strings = []
    for string_id, message_text in zip(kStringIds, message_texts):
        translated_strings.append(
            TranslationStruct(string_id + '_EN_US', 'EN_US', message_text))

    # Gather the translated strings from the .xtb files.  If an .xtb file doesn't
    # have the string we want, use the en-US string.
    for xtb_filename in kTranslationFiles:
        dom = minidom.parse(xtb_filename)
        language = dom.documentElement.getAttribute('lang')
        language = language.replace('-', '_').upper()
        translation_nodes = {}
        for translation_node in dom.getElementsByTagName('translation'):
            translation_id = translation_node.getAttribute('id')
            if translation_id in translation_ids:
                translation_nodes[translation_id] = (
                    translation_node.firstChild.nodeValue.strip())
        for i, string_id in enumerate(kStringIds):
            translated_string = translation_nodes.get(translation_ids[i],
                                                      message_texts[i])
            translated_strings.append(
                TranslationStruct(string_id + '_' + language, language,
                                  translated_string))

    translated_strings.sort()
    return translated_strings
Пример #2
0
def main(argv):
    if '--hash' in argv:
        hash_output = True
    else:
        hash_output = False
        print >>sys.stderr, "WARNING: if you added new UMA tags, you need to" + \
               " override the chromeactions.txt file and use the --hash option"
    # if we do a hash output, we want to only append NEW actions, and we know
    # the file we want to work on
    actions = set()

    if hash_output:
        f = open("chromeactions.txt")
        for line in f:
            part = line.rpartition("\t")
            part = part[2].strip()
            actions.add(part)
        f.close()

    AddComputedActions(actions)
    # TODO(fmantek): bring back webkit editor actions.
    # AddWebKitEditorActions(actions)
    AddAboutFlagsActions(actions)

    # Walk the source tree to process all .cc files.
    chrome_root = os.path.join(path_utils.ScriptDir(), '..')
    WalkDirectory(chrome_root, actions)
    webkit_root = os.path.join(path_utils.ScriptDir(), '..', '..', 'webkit')
    WalkDirectory(os.path.join(webkit_root, 'glue'), actions)
    WalkDirectory(os.path.join(webkit_root, 'port'), actions)

    # print "Scanned {0} number of files".format(number_of_files_total)
    # print "Found {0} entries".format(len(actions))

    AddClosedSourceActions(actions)

    if hash_output:
        f = open("chromeactions.txt", "w")

    # Print out the actions as a sorted list.
    for action in sorted(actions):
        if hash_output:
            hash = hashlib.md5()
            hash.update(action)
            print >> f, '0x%s\t%s' % (hash.hexdigest()[:16], action)
        else:
            print action

    if hash_output:
        print "Done. Do not forget to add chromeactions.txt to your changelist"
Пример #3
0
def AddRendererActions(actions):
  """Add user actions sent via calls to RenderThread::RecordUserMetrics.

  Arguments:
    actions: set of actions to add to.
  """
  EXTENSIONS = ('.cc', '.mm', '.c', '.m')

  chrome_renderer_root = os.path.join(path_utils.ScriptDir(), '..', 'renderer')
  content_renderer_root = os.path.join(path_utils.ScriptDir(), '..', '..',
      'content', 'renderer')
  WalkDirectory(chrome_renderer_root, actions, EXTENSIONS,
                GrepForRendererActions)
  WalkDirectory(content_renderer_root, actions, EXTENSIONS,
                GrepForRendererActions)
Пример #4
0
def main(argv):
    actions = set()
    AddComputedActions(actions)
    AddWebKitEditorActions(actions)

    # Walk the source tree to process all .cc files.
    chrome_root = os.path.join(path_utils.ScriptDir(), '..')
    WalkDirectory(chrome_root, actions)
    webkit_root = os.path.join(path_utils.ScriptDir(), '..', '..', 'webkit')
    WalkDirectory(os.path.join(webkit_root, 'glue'), actions)
    WalkDirectory(os.path.join(webkit_root, 'port'), actions)

    # Print out the actions as a sorted list.
    for action in sorted(actions):
        print action
Пример #5
0
def AddWebUIActions(actions):
  """Add user actions defined in WebUI files.

  Arguments:
    actions: set of actions to add to.
  """
  resources_root = os.path.join(path_utils.ScriptDir(), '..', 'browser',
                                'resources')
  WalkDirectory(resources_root, actions, ('.html'), GrepForWebUIActions)
def main(argv):
    if '--hash' in argv:
        hash_output = True
    else:
        hash_output = False
        print >>sys.stderr, "WARNING: If you added new UMA tags, you must" + \
               " use the --hash option to update chromeactions.txt."
    # if we do a hash output, we want to only append NEW actions, and we know
    # the file we want to work on
    actions = set()

    chromeactions_path = os.path.join(path_utils.ScriptDir(),
                                      "chromeactions.txt")

    if hash_output:
        f = open(chromeactions_path)
        for line in f:
            part = line.rpartition("\t")
            part = part[2].strip()
            actions.add(part)
        f.close()

    AddComputedActions(actions)
    # TODO(fmantek): bring back webkit editor actions.
    # AddWebKitEditorActions(actions)
    AddAboutFlagsActions(actions)
    AddWebUIActions(actions)

    AddLiteralActions(actions)

    # print "Scanned {0} number of files".format(number_of_files_total)
    # print "Found {0} entries".format(len(actions))

    AddAndroidActions(actions)
    AddAutomaticResetBannerActions(actions)
    AddBookmarkManagerActions(actions)
    AddChromeOSActions(actions)
    AddClosedSourceActions(actions)
    AddExtensionActions(actions)
    AddHistoryPageActions(actions)
    AddKeySystemSupportActions(actions)

    if hash_output:
        f = open(chromeactions_path, "wb")

    # Print out the actions as a sorted list.
    for action in sorted(actions):
        if hash_output:
            hash = hashlib.md5()
            hash.update(action)
            print >> f, '0x%s\t%s' % (hash.hexdigest()[:16], action)
        else:
            print action

    if hash_output:
        print "Done. Do not forget to add chromeactions.txt to your changelist"
    return 0
Пример #7
0
def main():
    logging.basicConfig(level=logging.INFO)

    presubmit = ('--presubmit' in sys.argv)

    histograms_filename = 'histograms.xml'
    histograms_backup_filename = 'histograms.before.pretty-print.xml'

    # If there is a histograms.xml in the current working directory, use that.
    # Otherwise, use the one residing in the same directory as this script.
    histograms_dir = os.getcwd()
    if not os.path.isfile(os.path.join(histograms_dir, histograms_filename)):
        histograms_dir = path_utils.ScriptDir()

    histograms_pathname = os.path.join(histograms_dir, histograms_filename)
    histograms_backup_pathname = os.path.join(histograms_dir,
                                              histograms_backup_filename)

    logging.info('Loading %s...' % os.path.relpath(histograms_pathname))
    with open(histograms_pathname, 'rb') as f:
        xml = f.read()

    # Check there are no CR ('\r') characters in the file.
    if '\r' in xml:
        logging.info(
            'DOS-style line endings (CR characters) detected - these are '
            'not allowed. Please run dos2unix %s' % histograms_filename)
        sys.exit(1)

    logging.info('Pretty-printing...')
    try:
        pretty = PrettyPrint(xml)
    except Error:
        logging.error('Aborting parsing due to fatal errors.')
        sys.exit(1)

    if xml == pretty:
        logging.info('%s is correctly pretty-printed.' % histograms_filename)
        sys.exit(0)
    if presubmit:
        logging.info(
            '%s is not formatted correctly; run pretty_print.py to fix.' %
            histograms_filename)
        sys.exit(1)
    if not diff_util.PromptUserToAcceptDiff(
            xml, pretty, 'Is the prettified version acceptable?'):
        logging.error('Aborting')
        return

    logging.info('Creating backup file %s' % histograms_backup_filename)
    shutil.move(histograms_pathname, histograms_backup_pathname)

    logging.info('Writing new %s file' % histograms_filename)
    with open(histograms_pathname, 'wb') as f:
        f.write(pretty)
Пример #8
0
def AddLiteralActions(actions):
  """Add literal actions specified via calls to UserMetrics functions.

  Arguments:
    actions: set of actions to add to.
  """
  EXTENSIONS = ('.cc', '.mm', '.c', '.m')

  # Walk the source tree to process all .cc files.
  chrome_root = os.path.normpath(os.path.join(path_utils.ScriptDir(), '..'))
  WalkDirectory(chrome_root, actions, EXTENSIONS, GrepForActions)
  content_root = os.path.normpath(os.path.join(path_utils.ScriptDir(),
                                               '..', '..', 'content'))
  WalkDirectory(content_root, actions, EXTENSIONS, GrepForActions)
  webkit_root = os.path.normpath(os.path.join(path_utils.ScriptDir(),
                                              '..', '..', 'webkit'))
  WalkDirectory(os.path.join(webkit_root, 'glue'), actions, EXTENSIONS,
                GrepForActions)
  WalkDirectory(os.path.join(webkit_root, 'port'), actions, EXTENSIONS,
                GrepForActions)
Пример #9
0
def main(argv):
  presubmit = ('--presubmit' in argv)
  actions_xml_path = os.path.join(path_utils.ScriptDir(), 'actions.xml')

  # Save the original file content.
  with open(actions_xml_path, 'rb') as f:
    original_xml = f.read()

  actions, actions_dict, comment_nodes = ParseActionFile(original_xml)

  AddComputedActions(actions)
  # TODO(fmantek): bring back webkit editor actions.
  # AddWebKitEditorActions(actions)
  AddAboutFlagsActions(actions)
  AddWebUIActions(actions)

  AddLiteralActions(actions)

  # print "Scanned {0} number of files".format(number_of_files_total)
  # print "Found {0} entries".format(len(actions))

  AddAndroidActions(actions)
  AddAutomaticResetBannerActions(actions)
  AddBookmarkManagerActions(actions)
  AddChromeOSActions(actions)
  AddClosedSourceActions(actions)
  AddExtensionActions(actions)
  AddHistoryPageActions(actions)
  AddKeySystemSupportActions(actions)

  pretty = PrettyPrint(actions, actions_dict, comment_nodes)
  if original_xml == pretty:
    print 'actions.xml is correctly pretty-printed.'
    sys.exit(0)
  if presubmit:
    logging.info('actions.xml is not formatted correctly; run '
                 'extract_actions.py to fix.')
    sys.exit(1)

  # Prompt user to consent on the change.
  if not diff_util.PromptUserToAcceptDiff(
      original_xml, pretty, 'Is the new version acceptable?'):
    logging.error('Aborting')
    sys.exit(1)

  print 'Creating backup file: actions.old.xml.'
  shutil.move(actions_xml_path, 'actions.old.xml')

  with open(actions_xml_path, 'wb') as f:
    f.write(pretty)
  print ('Updated %s. Don\'t forget to add it to your changelist' %
         actions_xml_path)
  return 0
Пример #10
0
def AddWebKitEditorActions(actions):
    """Add editor actions from editor_client_impl.cc.

  Arguments:
    actions: set of actions to add to.
  """
    action_re = re.compile(r'''\{ [\w']+, +\w+, +"(.*)" +\},''')

    editor_file = os.path.join(path_utils.ScriptDir(), '..', '..', 'webkit',
                               'api', 'src', 'EditorClientImpl.cc')
    for line in open(editor_file):
        match = action_re.search(line)
        if match:  # Plain call to RecordAction
            actions.add(match.group(1))
Пример #11
0
def AddAboutFlagsActions(actions):
    """This parses the experimental feature flags for UMA actions.

  Arguments:
    actions: set of actions to add to.
  """
    about_flags = os.path.join(path_utils.ScriptDir(), '..', 'browser',
                               'about_flags.cc')
    flag_name_re = re.compile(r'\s*"([0-9a-zA-Z\-_]+)",\s*// FLAGS:RECORD_UMA')
    for line in open(about_flags):
        match = flag_name_re.search(line)
        if match:
            actions.add("AboutFlags_" + match.group(1))
        # If the line contains the marker but was not matched by the regex, put up
        # an error if the line is not a comment.
        elif 'FLAGS:RECORD_UMA' in line and line[0:2] != '//':
            print >>sys.stderr, 'WARNING: This line is marked for recording ' + \
                'about:flags metrics, but is not in the proper format:\n' + line
    'xkb:ru::rus',
    'xkb:ru:phonetic:rus',
    'xkb:se::swe',
    'xkb:si::slv',
    'xkb:sk::slo',
    'xkb:tr::tur',
    'xkb:ua::ukr',
    'xkb:us::eng',
    'xkb:us:altgr-intl:eng',
    'xkb:us:colemak:eng',
    'xkb:us:dvorak:eng',
    'xkb:us:intl:eng',
)

# The path to the root of the repository.
REPOSITORY_ROOT = os.path.join(path_utils.ScriptDir(), '..', '..', '..')

number_of_files_total = 0


def AddComputedActions(actions):
    """Add computed actions to the actions list.

  Arguments:
    actions: set of actions to add to.
  """

    # Actions for back_forward_menu_model.cc.
    for dir in ('BackMenu_', 'ForwardMenu_'):
        actions.add(dir + 'ShowFullHistory')
        actions.add(dir + 'Popup')
Пример #13
0
def DoPresubmitMain(argv, original_filename, backup_filename, script_name,
                    prettyFn):
    """Execute presubmit/pretty printing for the target file.

  Args:
    argv: command line arguments
    original_filename: The filename to read from.
    backup_filename: When pretty printing, move the old file contents here.
    script_name: The name of the script to run for pretty printing.
    prettyFn: A function which takes the original xml content and produces
        pretty printed xml.

  Returns:
    An exit status.  Non-zero indicates errors.
  """
    logging.basicConfig(level=logging.INFO)
    presubmit = ('--presubmit' in argv)

    # If there is a description xml in the current working directory, use that.
    # Otherwise, use the one residing in the same directory as this script.
    xml_dir = os.getcwd()
    if not os.path.isfile(os.path.join(xml_dir, original_filename)):
        xml_dir = path_utils.ScriptDir()

    xml_path = os.path.join(xml_dir, original_filename)

    # Save the original file content.
    logging.info('Loading %s...', os.path.relpath(xml_path))
    with open(xml_path, 'rb') as f:
        original_xml = f.read()

    # Check there are no CR ('\r') characters in the file.
    if '\r' in original_xml:
        logging.error(
            'DOS-style line endings (CR characters) detected - these are '
            'not allowed. Please run dos2unix %s', original_filename)
        sys.exit(1)

    try:
        pretty = prettyFn(original_xml)
    except Error:
        logging.error('Aborting parsing due to fatal errors.')
        sys.exit(1)

    if original_xml == pretty:
        logging.info('%s is correctly pretty-printed.', original_filename)
        sys.exit(0)
    if presubmit:
        logging.error('%s is not formatted correctly; run %s to fix.',
                      original_filename, script_name)
        sys.exit(1)

    # Prompt user to consent on the change.
    if not diff_util.PromptUserToAcceptDiff(original_xml, pretty,
                                            'Is the new version acceptable?'):
        logging.error('Diff not accepted. Aborting.')
        sys.exit(1)

    logging.info('Creating backup file: %s', backup_filename)
    shutil.move(xml_path, os.path.join(xml_dir, backup_filename))

    with open(xml_path, 'wb') as f:
        f.write(pretty)
    logging.info('Updated %s. Don\'t forget to add it to your changelist',
                 xml_path)
    sys.exit(0)