summaryrefslogtreecommitdiff
path: root/chromium/tools/metrics/histograms/merge_xml.py
blob: 6311e9e27061fc770cca07f6a3e97709ef231fbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""A script to merge multiple source xml files into a single histograms.xml."""

import argparse
import xml.dom.minidom

def GetElementsByTagName(trees, tag):
  """Get all elements with the specified tag from a set of DOM trees.

  Args:
    trees: A list of DOM trees.
    tag: The tag of the elements to find.
  Returns:
    A list of DOM nodes with the specified tag.
  """
  return [e for t in trees for e in t.getElementsByTagName(tag)]


def MakeNodeWithChildren(doc, tag, children):
  """Create a dom node with specified tag and child nodes.

  Args:
    doc: The document to create the node in.
    tag: The tag to create the node with.
    children: A list of DOM nodes to add as children.
  Returns:
    A DOM node.
  """
  node = doc.createElement(tag)
  for child in children:
    node.appendChild(child)
  return node


def MergeTrees(trees):
  """Merge a list of histograms.xml DOM trees.

  Args:
    trees: A list of histograms.xml DOM trees.
  Returns:
    A merged DOM tree.
  """
  doc = xml.dom.minidom.Document()
  doc.appendChild(MakeNodeWithChildren(doc, 'histogram-configuration',
    # This can result in the merged document having multiple <enums> and
    # similar sections, but scripts ignore these anyway.
    GetElementsByTagName(trees, 'enums') +
    GetElementsByTagName(trees, 'histograms') +
    GetElementsByTagName(trees, 'histogram_suffixes_list')))
  return doc


def MergeFiles(filenames):
  """Merge a list of histograms.xml files.

  Args:
    filenames: A list of histograms.xml filenames.
  Returns:
    A merged DOM tree.
  """
  trees = [xml.dom.minidom.parse(open(f)) for f in filenames]
  return MergeTrees(trees)


def main():
  parser = argparse.ArgumentParser()
  parser.add_argument('inputs', nargs="+")
  parser.add_argument('--output', required=True)
  args = parser.parse_args()
  MergeFiles(args.inputs).writexml(open(args.output, 'w'))


if __name__ == '__main__':
  main()