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 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 | 315 |
316 | 316 |
317 def main(): | 317 def main(): |
318 logging.basicConfig(level=logging.INFO) | 318 logging.basicConfig(level=logging.INFO) |
319 | 319 |
320 presubmit = ('--presubmit' in sys.argv) | 320 presubmit = ('--presubmit' in sys.argv) |
321 | 321 |
322 histograms_filename = 'histograms.xml' | 322 histograms_filename = 'histograms.xml' |
323 histograms_backup_filename = 'histograms.before.pretty-print.xml' | 323 histograms_backup_filename = 'histograms.before.pretty-print.xml' |
324 | 324 |
325 script_dir = path_utils.ScriptDir() | 325 # If there is a histograms.xml in the current working directory, use that. |
| 326 # Otherwise, use the one residing in the same directory as this script. |
| 327 histograms_dir = os.getcwd() |
| 328 if not os.path.isfile(os.path.join(histograms_dir, histograms_filename)): |
| 329 histograms_dir = path_utils.ScriptDir() |
326 | 330 |
327 histograms_pathname = os.path.join(script_dir, histograms_filename) | 331 histograms_pathname = os.path.join(histograms_dir, histograms_filename) |
328 histograms_backup_pathname = os.path.join(script_dir, | 332 histograms_backup_pathname = os.path.join(histograms_dir, |
329 histograms_backup_filename) | 333 histograms_backup_filename) |
330 | 334 |
331 logging.info('Loading %s...' % histograms_filename) | 335 logging.info('Loading %s...' % os.path.relpath(histograms_pathname)) |
332 with open(histograms_pathname, 'rb') as f: | 336 with open(histograms_pathname, 'rb') as f: |
333 xml = f.read() | 337 xml = f.read() |
334 | 338 |
335 # Check there are no CR ('\r') characters in the file. | 339 # Check there are no CR ('\r') characters in the file. |
336 if '\r' in xml: | 340 if '\r' in xml: |
337 logging.info('DOS-style line endings (CR characters) detected - these are ' | 341 logging.info('DOS-style line endings (CR characters) detected - these are ' |
338 'not allowed. Please run dos2unix %s' % histograms_filename) | 342 'not allowed. Please run dos2unix %s' % histograms_filename) |
339 sys.exit(1) | 343 sys.exit(1) |
340 | 344 |
341 logging.info('Pretty-printing...') | 345 logging.info('Pretty-printing...') |
(...skipping 19 matching lines...) Expand all Loading... |
361 logging.info('Creating backup file %s' % histograms_backup_filename) | 365 logging.info('Creating backup file %s' % histograms_backup_filename) |
362 shutil.move(histograms_pathname, histograms_backup_pathname) | 366 shutil.move(histograms_pathname, histograms_backup_pathname) |
363 | 367 |
364 logging.info('Writing new %s file' % histograms_filename) | 368 logging.info('Writing new %s file' % histograms_filename) |
365 with open(histograms_pathname, 'wb') as f: | 369 with open(histograms_pathname, 'wb') as f: |
366 f.write(pretty) | 370 f.write(pretty) |
367 | 371 |
368 | 372 |
369 if __name__ == '__main__': | 373 if __name__ == '__main__': |
370 main() | 374 main() |
OLD | NEW |