Commit fec9a648 authored by gavinp@chromium.org's avatar gavinp@chromium.org

Have pretty_print.py find histograms.xml.

Whenever I need to pretty print histograms.xml, I always forget that
it wants to be run from the directory containing histograms.xml. This
CL fixes that, by having the tool look for the directory from which it's
running.

R=isherman@chromium.org

Review URL: https://codereview.chromium.org/121403002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243267 0039d316-1c4b-4281-b951-d872f2087c98
parent ea512b51
...@@ -17,11 +17,14 @@ from __future__ import with_statement ...@@ -17,11 +17,14 @@ from __future__ import with_statement
import diffutil import diffutil
import json import json
import logging import logging
import os
import shutil import shutil
import sys import sys
import textwrap import textwrap
import xml.dom.minidom import xml.dom.minidom
sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'python'))
from google import path_utils
WRAP_COLUMN = 80 WRAP_COLUMN = 80
...@@ -316,14 +319,23 @@ def main(): ...@@ -316,14 +319,23 @@ def main():
presubmit = ('--presubmit' in sys.argv) presubmit = ('--presubmit' in sys.argv)
logging.info('Loading histograms.xml...') histograms_filename = 'histograms.xml'
with open('histograms.xml', 'rb') as f: histograms_backup_filename = 'histograms.before.pretty-print.xml'
script_dir = path_utils.ScriptDir()
histograms_pathname = os.path.join(script_dir, histograms_filename)
histograms_backup_pathname = os.path.join(script_dir,
histograms_backup_filename)
logging.info('Loading %s...' % histograms_filename)
with open(histograms_pathname, 'rb') as f:
xml = f.read() xml = f.read()
# Check there are no CR ('\r') characters in the file. # Check there are no CR ('\r') characters in the file.
if '\r' in xml: if '\r' in xml:
logging.info('DOS-style line endings (CR characters) detected - these are ' logging.info('DOS-style line endings (CR characters) detected - these are '
'not allowed. Please run dos2unix histograms.xml') 'not allowed. Please run dos2unix %s' % histograms_filename)
sys.exit(1) sys.exit(1)
logging.info('Pretty-printing...') logging.info('Pretty-printing...')
...@@ -334,11 +346,11 @@ def main(): ...@@ -334,11 +346,11 @@ def main():
sys.exit(1) sys.exit(1)
if xml == pretty: if xml == pretty:
logging.info('histograms.xml is correctly pretty-printed.') logging.info('%s is correctly pretty-printed.' % histograms_filename)
sys.exit(0) sys.exit(0)
if presubmit: if presubmit:
logging.info('histograms.xml is not formatted correctly; run ' logging.info('%s is not formatted correctly; run pretty_print.py to fix.' %
'pretty_print.py to fix.') histograms_filename)
sys.exit(1) sys.exit(1)
if not diffutil.PromptUserToAcceptDiff( if not diffutil.PromptUserToAcceptDiff(
xml, pretty, xml, pretty,
...@@ -346,11 +358,11 @@ def main(): ...@@ -346,11 +358,11 @@ def main():
logging.error('Aborting') logging.error('Aborting')
return return
logging.info('Creating backup file histograms.before.pretty-print.xml') logging.info('Creating backup file %s' % histograms_backup_filename)
shutil.move('histograms.xml', 'histograms.before.pretty-print.xml') shutil.move(histograms_pathname, histograms_backup_pathname)
logging.info('Writing new histograms.xml file') logging.info('Writing new %s file' % histograms_filename)
with open('histograms.xml', 'wb') as f: with open(histograms_pathname, 'wb') as f:
f.write(pretty) f.write(pretty)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment