Index: tools/metrics/histograms/pretty_print.py |
diff --git a/tools/metrics/histograms/pretty_print.py b/tools/metrics/histograms/pretty_print.py |
index 41cc2b6126cba9876d454dd66a56bbd0cbe107c8..d9e0327952beb283a0ef484229851544735a09e8 100755 |
--- a/tools/metrics/histograms/pretty_print.py |
+++ b/tools/metrics/histograms/pretty_print.py |
@@ -28,7 +28,6 @@ |
# Import the metrics/common module. |
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) |
import diff_util |
-import presubmit_util |
# Tags whose children we want to alphabetize. The key is the parent tag name, |
# and the value is a pair of the tag name of the children we want to sort, |
@@ -131,10 +130,62 @@ |
tree = TransformByAlphabetizing(tree) |
return print_style.GetPrintStyle().PrettyPrintNode(tree) |
+ |
def main(): |
- presubmit_util.DoPresubmitMain(sys.argv, 'histograms.xml', |
- 'histograms.before.pretty-print.xml', |
- 'pretty_print.py', PrettyPrint) |
+ logging.basicConfig(level=logging.INFO) |
+ |
+ presubmit = ('--presubmit' in sys.argv) |
+ |
+ histograms_filename = 'histograms.xml' |
+ histograms_backup_filename = 'histograms.before.pretty-print.xml' |
+ |
+ # If there is a histograms.xml in the current working directory, use that. |
+ # Otherwise, use the one residing in the same directory as this script. |
+ histograms_dir = os.getcwd() |
+ if not os.path.isfile(os.path.join(histograms_dir, histograms_filename)): |
+ histograms_dir = path_utils.ScriptDir() |
+ |
+ histograms_pathname = os.path.join(histograms_dir, histograms_filename) |
+ histograms_backup_pathname = os.path.join(histograms_dir, |
+ histograms_backup_filename) |
+ |
+ logging.info('Loading %s...' % os.path.relpath(histograms_pathname)) |
+ with open(histograms_pathname, 'rb') as f: |
+ xml = f.read() |
+ |
+ # Check there are no CR ('\r') characters in the file. |
+ if '\r' in xml: |
+ logging.info('DOS-style line endings (CR characters) detected - these are ' |
+ 'not allowed. Please run dos2unix %s' % histograms_filename) |
+ sys.exit(1) |
+ |
+ logging.info('Pretty-printing...') |
+ try: |
+ pretty = PrettyPrint(xml) |
+ except Error: |
+ logging.error('Aborting parsing due to fatal errors.') |
+ sys.exit(1) |
+ |
+ if xml == pretty: |
+ logging.info('%s is correctly pretty-printed.' % histograms_filename) |
+ sys.exit(0) |
+ if presubmit: |
+ logging.info('%s is not formatted correctly; run pretty_print.py to fix.' % |
+ histograms_filename) |
+ sys.exit(1) |
+ if not diff_util.PromptUserToAcceptDiff( |
+ xml, pretty, |
+ 'Is the prettified version acceptable?'): |
+ logging.error('Aborting') |
+ return |
+ |
+ logging.info('Creating backup file %s' % histograms_backup_filename) |
+ shutil.move(histograms_pathname, histograms_backup_pathname) |
+ |
+ logging.info('Writing new %s file' % histograms_filename) |
+ with open(histograms_pathname, 'wb') as f: |
+ f.write(pretty) |
+ |
if __name__ == '__main__': |
main() |