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): ...@@ -75,19 +75,57 @@ def fixObsoleteOrder(tree):
if obsoletes: if obsoletes:
tree.insert(0, obsoletes[0]) 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.""" """Drop all nodes with named tag from the XML tree."""
removes = [] removes = []
for child in tree: for child in tree:
if child.tag == tag: if child.tag == tag:
removes.append(child) removes.append(child)
dropped_nodes.append(child)
else: else:
DropNodesByTagName(child, tag) DropNodesByTagName(child, tag)
for child in removes: for child in removes:
tree.remove(child) 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): def PrettyPrintHistograms(raw_xml):
"""Pretty-print the given histograms XML. """Pretty-print the given histograms XML.
...@@ -112,6 +150,7 @@ def PrettyPrintHistogramsTree(tree): ...@@ -112,6 +150,7 @@ def PrettyPrintHistogramsTree(tree):
""" """
# Prevent accidentally adding enums to histograms.xml # Prevent accidentally adding enums to histograms.xml
DropNodesByTagName(tree, 'enums') DropNodesByTagName(tree, 'enums')
FixMisplacedHistogramsAndHistogramSuffixes(tree)
canonicalizeUnits(tree) canonicalizeUnits(tree)
fixObsoleteOrder(tree) fixObsoleteOrder(tree)
return histograms_print_style.GetPrintStyle().PrettyPrintXml(tree) return histograms_print_style.GetPrintStyle().PrettyPrintXml(tree)
......
...@@ -25,6 +25,7 @@ ORIGINAL_XML = """ ...@@ -25,6 +25,7 @@ ORIGINAL_XML = """
Removed 1/2019. Removed 1/2019.
</obsolete> </obsolete>
</histogram> </histogram>
<histogram name="Foo.Bar" units="xxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyzzzz"> <histogram name="Foo.Bar" units="xxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyzzzz">
<summary>Foo</summary> <summary>Foo</summary>
<obsolete>Obsolete 1</obsolete> <obsolete>Obsolete 1</obsolete>
...@@ -33,12 +34,29 @@ ORIGINAL_XML = """ ...@@ -33,12 +34,29 @@ ORIGINAL_XML = """
<component>Component</component> <component>Component</component>
<component>Other&gt;Component</component> <component>Other&gt;Component</component>
</histogram> </histogram>
<histogram_suffixes name="Test.HistogramSuffixes" separator=".">
<suffix name="TestSuffix" label="A misplaced histogram_suffixes"/>
<affected-histogram name="Test.Histogram"/>
</histogram_suffixes>
</histograms> </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> <enums>This shouldn't be here</enums>
</histogram-configuration> </histogram-configuration>
""".strip() """.strip()
PRETTY_XML = """ PRETTY_XML = """
<!-- Top level Comment 1 --> <!-- Top level Comment 1 -->
<!-- Top level Comment 2 --> <!-- Top level Comment 2 -->
...@@ -70,8 +88,23 @@ PRETTY_XML = """ ...@@ -70,8 +88,23 @@ PRETTY_XML = """
Mixed content. Mixed content.
</histogram> </histogram>
<histogram name="Test.MisplacedHistogram" units="microseconds">
<owner>person@chromium.org</owner>
<summary>A misplaced histogram</summary>
Misplaced content.
</histogram>
</histograms> </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> </histogram-configuration>
""".strip() """.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