def ValidateObsoleteXml():
    """Checks that all the histograms in the obsolete file are obsolete.

  Returns:
    True if at least a histogram is not obsolete, False otherwise.
  """
    has_obsolete_error = False
    tree = xml.dom.minidom.parse(histogram_paths.OBSOLETE_XML)

    for node in extract_histograms.IterElementsWithTag(tree, 'histogram', 3):
        obsolete_tag_nodelist = node.getElementsByTagName('obsolete')
        if len(obsolete_tag_nodelist) > 0:
            continue

        has_obsolete_error = True
        # If histogram is not obsolete, find out the directory that it should be
        # placed in.
        correct_dir = split_xml.GetDirForNode(node)
        histogram_name = node.getAttribute('name')

        logging.error(
            'Histogram of name %s is not obsolete, please move it to the '
            'histograms_xml/%s directory.', histogram_name, correct_dir)

    return has_obsolete_error
示例#2
0
def CheckNamespaces():
    namespaces = {}
    has_errors = False
    # Iterate over HISTOGRAMS_XMLS rather than ALL_XMLS because it's fine for
    # histogram namespaces in obsolete_histograms.xml to also appear in
    # non-obsolete histograms.xml files.
    for path in histogram_paths.HISTOGRAMS_XMLS:
        tree = xml.dom.minidom.parse(path)

        def _GetNamespace(node):
            return node.getAttribute('name').lower().split('.')[0]

        namespaces_in_file = set(
            _GetNamespace(node) for node in
            extract_histograms.IterElementsWithTag(tree, 'histogram', depth=3))
        for namespace in namespaces_in_file:
            if (namespace in namespaces
                    and namespace not in _NAMESPACES_IN_MULTIPLE_FILES):
                logging.error(
                    'Namespace %s has already been used in %s. it\'s recommended to '
                    'put histograms with the same namespace in the same file. If you '
                    'intentionally want to split a namespace across multiple files, '
                    'please add the namespace to the |_NAMESPACES_IN_MULTIPLE_FILES| '
                    'in the validate_format.py.' %
                    (namespace, namespaces[namespace]))
                has_errors = True
            namespaces[namespace] = path

    return has_errors
示例#3
0
def PopulateEnumsWithUkmEvents(doc, enums, ukm_events):
    """Populates enum nodes in the enums with a list of ukm events

  Args:
    doc: The document to create the node in.
    enums: The enums node to be iterated.
    ukm_events: A list of ukm event nodes.
  """
    for enum in extract_histograms.IterElementsWithTag(enums, 'enum', 1):
        # We only special case 'UkmEventNameHash' currently.
        if enum.getAttribute('name') == 'UkmEventNameHash':
            PopulateEnumWithUkmEvents(doc, enum, ukm_events)
示例#4
0
def ValidatePrefixInFile(xml_path):
  """Validates that all <histogram> and <variants> are put in the correct file.

  Args:
    xml_path: The path to the histograms.xml file.

  Returns:
    A boolean that is True if at least a histogram has incorrect prefix, False
        otherwise.
  """
  prefix = os.path.basename(os.path.dirname(xml_path))
  has_prefix_error = False
  tree = xml.dom.minidom.parse(xml_path)

  for node in extract_histograms.IterElementsWithTag(tree, 'variants', 3):
    correct_dir = split_xml.GetDirForNode(node)
    if correct_dir != prefix:
      variants_name = node.getAttribute('name')
      logging.error(
          'Variants of name %s is not placed in the correct directory, '
          'please remove it from the metadata/%s directory '
          'and place it in the metadata/%s directory.', variants_name, prefix,
          correct_dir)
      has_prefix_error = True

  for node in extract_histograms.IterElementsWithTag(tree, 'histogram', 3):
    correct_dir = split_xml.GetDirForNode(node)
    if correct_dir != prefix:
      histogram_name = node.getAttribute('name')
      logging.error(
          'Histogram of name %s is not placed in the correct directory, '
          'please remove it from the metadata/%s directory '
          'and place it in the metadata/%s directory.', histogram_name, prefix,
          correct_dir)
      has_prefix_error = True

  return has_prefix_error