OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import os |
| 6 import sys |
| 7 import logging |
| 8 import shutil |
| 9 |
| 10 import diff_util |
| 11 |
| 12 sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'python')) |
| 13 from google import path_utils |
| 14 |
| 15 def DoPresubmitMain(argv, original_filename, backup_filename, script_name, |
| 16 prettyFn): |
| 17 """Execute presubmit/pretty printing for the target file. |
| 18 |
| 19 Args: |
| 20 argv: command line arguments |
| 21 original_filename: The filename to read from. |
| 22 backup_filename: When pretty printing, move the old file contents here. |
| 23 script_name: The name of the script to run for pretty printing. |
| 24 prettyFn: A function which takes the original xml content and produces |
| 25 pretty printed xml. |
| 26 |
| 27 Returns: |
| 28 An exit status. Non-zero indicates errors. |
| 29 """ |
| 30 presubmit = ('--presubmit' in argv) |
| 31 xml_path = os.path.join(path_utils.ScriptDir(), original_filename) |
| 32 |
| 33 # Save the original file content. |
| 34 with open(xml_path, 'rb') as f: |
| 35 original_xml = f.read() |
| 36 |
| 37 pretty = prettyFn(original_xml) |
| 38 |
| 39 if original_xml == pretty: |
| 40 print '%s is correctly pretty-printed.' % original_filename |
| 41 return 0 |
| 42 if presubmit: |
| 43 logging.info('%s is not formatted correctly; run %s to fix.' % ( |
| 44 original_filename, script_name)) |
| 45 return 1 |
| 46 |
| 47 # Prompt user to consent on the change. |
| 48 if not diff_util.PromptUserToAcceptDiff( |
| 49 original_xml, pretty, 'Is the new version acceptable?'): |
| 50 logging.error('Aborting') |
| 51 return 1 |
| 52 |
| 53 print 'Creating backup file: %s' % backup_filename |
| 54 shutil.move(xml_path, backup_filename) |
| 55 |
| 56 with open(xml_path, 'wb') as f: |
| 57 f.write(pretty) |
| 58 print ('Updated %s. Don\'t forget to add it to your changelist' % |
| 59 xml_path) |
| 60 return 0 |
OLD | NEW |