OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
| 5 import difflib |
| 6 import logging |
5 import os | 7 import os |
| 8 import shutil |
6 import sys | 9 import sys |
7 import logging | |
8 import shutil | |
9 | 10 |
10 sys.path.append( | 11 sys.path.append( |
11 os.path.join(os.path.dirname(os.path.abspath(__file__)), | 12 os.path.join(os.path.dirname(os.path.abspath(__file__)), |
12 os.pardir, os.pardir, 'python', 'google')) | 13 os.pardir, os.pardir, 'python', 'google')) |
13 import path_utils | 14 import path_utils |
14 | 15 |
15 import diff_util | 16 import diff_util |
16 | 17 |
17 def DoPresubmitMain(argv, original_filename, backup_filename, script_name, | 18 def DoPresubmitMain(argv, original_filename, backup_filename, script_name, |
18 prettyFn): | 19 prettyFn): |
19 """Execute presubmit/pretty printing for the target file. | 20 """Execute presubmit/pretty printing for the target file. |
20 | 21 |
21 Args: | 22 Args: |
22 argv: command line arguments | 23 argv: command line arguments |
23 original_filename: The filename to read from. | 24 original_filename: The filename to read from. |
24 backup_filename: When pretty printing, move the old file contents here. | 25 backup_filename: When pretty printing, move the old file contents here. |
25 script_name: The name of the script to run for pretty printing. | 26 script_name: The name of the script to run for pretty printing. |
26 prettyFn: A function which takes the original xml content and produces | 27 prettyFn: A function which takes the original xml content and produces |
27 pretty printed xml. | 28 pretty printed xml. |
28 | 29 |
29 Returns: | 30 Returns: |
30 An exit status. Non-zero indicates errors. | 31 An exit status. Non-zero indicates errors. |
31 """ | 32 """ |
32 logging.basicConfig(level=logging.INFO) | 33 # interactive: Print log info messages and prompt user to accept the diff. |
| 34 interactive = ('--non-interactive' not in argv) |
| 35 # presubmit: Simply print a message if the input is not formatted correctly. |
33 presubmit = ('--presubmit' in argv) | 36 presubmit = ('--presubmit' in argv) |
| 37 # diff: Print diff to stdout rather than modifying files. |
| 38 diff = ('--diff' in argv) |
| 39 |
| 40 if interactive: |
| 41 logging.basicConfig(level=logging.INFO) |
| 42 else: |
| 43 logging.basicConfig(level=logging.ERROR) |
34 | 44 |
35 # If there is a description xml in the current working directory, use that. | 45 # If there is a description xml in the current working directory, use that. |
36 # Otherwise, use the one residing in the same directory as this script. | 46 # Otherwise, use the one residing in the same directory as this script. |
37 xml_dir = os.getcwd() | 47 xml_dir = os.getcwd() |
38 if not os.path.isfile(os.path.join(xml_dir, original_filename)): | 48 if not os.path.isfile(os.path.join(xml_dir, original_filename)): |
39 xml_dir = path_utils.ScriptDir() | 49 xml_dir = path_utils.ScriptDir() |
40 | 50 |
41 xml_path = os.path.join(xml_dir, original_filename) | 51 xml_path = os.path.join(xml_dir, original_filename) |
42 | 52 |
43 # Save the original file content. | 53 # Save the original file content. |
44 logging.info('Loading %s...', os.path.relpath(xml_path)) | 54 logging.info('Loading %s...', os.path.relpath(xml_path)) |
45 with open(xml_path, 'rb') as f: | 55 with open(xml_path, 'rb') as f: |
46 original_xml = f.read() | 56 original_xml = f.read() |
47 | 57 |
48 # Check there are no CR ('\r') characters in the file. | 58 # Check there are no CR ('\r') characters in the file. |
49 if '\r' in original_xml: | 59 if '\r' in original_xml: |
50 logging.error('DOS-style line endings (CR characters) detected - these are ' | 60 logging.error('DOS-style line endings (CR characters) detected - these are ' |
51 'not allowed. Please run dos2unix %s', original_filename) | 61 'not allowed. Please run dos2unix %s', original_filename) |
52 sys.exit(1) | 62 sys.exit(1) |
53 | 63 |
54 try: | 64 try: |
55 pretty = prettyFn(original_xml) | 65 pretty = prettyFn(original_xml) |
56 except Exception as e: | 66 except Exception as e: |
57 logging.exception('Aborting parsing due to fatal errors:') | 67 logging.exception('Aborting parsing due to fatal errors:') |
58 sys.exit(1) | 68 sys.exit(1) |
59 | 69 |
60 if original_xml == pretty: | 70 if original_xml == pretty: |
61 logging.info('%s is correctly pretty-printed.', original_filename) | 71 logging.info('%s is correctly pretty-printed.', original_filename) |
62 sys.exit(0) | 72 sys.exit(0) |
| 73 |
63 if presubmit: | 74 if presubmit: |
64 logging.error('%s is not formatted correctly; run %s to fix.', | 75 logging.error('%s is not formatted correctly; run %s to fix.', |
65 original_filename, script_name) | 76 original_filename, script_name) |
66 sys.exit(1) | 77 sys.exit(1) |
67 | 78 |
68 # Prompt user to consent on the change. | 79 # Prompt user to consent on the change. |
69 if not diff_util.PromptUserToAcceptDiff( | 80 if interactive and not diff_util.PromptUserToAcceptDiff( |
70 original_xml, pretty, 'Is the new version acceptable?'): | 81 original_xml, pretty, 'Is the new version acceptable?'): |
71 logging.error('Diff not accepted. Aborting.') | 82 logging.error('Diff not accepted. Aborting.') |
72 sys.exit(1) | 83 sys.exit(1) |
73 | 84 |
| 85 if diff: |
| 86 for line in difflib.unified_diff(original_xml.splitlines(), |
| 87 pretty.splitlines()): |
| 88 print line |
| 89 sys.exit(0) |
| 90 |
74 logging.info('Creating backup file: %s', backup_filename) | 91 logging.info('Creating backup file: %s', backup_filename) |
75 shutil.move(xml_path, os.path.join(xml_dir, backup_filename)) | 92 shutil.move(xml_path, os.path.join(xml_dir, backup_filename)) |
76 | 93 |
77 with open(xml_path, 'wb') as f: | 94 with open(xml_path, 'wb') as f: |
78 f.write(pretty) | 95 f.write(pretty) |
79 logging.info('Updated %s. Don\'t forget to add it to your changelist', | 96 logging.info('Updated %s. Don\'t forget to add it to your changelist', |
80 xml_path) | 97 xml_path) |
81 sys.exit(0) | 98 sys.exit(0) |
OLD | NEW |