2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Pretty-prints the histograms.xml file, alphabetizing tags, wrapping text
7 at 80 chars, enforcing standard attribute ordering, and standardizing
10 This is quite a bit more complicated than just calling tree.toprettyxml();
11 we need additional customization, like special attribute ordering in tags
12 and wrapping text nodes, so we implement our own full custom XML pretty-printer.
15 from __future__
import with_statement
21 import xml
.dom
.minidom
25 sys
.path
.insert(1, os
.path
.join(sys
.path
[0], '..', '..', 'python'))
26 from google
import path_utils
28 # Import the metrics/common module.
29 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
), '..', 'common'))
32 # Tags whose children we want to alphabetize. The key is the parent tag name,
33 # and the value is a pair of the tag name of the children we want to sort,
34 # and a key function that maps each child node to the desired sort key.
35 ALPHABETIZATION_RULES
= {
36 'histograms': ('histogram', lambda n
: n
.attributes
['name'].value
.lower()),
37 'enums': ('enum', lambda n
: n
.attributes
['name'].value
.lower()),
38 'enum': ('int', lambda n
: int(n
.attributes
['value'].value
)),
39 'histogram_suffixes_list': (
40 'histogram_suffixes', lambda n
: n
.attributes
['name'].value
.lower()),
41 'histogram_suffixes': ('affected-histogram',
42 lambda n
: n
.attributes
['name'].value
.lower()),
43 # TODO(yiyaoliu): Remove fieldtrial related pieces when it is not used.
44 'fieldtrials': ('fieldtrial', lambda n
: n
.attributes
['name'].value
.lower()),
45 'fieldtrial': ('affected-histogram',
46 lambda n
: n
.attributes
['name'].value
.lower()),
50 class Error(Exception):
54 def unsafeAppendChild(parent
, child
):
55 """Append child to parent's list of children, ignoring the possibility that it
56 is already in another node's childNodes list. Requires that the previous
57 parent of child is discarded (to avoid non-tree DOM graphs).
58 This can provide a significant speedup as O(n^2) operations are removed (in
59 particular, each child insertion avoids the need to traverse the old parent's
60 entire list of children)."""
61 child
.parentNode
= None
62 parent
.appendChild(child
)
63 child
.parentNode
= parent
66 def TransformByAlphabetizing(node
):
67 """Transform the given XML by alphabetizing specific node types according to
68 the rules in ALPHABETIZATION_RULES.
71 node: The minidom node to transform.
74 The minidom node, with children appropriately alphabetized. Note that the
75 transformation is done in-place, i.e. the original minidom tree is modified
78 if node
.nodeType
!= xml
.dom
.minidom
.Node
.ELEMENT_NODE
:
79 for c
in node
.childNodes
: TransformByAlphabetizing(c
)
82 # Element node with a tag name that we alphabetize the children of?
83 if node
.tagName
in ALPHABETIZATION_RULES
:
84 # Put subnodes in a list of node,key pairs to allow for custom sorting.
85 subtag
, key_function
= ALPHABETIZATION_RULES
[node
.tagName
]
88 for c
in node
.childNodes
:
89 if (c
.nodeType
== xml
.dom
.minidom
.Node
.ELEMENT_NODE
and
91 last_key
= key_function(c
)
92 # Subnodes that we don't want to rearrange use the last node's key,
93 # so they stay in the same relative position.
94 subnodes
.append( (c
, last_key
) )
96 # Sort the subnode list.
97 subnodes
.sort(key
=lambda pair
: pair
[1])
99 # Re-add the subnodes, transforming each recursively.
100 while node
.firstChild
:
101 node
.removeChild(node
.firstChild
)
102 for (c
, _
) in subnodes
:
103 unsafeAppendChild(node
, TransformByAlphabetizing(c
))
106 # Recursively handle other element nodes and other node types.
107 for c
in node
.childNodes
: TransformByAlphabetizing(c
)
111 def PrettyPrint(raw_xml
):
112 """Pretty-print the given XML.
115 raw_xml: The contents of the histograms XML file, as a string.
118 The pretty-printed version.
120 tree
= xml
.dom
.minidom
.parseString(raw_xml
)
121 tree
= TransformByAlphabetizing(tree
)
122 return print_style
.GetPrintStyle().PrettyPrintNode(tree
)
126 logging
.basicConfig(level
=logging
.INFO
)
128 presubmit
= ('--presubmit' in sys
.argv
)
130 histograms_filename
= 'histograms.xml'
131 histograms_backup_filename
= 'histograms.before.pretty-print.xml'
133 # If there is a histograms.xml in the current working directory, use that.
134 # Otherwise, use the one residing in the same directory as this script.
135 histograms_dir
= os
.getcwd()
136 if not os
.path
.isfile(os
.path
.join(histograms_dir
, histograms_filename
)):
137 histograms_dir
= path_utils
.ScriptDir()
139 histograms_pathname
= os
.path
.join(histograms_dir
, histograms_filename
)
140 histograms_backup_pathname
= os
.path
.join(histograms_dir
,
141 histograms_backup_filename
)
143 logging
.info('Loading %s...' % os
.path
.relpath(histograms_pathname
))
144 with
open(histograms_pathname
, 'rb') as f
:
147 # Check there are no CR ('\r') characters in the file.
149 logging
.info('DOS-style line endings (CR characters) detected - these are '
150 'not allowed. Please run dos2unix %s' % histograms_filename
)
153 logging
.info('Pretty-printing...')
155 pretty
= PrettyPrint(xml
)
157 logging
.error('Aborting parsing due to fatal errors.')
161 logging
.info('%s is correctly pretty-printed.' % histograms_filename
)
164 logging
.info('%s is not formatted correctly; run pretty_print.py to fix.' %
167 if not diff_util
.PromptUserToAcceptDiff(
169 'Is the prettified version acceptable?'):
170 logging
.error('Aborting')
173 logging
.info('Creating backup file %s' % histograms_backup_filename
)
174 shutil
.move(histograms_pathname
, histograms_backup_pathname
)
176 logging
.info('Writing new %s file' % histograms_filename
)
177 with
open(histograms_pathname
, 'wb') as f
:
181 if __name__
== '__main__':