| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Top-level presubmit script for Chromium. | 6 """Top-level presubmit script for Chromium. |
| 7 | 7 |
| 8 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for | 8 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for |
| 9 details on the presubmit API built into gcl. | 9 details on the presubmit API built into gcl. |
| 10 """ | 10 """ |
| 11 | 11 |
| 12 | |
| 13 import os | |
| 14 | |
| 15 | |
| 16 # Files with these extensions will be considered source files | 12 # Files with these extensions will be considered source files |
| 17 SOURCE_FILE_EXTENSIONS = ['.c', '.cc', '.cpp', '.h', '.m', '.mm', '.py'] | 13 SOURCE_FILE_EXTENSIONS = [ |
| 18 | 14 '.c', '.cc', '.cpp', '.h', '.m', '.mm', '.py', '.mk', '.am', '.html', |
| 15 '.htm', '.json', |
| 16 ] |
| 17 EXCLUDED_PATHS = [ |
| 18 r"breakpad[\\\/].*", |
| 19 r"chrome[\\\/]Debug[\\\/].*", |
| 20 r"chrome[\\\/]Hammer[\\\/].*", |
| 21 r"chrome[\\\/]Release[\\\/].*", |
| 22 r"xcodebuild[\\\/].*", |
| 23 r"skia[\\\/].*", |
| 24 r".*third_party[\\\/].*", |
| 25 r"v8[\\\/].*", |
| 26 ] |
| 19 | 27 |
| 20 def ReadFile(path): | 28 def ReadFile(path): |
| 21 """Given a path, returns the full contents of the file. | 29 """Given a path, returns the full contents of the file. |
| 22 | 30 |
| 23 Reads files in binary format. | 31 Reads files in binary format. |
| 24 """ | 32 """ |
| 25 fo = open(path, 'rb') | 33 fo = open(path, 'rb') |
| 26 try: | 34 try: |
| 27 contents = fo.read() | 35 contents = fo.read() |
| 28 finally: | 36 finally: |
| 29 fo.close() | 37 fo.close() |
| 30 return contents | 38 return contents |
| 31 | 39 |
| 32 | 40 |
| 33 # Seam for unit testing | 41 # Seam for unit testing |
| 34 _ReadFile = ReadFile | 42 _ReadFile = ReadFile |
| 35 | 43 |
| 36 | 44 |
| 37 def CheckChangeOnUpload(input_api, output_api): | 45 def CheckChangeOnUpload(input_api, output_api): |
| 38 return (CheckNoCrOrTabs(input_api, output_api) + | 46 return LocalChecks(input_api, output_api) |
| 47 |
| 48 |
| 49 def CheckChangeOnCommit(input_api, output_api): |
| 50 return (LocalChecks(input_api, output_api) + |
| 39 input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)) | 51 input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)) |
| 40 | 52 |
| 41 | 53 |
| 42 def CheckChangeOnCommit(input_api, output_api): | 54 def LocalChecks(input_api, output_api, max_cols=80): |
| 43 # No extra checks on commit for now | 55 """Reports an error if for any source file in SOURCE_FILE_EXTENSIONS: |
| 44 return CheckChangeOnUpload(input_api, output_api) | 56 - uses CR (or CRLF) |
| 57 - contains a TAB |
| 58 - has a line that ends with whitespace |
| 59 - contains a line >|max_cols| cols unless |max_cols| is 0. |
| 45 | 60 |
| 46 | 61 Note that the whole file is checked, not only the changes. |
| 47 def CheckNoCrOrTabs(input_api, output_api): | |
| 48 """Reports an error if source files use CR (or CRLF) or TAB. | |
| 49 """ | 62 """ |
| 50 cr_files = [] | 63 cr_files = [] |
| 51 tab_files = [] | 64 tab_files = [] |
| 52 results = [] | 65 results = [] |
| 53 | 66 excluded_paths = [input_api.re.compile(x) for x in EXCLUDED_PATHS] |
| 54 for f in input_api.AffectedTextFiles(include_deletes=False): | 67 files = input_api.AffectedFiles() |
| 68 for f in files: |
| 55 path = f.LocalPath() | 69 path = f.LocalPath() |
| 56 root, ext = os.path.splitext(path) | 70 root, ext = input_api.os_path.splitext(path) |
| 57 if ext in SOURCE_FILE_EXTENSIONS: | 71 # Look for unsupported extensions. |
| 58 # Need to read the file ourselves since AffectedFile.NewContents() | 72 if not ext in SOURCE_FILE_EXTENSIONS: |
| 59 # will normalize line endings. | 73 continue |
| 60 contents = _ReadFile(path) | 74 # Look for excluded paths. |
| 61 if '\r' in contents: | 75 found = False |
| 62 cr_files.append(path) | 76 for item in excluded_paths: |
| 63 if '\t' in contents: | 77 if item.match(path): |
| 64 tab_files.append(path) | 78 found = True |
| 79 break |
| 80 if found: |
| 81 continue |
| 82 |
| 83 # Need to read the file ourselves since AffectedFile.NewContents() |
| 84 # will normalize line endings. |
| 85 contents = _ReadFile(path) |
| 86 if '\r' in contents: |
| 87 cr_files.append(path) |
| 88 |
| 89 local_errors = [] |
| 90 # Remove EOL character. |
| 91 lines = contents.splitlines(False) |
| 92 line_num = 1 |
| 93 for line in lines: |
| 94 if line.endswith(' '): |
| 95 local_errors.append(output_api.PresubmitError( |
| 96 '%s, line %s ends with whitespaces.' % |
| 97 (path, line_num))) |
| 98 # Accept lines with http:// to exceed the max_cols rule. |
| 99 if max_cols and len(line) > max_cols and not 'http://' in line: |
| 100 local_errors.append(output_api.PresubmitError( |
| 101 '%s, line %s has %s chars, please reduce to %d chars.' % |
| 102 (path, line_num, len(line), max_cols))) |
| 103 if '\t' in line: |
| 104 local_errors.append(output_api.PresubmitError( |
| 105 "%s, line %s contains a tab character." % |
| 106 (path, line_num))) |
| 107 line_num += 1 |
| 108 # Just show the first 5 errors. |
| 109 results.extend(local_errors[0:min(len(local_errors), 5)]) |
| 110 |
| 65 if cr_files: | 111 if cr_files: |
| 66 results.append(output_api.PresubmitError( | 112 results.append(output_api.PresubmitError( |
| 67 'Found CR (or CRLF) line ending in these files, please use only LF:', | 113 'Found CR (or CRLF) line ending in these files, please use only LF:', |
| 68 items=cr_files)) | 114 items=cr_files)) |
| 69 if tab_files: | 115 if tab_files: |
| 70 results.append(output_api.PresubmitError( | 116 results.append(output_api.PresubmitError( |
| 71 'Found tabs in the following files, please use spaces', | 117 'Found tabs in the following files, please use spaces', |
| 72 items=tab_files)) | 118 items=tab_files)) |
| 73 return results | 119 return results |
| OLD | NEW |