コード例 #1
0
def SuffixesToVariantsMigration(args):
  """Migates all histogram suffixes to patterned histograms."""
  histogram_suffixes_list = minidom.parse(open(HISTOGRAM_SUFFIXES_LIST_PATH))
  histogram_suffixes_nodes = histogram_suffixes_list.getElementsByTagName(
      'histogram_suffixes')

  doc = minidom.Document()
  for histograms_file in ChooseFiles(args):
    histograms = minidom.parse(open(histograms_file))
    single_affected, all_affected_found = _GetSuffixesDict(
        histogram_suffixes_nodes, histograms.getElementsByTagName('histogram'))
    suffixes_to_convert = set()
    for histogram in histograms.getElementsByTagName('histogram'):
      name = histogram.getAttribute('name')
      # Migrate inline patterned histograms.
      if name in single_affected.keys():
        MigrateToInlinePatterenedHistogram(doc, histogram,
                                           single_affected[name])
      elif name in all_affected_found.keys():
        suffixes_to_convert.add(all_affected_found[name])
        MigrateToOutOflinePatterenedHistogram(doc, histogram,
                                              all_affected_found[name])

    _MigrateOutOfLineVariants(doc, histograms, suffixes_to_convert)

    # Update histograms.xml with patterned histograms.
    with open(histograms_file, 'w') as f:
      pretty_xml_string = histogram_configuration_model.PrettifyTree(histograms)
      f.write(pretty_xml_string)

  # Remove histogram_suffixes that have already been migrated.
  with open(HISTOGRAM_SUFFIXES_LIST_PATH, 'w') as f:
    pretty_xml_string = histogram_configuration_model.PrettifyTree(
        histogram_suffixes_list)
    f.write(pretty_xml_string)
 def testRegex(self, _, pretty_input_xml, original_string, bad_string):
     BAD_XML = pretty_input_xml.replace(original_string, bad_string)
     with self.assertRaises(ValueError) as context:
         histogram_configuration_model.PrettifyTree(
             etree_util.ParseXMLString(BAD_XML))
     self.assertIn(bad_string, str(context.exception))
     self.assertIn('does not match regex', str(context.exception))
コード例 #3
0
def main():
  if len(sys.argv) > 1:
    print('No arguments expected!', file=sys.stderr)
    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 = histogram_configuration_model.PrettifyTree(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.')
コード例 #4
0
def PrettyPrintEnums(raw_xml):
    """Pretty print the given enums XML."""

    root = etree_util.ParseXMLString(raw_xml)

    # Prevent accidentally adding histograms to enums.xml
    DropNodesByTagName(root, 'histograms')
    DropNodesByTagName(root, 'histogram_suffixes_list')
    top_level_content = etree_util.GetTopLevelContent(raw_xml)
    formatted_xml = histogram_configuration_model.PrettifyTree(root)
    return top_level_content + formatted_xml
コード例 #5
0
def PrettyPrintHistogramsTree(tree):
    """Pretty-print the given ElementTree element.

  Args:
    tree: The ElementTree element.

  Returns:
    The pretty-printed version as an XML string.
  """
    # Prevent accidentally adding enums to histograms.xml
    DropNodesByTagName(tree, 'enums')
    FixMisplacedHistogramsAndHistogramSuffixes(tree)
    canonicalizeUnits(tree)
    fixObsoleteOrder(tree)
    return histogram_configuration_model.PrettifyTree(tree)
コード例 #6
0
def main(args):
    # Extract all histograms into a dict.
    doc = merge_xml.MergeFiles(filenames=histogram_paths.ALL_XMLS,
                               should_expand_owners=True)
    histograms, had_errors = extract_histograms.ExtractHistogramsFromDom(doc)
    if had_errors:
        raise ValueError("Error parsing inputs.")
    # Construct a dom tree that is similar to the normal histograms.xml so that
    # we can use histogram_configuration_model to pretty print it.
    doc = xml.dom.minidom.Document()
    configuration = doc.createElement('histogram-configuration')
    histograms_node = doc.createElement('histograms')
    for name, histogram in histograms.items():
        if re.match(args.pattern, name):
            histograms_node.appendChild(
                ConstructHistogram(doc, name, histogram))
    configuration.appendChild(histograms_node)
    doc.appendChild(configuration)
    print(histogram_configuration_model.PrettifyTree(doc))
コード例 #7
0
def _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values,
                         source_enum_path, caller_script_name):
  """Reads old histogram from |histogram_enum_name| from |ENUMS_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(ENUMS_PATH))
  with io.open(ENUMS_PATH, 'r', encoding='utf-8') 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, caller_script_name,
                             histograms_doc)

  new_xml = histogram_configuration_model.PrettifyTree(histograms_doc)
  return (xml, new_xml)
コード例 #8
0
ファイル: split_xml.py プロジェクト: Azile-Ttenneb/chromium-1
def _CreateXMLFile(comment, parent_node_string, nodes, output_dir, filename):
    """Creates XML file for given type of XML nodes.

  This function also creates a |parent_node_string| tag as the parent node, e.g.
  <histograms> or <histogram_suffixes_list>, that wraps all the |nodes| in the
  output XML.

  Args:
    comment: The string to be formatted in the |TOP_LEVEL_COMMENT_TEMPLATE|
        which will then be added on top of each split xml.
    parent_node_string: The name of the the second-level parent node, e.g.
        <histograms> or <histogram_suffixes_list>.
    nodes: A DOM NodeList object or a list containing <histogram> or
        <histogram_suffixes> that will be inserted under the parent node.
    output_dir: The output directory.
    filename: The output filename.
  """
    doc = minidom.Document()

    doc.appendChild(doc.createComment(FIRST_TOP_LEVEL_COMMENT_TEMPLATE))
    doc.appendChild(
        doc.createComment(SECOND_TOP_LEVEL_COMMENT_TEMPLATE % comment))

    # Create the <histogram-configuration> node for the new histograms.xml file.
    histogram_config_element = doc.createElement('histogram-configuration')
    doc.appendChild(histogram_config_element)
    parent_element = doc.createElement(parent_node_string)
    histogram_config_element.appendChild(parent_element)

    # Under the parent node, append the children nodes.
    for node in nodes:
        parent_element.appendChild(node)

    output_path = os.path.join(output_dir, filename)
    if os.path.exists(output_path):
        os.remove(output_path)

    # Use the model to get pretty-printed XML string and write into file.
    with open(output_path, 'w') as output_file:
        pretty_xml_string = histogram_configuration_model.PrettifyTree(doc)
        output_file.write(pretty_xml_string)
コード例 #9
0
def main():
    if len(sys.argv) > 1:
        print('No arguments expected!', file=sys.stderr)
        sys.stderr.write(__doc__)
        sys.exit(1)

    with open(path_util.GetInputFile(POLICY_TEMPLATES_PATH), 'rb') as f:
        policy_templates = literal_eval(f.read().decode('utf-8'))

    with open(ENUMS_PATH, 'rb') as f:
        histograms_doc = minidom.parse(f)
        f.seek(0)
        xml = f.read().decode('utf-8')

    UpdatePoliciesHistogramDefinitions(policy_templates, histograms_doc)
    UpdateAtomicGroupsHistogramDefinitions(policy_templates, histograms_doc)
    new_xml = histogram_configuration_model.PrettifyTree(histograms_doc)
    if PromptUserToAcceptDiff(xml, new_xml,
                              'Is the updated version acceptable?'):
        with open(ENUMS_PATH, 'wb') as f:
            f.write(new_xml.encode('utf-8'))
コード例 #10
0
def _CreateXMLFile(comments, parent_node_string, nodes, output_dir, filename):
    """Creates XML file for given type of XML nodes.

  This function also creates a |parent_node_string| tag as the parent node, e.g.
  <histograms> or <histogram_suffixes_list>, that wraps all the |nodes| in the
  output XML.

  Args:
    comments: Top level comment nodes from the original histograms.xml file.
    parent_node_string: The name of the the second-level parent node, e.g.
        <histograms> or <histogram_suffixes_list>.
    nodes: A DOM NodeList object or a list containing <histogram> or
        <histogram_suffixes> that will be inserted under the parent node.
    output_dir: The output directory.
    filename: The output filename.
  """
    doc = minidom.Document()

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

    # Create the <histogram-configuration> node for the new histograms.xml file.
    histogram_config_element = doc.createElement('histogram-configuration')
    doc.appendChild(histogram_config_element)
    parent_element = doc.createElement(parent_node_string)
    histogram_config_element.appendChild(parent_element)

    # Under the parent node, append the children nodes.
    for node in nodes:
        parent_element.appendChild(node)

    # Use the model to get pretty-printed XML string and write into file.
    with open(os.path.join(output_dir, filename), 'w') as output_file:
        pretty_xml_string = histogram_configuration_model.PrettifyTree(doc)
        output_file.write(pretty_xml_string)
コード例 #11
0
def PrettyPrintMergedFiles(filenames=[], files=[]):
    return histogram_configuration_model.PrettifyTree(
        MergeFiles(filenames=filenames, files=files))
 def testTokenPrettify(self, _, input_xml, expected_xml):
     self.maxDiff = None
     result = histogram_configuration_model.PrettifyTree(
         etree_util.ParseXMLString(input_xml))
     self.assertMultiLineEqual(result.strip(), expected_xml)
 def testMissingRequiredAttribute(self):
     with self.assertRaises(Exception) as context:
         histogram_configuration_model.PrettifyTree(
             etree_util.ParseXMLString(XML_MISSING_SEPARATOR))
     self.assertIn('separator', str(context.exception))
     self.assertIn('Missing attribute', str(context.exception))
コード例 #14
0
ファイル: merge_xml.py プロジェクト: Azile-Ttenneb/chromium-1
def PrettyPrintMergedFiles(filenames=[], files=[]):
    return histogram_configuration_model.PrettifyTree(
        MergeFiles(filenames=filenames, files=files,
                   should_expand_owners=True))