Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: tools/metrics/histograms/pretty_print.py

Issue 121403002: Have pretty_print.py find histograms.xml. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remediate Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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'))
Ilya Sherman 2013/12/27 21:48:05 Hmm, I hadn't noticed that this was needed to supp
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
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
322 script_dir = path_utils.ScriptDir()
323
319 logging.info('Loading histograms.xml...') 324 logging.info('Loading histograms.xml...')
320 with open('histograms.xml', 'rb') as f: 325 with open(os.path.join(script_dir, 'histograms.xml'), 'rb') as f:
321 xml = f.read() 326 xml = f.read()
322 327
323 # Check there are no CR ('\r') characters in the file. 328 # Check there are no CR ('\r') characters in the file.
324 if '\r' in xml: 329 if '\r' in xml:
325 logging.info('DOS-style line endings (CR characters) detected - these are ' 330 logging.info('DOS-style line endings (CR characters) detected - these are '
326 'not allowed. Please run dos2unix histograms.xml') 331 'not allowed. Please run dos2unix histograms.xml')
327 sys.exit(1) 332 sys.exit(1)
328 333
329 logging.info('Pretty-printing...') 334 logging.info('Pretty-printing...')
330 try: 335 try:
331 pretty = PrettyPrint(xml) 336 pretty = PrettyPrint(xml)
332 except Error: 337 except Error:
333 logging.error('Aborting parsing due to fatal errors.') 338 logging.error('Aborting parsing due to fatal errors.')
334 sys.exit(1) 339 sys.exit(1)
335 340
336 if xml == pretty: 341 if xml == pretty:
337 logging.info('histograms.xml is correctly pretty-printed.') 342 logging.info('histograms.xml is correctly pretty-printed.')
338 sys.exit(0) 343 sys.exit(0)
339 if presubmit: 344 if presubmit:
340 logging.info('histograms.xml is not formatted correctly; run ' 345 logging.info('histograms.xml is not formatted correctly; run '
341 'pretty_print.py to fix.') 346 'pretty_print.py to fix.')
342 sys.exit(1) 347 sys.exit(1)
343 if not diffutil.PromptUserToAcceptDiff( 348 if not diffutil.PromptUserToAcceptDiff(
344 xml, pretty, 349 xml, pretty,
345 'Is the prettified version acceptable?'): 350 'Is the prettified version acceptable?'):
346 logging.error('Aborting') 351 logging.error('Aborting')
347 return 352 return
348 353
349 logging.info('Creating backup file histograms.before.pretty-print.xml') 354 logging.info('Creating backup file histograms.before.pretty-print.xml')
350 shutil.move('histograms.xml', 'histograms.before.pretty-print.xml') 355 shutil.move(os.path.join(script_dir, 'histograms.xml'),
356 os.path.join(script_dir, 'histograms.before.pretty-print.xml'))
Ilya Sherman 2013/12/27 21:48:05 nit: Please declare named constants for the file n
gavinp 2014/01/06 17:36:07 Done. I did this with automatic variables though i
Ilya Sherman 2014/01/06 23:22:36 This is fine, thanks.
351 357
352 logging.info('Writing new histograms.xml file') 358 logging.info('Writing new histograms.xml file')
353 with open('histograms.xml', 'wb') as f: 359 with open(os.path.join(script_dir, 'histograms.xml'), 'wb') as f:
354 f.write(pretty) 360 f.write(pretty)
355 361
356 362
357 if __name__ == '__main__': 363 if __name__ == '__main__':
358 main() 364 main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698