| 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(); |
| 11 we need additional customization, like special attribute ordering in tags | 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. | 12 and wrapping text nodes, so we implement our own full custom XML pretty-printer. |
| 13 """ | 13 """ |
| 14 | 14 |
| 15 from __future__ import with_statement | 15 from __future__ import with_statement |
| 16 | 16 |
| 17 import diffutil | 17 import diffutil |
| 18 import json | 18 import json |
| 19 import logging | 19 import logging |
| 20 import os |
| 20 import shutil | 21 import shutil |
| 21 import sys | 22 import sys |
| 22 import textwrap | 23 import textwrap |
| 23 import xml.dom.minidom | 24 import xml.dom.minidom |
| 24 | 25 |
| 26 sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'python')) |
| 27 from google import path_utils |
| 25 | 28 |
| 26 WRAP_COLUMN = 80 | 29 WRAP_COLUMN = 80 |
| 27 | 30 |
| 28 # Desired order for tag attributes; attributes listed here will appear first, | 31 # Desired order for tag attributes; attributes listed here will appear first, |
| 29 # and in the same order as in these lists. | 32 # and in the same order as in these lists. |
| 30 # { tag_name: [attribute_name, ...] } | 33 # { tag_name: [attribute_name, ...] } |
| 31 ATTRIBUTE_ORDER = { | 34 ATTRIBUTE_ORDER = { |
| 32 'enum': ['name', 'type'], | 35 'enum': ['name', 'type'], |
| 33 'histogram': ['name', 'enum', 'units'], | 36 'histogram': ['name', 'enum', 'units'], |
| 34 'int': ['value', 'label'], | 37 'int': ['value', 'label'], |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 tree = xml.dom.minidom.parseString(raw_xml) | 312 tree = xml.dom.minidom.parseString(raw_xml) |
| 310 tree = TransformByAlphabetizing(tree) | 313 tree = TransformByAlphabetizing(tree) |
| 311 return PrettyPrintNode(tree) | 314 return PrettyPrintNode(tree) |
| 312 | 315 |
| 313 | 316 |
| 314 def main(): | 317 def main(): |
| 315 logging.basicConfig(level=logging.INFO) | 318 logging.basicConfig(level=logging.INFO) |
| 316 | 319 |
| 317 presubmit = ('--presubmit' in sys.argv) | 320 presubmit = ('--presubmit' in sys.argv) |
| 318 | 321 |
| 319 logging.info('Loading histograms.xml...') | 322 histograms_filename = 'histograms.xml' |
| 320 with open('histograms.xml', 'rb') as f: | 323 histograms_backup_filename = 'histograms.before.pretty-print.xml' |
| 324 |
| 325 script_dir = path_utils.ScriptDir() |
| 326 |
| 327 histograms_pathname = os.path.join(script_dir, histograms_filename) |
| 328 histograms_backup_pathname = os.path.join(script_dir, |
| 329 histograms_backup_filename) |
| 330 |
| 331 logging.info('Loading %s...' % histograms_filename) |
| 332 with open(histograms_pathname, 'rb') as f: |
| 321 xml = f.read() | 333 xml = f.read() |
| 322 | 334 |
| 323 # Check there are no CR ('\r') characters in the file. | 335 # Check there are no CR ('\r') characters in the file. |
| 324 if '\r' in xml: | 336 if '\r' in xml: |
| 325 logging.info('DOS-style line endings (CR characters) detected - these are ' | 337 logging.info('DOS-style line endings (CR characters) detected - these are ' |
| 326 'not allowed. Please run dos2unix histograms.xml') | 338 'not allowed. Please run dos2unix %s' % histograms_filename) |
| 327 sys.exit(1) | 339 sys.exit(1) |
| 328 | 340 |
| 329 logging.info('Pretty-printing...') | 341 logging.info('Pretty-printing...') |
| 330 try: | 342 try: |
| 331 pretty = PrettyPrint(xml) | 343 pretty = PrettyPrint(xml) |
| 332 except Error: | 344 except Error: |
| 333 logging.error('Aborting parsing due to fatal errors.') | 345 logging.error('Aborting parsing due to fatal errors.') |
| 334 sys.exit(1) | 346 sys.exit(1) |
| 335 | 347 |
| 336 if xml == pretty: | 348 if xml == pretty: |
| 337 logging.info('histograms.xml is correctly pretty-printed.') | 349 logging.info('%s is correctly pretty-printed.' % histograms_filename) |
| 338 sys.exit(0) | 350 sys.exit(0) |
| 339 if presubmit: | 351 if presubmit: |
| 340 logging.info('histograms.xml is not formatted correctly; run ' | 352 logging.info('%s is not formatted correctly; run pretty_print.py to fix.' % |
| 341 'pretty_print.py to fix.') | 353 histograms_filename) |
| 342 sys.exit(1) | 354 sys.exit(1) |
| 343 if not diffutil.PromptUserToAcceptDiff( | 355 if not diffutil.PromptUserToAcceptDiff( |
| 344 xml, pretty, | 356 xml, pretty, |
| 345 'Is the prettified version acceptable?'): | 357 'Is the prettified version acceptable?'): |
| 346 logging.error('Aborting') | 358 logging.error('Aborting') |
| 347 return | 359 return |
| 348 | 360 |
| 349 logging.info('Creating backup file histograms.before.pretty-print.xml') | 361 logging.info('Creating backup file %s' % histograms_backup_filename) |
| 350 shutil.move('histograms.xml', 'histograms.before.pretty-print.xml') | 362 shutil.move(histograms_pathname, histograms_backup_pathname) |
| 351 | 363 |
| 352 logging.info('Writing new histograms.xml file') | 364 logging.info('Writing new %s file' % histograms_filename) |
| 353 with open('histograms.xml', 'wb') as f: | 365 with open(histograms_pathname, 'wb') as f: |
| 354 f.write(pretty) | 366 f.write(pretty) |
| 355 | 367 |
| 356 | 368 |
| 357 if __name__ == '__main__': | 369 if __name__ == '__main__': |
| 358 main() | 370 main() |
| OLD | NEW |