Exemple #1
0
def UpdateHistogramFromDict(histogram_enum_name, source_enum_values,
                            source_enum_path):
    """Updates |histogram_enum_name| enum in histograms.xml file with values
  from the {value: 'key'} dictionary |source_enum_values|. A comment is added
  to histograms.xml citing that the values in |histogram_enum_name| were
  sourced from |source_enum_path|.
  """
    HISTOGRAMS_PATH = path_util.GetHistogramsFile()

    Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH))
    with open(HISTOGRAMS_PATH, 'rb') as f:
        histograms_doc = minidom.parse(f)
        f.seek(0)
        xml = f.read()

    Log('Comparing histograms enum with new enum definition.')
    UpdateHistogramDefinitions(histogram_enum_name, source_enum_values,
                               source_enum_path, histograms_doc)

    Log('Writing out new histograms file.')
    new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc)
    if not diff_util.PromptUserToAcceptDiff(
            xml, new_xml, 'Is the updated version acceptable?'):
        Log('Cancelled.')
        return

    with open(HISTOGRAMS_PATH, 'wb') as f:
        f.write(new_xml)

    Log('Done.')
Exemple #2
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)
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
def UpdateHistogramFromDict(histogram_enum_name, source_enum_values,
                            source_enum_path):
    """Updates |histogram_enum_name| enum in histograms.xml file with values
  from the {value: 'key'} dictionary |source_enum_values|. A comment is added
  to histograms.xml citing that the values in |histogram_enum_name| were
  sourced from |source_enum_path|.
  """
    (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name,
                                          source_enum_values, source_enum_path)
    if not diff_util.PromptUserToAcceptDiff(
            xml, new_xml, 'Is the updated version acceptable?'):
        Log('Cancelled.')
        return

    with open(HISTOGRAMS_PATH, 'wb') as f:
        f.write(new_xml)

    Log('Done.')
def DoPresubmit(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.
  """
  # interactive: Print log info messages and prompt user to accept the diff.
  interactive = ('--non-interactive' not in argv)
  # presubmit: Simply print a message if the input is not formatted correctly.
  presubmit = ('--presubmit' in argv)
  # diff: Print diff to stdout rather than modifying files.
  diff = ('--diff' in argv)

  if interactive:
    logging.basicConfig(level=logging.INFO)
  else:
    logging.basicConfig(level=logging.ERROR)

  # 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)
    return 1

  try:
    pretty = prettyFn(original_xml)
  except Exception as e:
    logging.exception('Aborting parsing due to fatal errors:')
    return 1

  if original_xml == pretty:
    logging.info('%s is correctly pretty-printed.', original_filename)
    return 0

  if presubmit:
    logging.error('%s is not formatted correctly; run %s to fix.',
                  original_filename, script_name)
    return 1

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

  if diff:
    for line in difflib.unified_diff(original_xml.splitlines(),
                                     pretty.splitlines()):
      print line
    return 0

  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)
  return 0