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|.
  """
    # TODO(ahernandez.miralles): The line below is present in nearly every
    # file in this directory; factor out into a central location
    HISTOGRAMS_PATH = 'histograms.xml'

    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 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.')
Exemplo n.º 2
0
def main():
  if len(sys.argv) > 1:
    print >>sys.stderr, 'No arguments expected!'
    sys.stderr.write(__doc__)
    sys.exit(1)

  Log('Reading histogram enum definition from "%s".' % EDITOR_COMMAND_CPP)
  histogram_values = ReadHistogramValues(EDITOR_COMMAND_CPP)

  Log('Reading existing histograms from "%s".' % (ENUMS_PATH))
  with open(ENUMS_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_values, histograms_doc)

  Log('Writing out new histograms file.')
  new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc)
  if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'):
    with open(ENUMS_PATH, 'wb') as f:
      f.write(new_xml)

  Log('Done.')
Exemplo n.º 3
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.')
Exemplo n.º 4
0
def PrettyPrint(actions, actions_dict, comment_nodes=[]):
    """Given a list of action data, create a well-printed minidom document.

  Args:
    actions: A list of action names.
    actions_dict: A mappting from action name to Action object.

  Returns:
    A well-printed minidom document that represents the input action data.
  """
    doc = minidom.Document()

    # Attach top-level comments.
    for node in comment_nodes:
        doc.appendChild(node)

    actions_element = doc.createElement('actions')
    doc.appendChild(actions_element)

    # Attach action node based on updated |actions|.
    for action in sorted(actions):
        actions_element.appendChild(
            _CreateActionTag(doc, action, actions_dict.get(action, None)))

    return print_style.GetPrintStyle().PrettyPrintNode(doc)
Exemplo n.º 5
0
def PrettyPrintEnums(raw_xml):
    """Pretty print the enums.xml file."""
    tree = xml.dom.minidom.parseString(raw_xml)
    # Prevent accidentally adding histograms to enums.xml
    DropNodesByTagName(tree, 'histograms')
    DropNodesByTagName(tree, 'histogram_suffixes_list')
    return print_style.GetPrintStyle().PrettyPrintXml(tree)
Exemplo n.º 6
0
def PrettyPrint(actions_dict, comment_nodes, suffixes):
    """Given a list of actions, create a well-printed minidom document.

  Args:
    actions_dict: A mappting from action name to Action object.
    comment_nodes: A list of top-level comment nodes.
    suffixes: A list of <action-suffix> tags to be appended as-is.

  Returns:
    A well-printed minidom document that represents the input action data.
  """
    doc = minidom.Document()

    # Attach top-level comments.
    for node in comment_nodes:
        doc.appendChild(node)

    actions_element = doc.createElement('actions')
    doc.appendChild(actions_element)

    # Attach action node based on updated |actions_dict|.
    for _, action in sorted(actions_dict.items()):
        action_tag = _CreateActionTag(doc, action)
        if action_tag:
            actions_element.appendChild(action_tag)

    for suffix_tag in suffixes:
        actions_element.appendChild(suffix_tag)

    return print_style.GetPrintStyle().PrettyPrintXml(doc)
Exemplo n.º 7
0
def PrettyPrint(raw_xml):
    """Pretty-print the given XML.

  Args:
    raw_xml: The contents of the histograms XML file, as a string.

  Returns:
    The pretty-printed version.
  """
    tree = xml.dom.minidom.parseString(raw_xml)
    tree = TransformByAlphabetizing(tree)
    return print_style.GetPrintStyle().PrettyPrintNode(tree)
Exemplo n.º 8
0
def PrettyPrint(raw_xml):
    """Pretty-print the given XML.

  Args:
    raw_xml: The contents of the histograms XML file, as a string.

  Returns:
    The pretty-printed version.
  """
    tree = xml.dom.minidom.parseString(raw_xml)
    canonicalizeUnits(tree)
    return print_style.GetPrintStyle().PrettyPrintXml(tree)
Exemplo n.º 9
0
def PrettyPrintHistograms(raw_xml):
    """Pretty-print the given XML.

  Args:
    raw_xml: The contents of the histograms XML file, as a string.

  Returns:
    The pretty-printed version.
  """
    tree = xml.dom.minidom.parseString(raw_xml)
    # Prevent accidentally adding enums to histograms.xml
    DropNodesByTagName(tree, 'enums')
    canonicalizeUnits(tree)
    fixObsoleteOrder(tree)
    return print_style.GetPrintStyle().PrettyPrintXml(tree)
Exemplo n.º 10
0
def PrettyPrintHistogramsTree(tree):
    """Pretty-print the given xml.dom.minidom.Document object.

  Args:
    tree: The xml.dom.minidom.Document object.

  Returns:
    The pretty-printed version as an XML string.
  """
    assert isinstance(tree, xml.dom.minidom.Document)
    # Prevent accidentally adding enums to histograms.xml
    DropNodesByTagName(tree, 'enums')
    canonicalizeUnits(tree)
    fixObsoleteOrder(tree)
    return print_style.GetPrintStyle().PrettyPrintXml(tree)
Exemplo n.º 11
0
def _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values,
                         source_enum_path):
    """Reads old histogram from |histogram_enum_name| from |HISTOGRAMS_PATH|, and
  calculates new histogram from |source_enum_values| from |source_enum_path|,
  and returns both in XML format.
  """
    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)

    new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc)
    return (xml, new_xml)
Exemplo n.º 12
0
def main():
    if len(sys.argv) > 1:
        print >> sys.stderr, 'No arguments expected!'
        sys.stderr.write(__doc__)
        sys.exit(1)

    with open(path_util.GetInputFile(POLICY_TEMPLATES_PATH), 'rb') as f:
        policy_templates = literal_eval(f.read())
    with open(HISTOGRAMS_PATH, 'rb') as f:
        histograms_doc = minidom.parse(f)
        f.seek(0)
        xml = f.read()

    UpdateHistogramDefinitions(policy_templates, histograms_doc)
    new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc)
    if PromptUserToAcceptDiff(xml, new_xml,
                              'Is the updated version acceptable?'):
        with open(HISTOGRAMS_PATH, 'wb') as f:
            f.write(new_xml)
Exemplo n.º 13
0
def UpdateHistogramEnum(histogram_enum_name, source_enum_path, start_marker,
                        end_marker):
    """Updates |histogram_enum_name| enum in histograms.xml file with values
  read from |source_enum_path|, where |start_marker| and |end_marker| indicate
  the beginning and end of the source enum definition, respectively.
  """
    # TODO(ahernandez.miralles): The line below is present in nearly every
    # file in this directory; factor out into a central location
    HISTOGRAMS_PATH = 'histograms.xml'

    if len(sys.argv) > 1:
        print >> sys.stderr, 'No arguments expected!'
        sys.stderr.write(__doc__)
        sys.exit(1)

    Log('Reading histogram enum definition from "{0}".'.format(
        source_enum_path))
    source_enum_values = ReadHistogramValues(source_enum_path, start_marker,
                                             end_marker)

    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 PromptUserToAcceptDiff(xml, new_xml,
                              'Is the updated version acceptable?'):
        with open(HISTOGRAMS_PATH, 'wb') as f:
            f.write(new_xml)

    Log('Done.')