OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Pretty-prints the histograms.xml file, alphabetizing tags, wrapping text | 6 """Pretty-prints the histograms.xml file, alphabetizing tags, wrapping text |
7 at 80 chars, enforcing standard attribute ordering, and standardizing | 7 at 80 chars, enforcing standard attribute ordering, and standardizing |
8 indentation. | 8 indentation. |
9 | 9 |
10 This is quite a bit more complicated than just calling tree.toprettyxml(); | 10 This is quite a bit more complicated than just calling tree.toprettyxml(); |
(...skipping 10 matching lines...) Expand all Loading... |
21 import xml.dom.minidom | 21 import xml.dom.minidom |
22 | 22 |
23 import print_style | 23 import print_style |
24 | 24 |
25 sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'python')) | 25 sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'python')) |
26 from google import path_utils | 26 from google import path_utils |
27 | 27 |
28 # Import the metrics/common module. | 28 # Import the metrics/common module. |
29 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) | 29 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) |
30 import diff_util | 30 import diff_util |
31 import presubmit_util | |
32 | 31 |
33 # Tags whose children we want to alphabetize. The key is the parent tag name, | 32 # Tags whose children we want to alphabetize. The key is the parent tag name, |
34 # and the value is a pair of the tag name of the children we want to sort, | 33 # and the value is a pair of the tag name of the children we want to sort, |
35 # and a key function that maps each child node to the desired sort key. | 34 # and a key function that maps each child node to the desired sort key. |
36 ALPHABETIZATION_RULES = { | 35 ALPHABETIZATION_RULES = { |
37 'histograms': ('histogram', lambda n: n.attributes['name'].value.lower()), | 36 'histograms': ('histogram', lambda n: n.attributes['name'].value.lower()), |
38 'enums': ('enum', lambda n: n.attributes['name'].value.lower()), | 37 'enums': ('enum', lambda n: n.attributes['name'].value.lower()), |
39 'enum': ('int', lambda n: int(n.attributes['value'].value)), | 38 'enum': ('int', lambda n: int(n.attributes['value'].value)), |
40 'histogram_suffixes_list': ( | 39 'histogram_suffixes_list': ( |
41 'histogram_suffixes', lambda n: n.attributes['name'].value.lower()), | 40 'histogram_suffixes', lambda n: n.attributes['name'].value.lower()), |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 Args: | 123 Args: |
125 raw_xml: The contents of the histograms XML file, as a string. | 124 raw_xml: The contents of the histograms XML file, as a string. |
126 | 125 |
127 Returns: | 126 Returns: |
128 The pretty-printed version. | 127 The pretty-printed version. |
129 """ | 128 """ |
130 tree = xml.dom.minidom.parseString(raw_xml) | 129 tree = xml.dom.minidom.parseString(raw_xml) |
131 tree = TransformByAlphabetizing(tree) | 130 tree = TransformByAlphabetizing(tree) |
132 return print_style.GetPrintStyle().PrettyPrintNode(tree) | 131 return print_style.GetPrintStyle().PrettyPrintNode(tree) |
133 | 132 |
| 133 |
134 def main(): | 134 def main(): |
135 presubmit_util.DoPresubmitMain(sys.argv, 'histograms.xml', | 135 logging.basicConfig(level=logging.INFO) |
136 'histograms.before.pretty-print.xml', | 136 |
137 'pretty_print.py', PrettyPrint) | 137 presubmit = ('--presubmit' in sys.argv) |
| 138 |
| 139 histograms_filename = 'histograms.xml' |
| 140 histograms_backup_filename = 'histograms.before.pretty-print.xml' |
| 141 |
| 142 # If there is a histograms.xml in the current working directory, use that. |
| 143 # Otherwise, use the one residing in the same directory as this script. |
| 144 histograms_dir = os.getcwd() |
| 145 if not os.path.isfile(os.path.join(histograms_dir, histograms_filename)): |
| 146 histograms_dir = path_utils.ScriptDir() |
| 147 |
| 148 histograms_pathname = os.path.join(histograms_dir, histograms_filename) |
| 149 histograms_backup_pathname = os.path.join(histograms_dir, |
| 150 histograms_backup_filename) |
| 151 |
| 152 logging.info('Loading %s...' % os.path.relpath(histograms_pathname)) |
| 153 with open(histograms_pathname, 'rb') as f: |
| 154 xml = f.read() |
| 155 |
| 156 # Check there are no CR ('\r') characters in the file. |
| 157 if '\r' in xml: |
| 158 logging.info('DOS-style line endings (CR characters) detected - these are ' |
| 159 'not allowed. Please run dos2unix %s' % histograms_filename) |
| 160 sys.exit(1) |
| 161 |
| 162 logging.info('Pretty-printing...') |
| 163 try: |
| 164 pretty = PrettyPrint(xml) |
| 165 except Error: |
| 166 logging.error('Aborting parsing due to fatal errors.') |
| 167 sys.exit(1) |
| 168 |
| 169 if xml == pretty: |
| 170 logging.info('%s is correctly pretty-printed.' % histograms_filename) |
| 171 sys.exit(0) |
| 172 if presubmit: |
| 173 logging.info('%s is not formatted correctly; run pretty_print.py to fix.' % |
| 174 histograms_filename) |
| 175 sys.exit(1) |
| 176 if not diff_util.PromptUserToAcceptDiff( |
| 177 xml, pretty, |
| 178 'Is the prettified version acceptable?'): |
| 179 logging.error('Aborting') |
| 180 return |
| 181 |
| 182 logging.info('Creating backup file %s' % histograms_backup_filename) |
| 183 shutil.move(histograms_pathname, histograms_backup_pathname) |
| 184 |
| 185 logging.info('Writing new %s file' % histograms_filename) |
| 186 with open(histograms_pathname, 'wb') as f: |
| 187 f.write(pretty) |
| 188 |
138 | 189 |
139 if __name__ == '__main__': | 190 if __name__ == '__main__': |
140 main() | 191 main() |
OLD | NEW |