Commit c8ba29ce authored by Weilun Shi's avatar Weilun Shi Committed by Commit Bot

Reorder misplaced <histogram> and <histogram_suffixes>

Ensure that <histogram> should be added in <hisotgrams> and
<histogram_suffixes> should be added in <histogram_suffixes_list> so
that they will be sorted separately.

Bug: 1034123
Change-Id: Iaf009dacf20a549f6e5865a4a699bb3919e96082
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1978539
Commit-Queue: Weilun Shi <sweilun@chromium.org>
Reviewed-by: default avatarIlya Sherman <isherman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#728635}
parent ab9b4001
......@@ -75,19 +75,57 @@ def fixObsoleteOrder(tree):
if obsoletes:
tree.insert(0, obsoletes[0])
def DropNodesByTagName(tree, tag):
def DropNodesByTagName(tree, tag, dropped_nodes=[]):
"""Drop all nodes with named tag from the XML tree."""
removes = []
for child in tree:
if child.tag == tag:
removes.append(child)
dropped_nodes.append(child)
else:
DropNodesByTagName(child, tag)
for child in removes:
tree.remove(child)
def FixMisplacedHistogramsAndHistogramSuffixes(tree):
"""Fixes misplaced histogram and histogram_suffixes nodes."""
histograms = []
histogram_suffixes = []
def ExtractMisplacedHistograms(tree):
"""Gets and drops misplaced histograms and histogram_suffixes.
Args:
tree: The node of the xml tree.
histograms: A list of histogram nodes inside histogram_suffixes_list
node. This is a return element.
histogram_suffixes: A list of histogram_suffixes nodes inside hisotgrams
node. This is a return element.
"""
for child in tree:
if child.tag == 'histograms':
DropNodesByTagName(child, 'histogram_suffixes', histogram_suffixes)
elif child.tag == 'histogram_suffixes_list':
DropNodesByTagName(child, 'histogram', histograms)
else:
ExtractMisplacedHistograms(child)
ExtractMisplacedHistograms(tree)
def AddBackMisplacedHisotgrams(tree):
"""Adds back those misplaced histogram and hsitogram_suffixes nodes."""
for child in tree:
if child.tag == 'histograms':
child.extend(histograms)
elif child.tag == 'histogram_suffixes_list':
child.extend(histogram_suffixes)
else:
FixMisplacedHisotgrams(child)
AddBackMisplacedHisotgrams(tree)
def PrettyPrintHistograms(raw_xml):
"""Pretty-print the given histograms XML.
......@@ -112,6 +150,7 @@ def PrettyPrintHistogramsTree(tree):
"""
# Prevent accidentally adding enums to histograms.xml
DropNodesByTagName(tree, 'enums')
FixMisplacedHistogramsAndHistogramSuffixes(tree)
canonicalizeUnits(tree)
fixObsoleteOrder(tree)
return histograms_print_style.GetPrintStyle().PrettyPrintXml(tree)
......
......@@ -25,6 +25,7 @@ ORIGINAL_XML = """
Removed 1/2019.
</obsolete>
</histogram>
<histogram name="Foo.Bar" units="xxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyzzzz">
<summary>Foo</summary>
<obsolete>Obsolete 1</obsolete>
......@@ -33,12 +34,29 @@ ORIGINAL_XML = """
<component>Component</component>
<component>Other&gt;Component</component>
</histogram>
<histogram_suffixes name="Test.HistogramSuffixes" separator=".">
<suffix name="TestSuffix" label="A misplaced histogram_suffixes"/>
<affected-histogram name="Test.Histogram"/>
</histogram_suffixes>
</histograms>
<histogram_suffixes_list>
<histogram name="Test.MisplacedHistogram" units="us">
<owner>person@chromium.org</owner>
<summary>A misplaced histogram
</summary>
Misplaced content.
</histogram>
</histogram_suffixes_list>
<enums>This shouldn't be here</enums>
</histogram-configuration>
""".strip()
PRETTY_XML = """
<!-- Top level Comment 1 -->
<!-- Top level Comment 2 -->
......@@ -70,8 +88,23 @@ PRETTY_XML = """
Mixed content.
</histogram>
<histogram name="Test.MisplacedHistogram" units="microseconds">
<owner>person@chromium.org</owner>
<summary>A misplaced histogram</summary>
Misplaced content.
</histogram>
</histograms>
<histogram_suffixes_list>
<histogram_suffixes name="Test.HistogramSuffixes" separator=".">
<suffix name="TestSuffix" label="A misplaced histogram_suffixes"/>
<affected-histogram name="Test.Histogram"/>
</histogram_suffixes>
</histogram_suffixes_list>
</histogram-configuration>
""".strip()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment