| Index: tools/metrics/common/presubmit_util.py
|
| diff --git a/tools/metrics/common/presubmit_util.py b/tools/metrics/common/presubmit_util.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..235bdda0b3fb522409e1baea83f8133772a6e020
|
| --- /dev/null
|
| +++ b/tools/metrics/common/presubmit_util.py
|
| @@ -0,0 +1,60 @@
|
| +# Copyright (c) 2015 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +import os
|
| +import sys
|
| +import logging
|
| +import shutil
|
| +
|
| +import diff_util
|
| +
|
| +sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'python'))
|
| +from google import path_utils
|
| +
|
| +def DoPresubmitMain(argv, original_filename, backup_filename, script_name,
|
| + prettyFn):
|
| + """Execute presubmit/pretty printing for the target file.
|
| +
|
| + Args:
|
| + argv: command line arguments
|
| + original_filename: The filename to read from.
|
| + backup_filename: When pretty printing, move the old file contents here.
|
| + script_name: The name of the script to run for pretty printing.
|
| + prettyFn: A function which takes the original xml content and produces
|
| + pretty printed xml.
|
| +
|
| + Returns:
|
| + An exit status. Non-zero indicates errors.
|
| + """
|
| + presubmit = ('--presubmit' in argv)
|
| + xml_path = os.path.join(path_utils.ScriptDir(), original_filename)
|
| +
|
| + # Save the original file content.
|
| + with open(xml_path, 'rb') as f:
|
| + original_xml = f.read()
|
| +
|
| + pretty = prettyFn(original_xml)
|
| +
|
| + if original_xml == pretty:
|
| + print '%s is correctly pretty-printed.' % original_filename
|
| + return 0
|
| + if presubmit:
|
| + logging.info('%s is not formatted correctly; run %s to fix.' % (
|
| + original_filename, script_name))
|
| + return 1
|
| +
|
| + # Prompt user to consent on the change.
|
| + if not diff_util.PromptUserToAcceptDiff(
|
| + original_xml, pretty, 'Is the new version acceptable?'):
|
| + logging.error('Aborting')
|
| + return 1
|
| +
|
| + print 'Creating backup file: %s' % backup_filename
|
| + shutil.move(xml_path, backup_filename)
|
| +
|
| + with open(xml_path, 'wb') as f:
|
| + f.write(pretty)
|
| + print ('Updated %s. Don\'t forget to add it to your changelist' %
|
| + xml_path)
|
| + return 0
|
|
|