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
3 # found in the LICENSE file.
12 sys
.path
.insert(1, os
.path
.join(sys
.path
[0], '..', '..', 'python'))
13 from google
import path_utils
15 def DoPresubmitMain(argv
, original_filename
, backup_filename
, script_name
,
17 """Execute presubmit/pretty printing for the target file.
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
28 An exit status. Non-zero indicates errors.
30 logging
.basicConfig(level
=logging
.INFO
)
31 presubmit
= ('--presubmit' in argv
)
33 # If there is a description xml in the current working directory, use that.
34 # Otherwise, use the one residing in the same directory as this script.
36 if not os
.path
.isfile(os
.path
.join(xml_dir
, original_filename
)):
37 xml_dir
= path_utils
.ScriptDir()
39 xml_path
= os
.path
.join(xml_dir
, original_filename
)
41 # Save the original file content.
42 logging
.info('Loading %s...', os
.path
.relpath(xml_path
))
43 with
open(xml_path
, 'rb') as f
:
44 original_xml
= f
.read()
46 # Check there are no CR ('\r') characters in the file.
47 if '\r' in original_xml
:
48 logging
.error('DOS-style line endings (CR characters) detected - these are '
49 'not allowed. Please run dos2unix %s', original_filename
)
53 pretty
= prettyFn(original_xml
)
55 logging
.error('Aborting parsing due to fatal errors.')
58 if original_xml
== pretty
:
59 logging
.info('%s is correctly pretty-printed.', original_filename
)
62 logging
.error('%s is not formatted correctly; run %s to fix.',
63 original_filename
, script_name
)
66 # Prompt user to consent on the change.
67 if not diff_util
.PromptUserToAcceptDiff(
68 original_xml
, pretty
, 'Is the new version acceptable?'):
69 logging
.error('Diff not accepted. Aborting.')
72 logging
.info('Creating backup file: %s', backup_filename
)
73 shutil
.move(xml_path
, os
.path
.join(xml_dir
, backup_filename
))
75 with
open(xml_path
, 'wb') as f
:
77 logging
.info('Updated %s. Don\'t forget to add it to your changelist',