blob: 13402161c56c00612c139690b724137f75c1b6c6 [file] [log] [blame]
[email protected]ca8d1982009-02-19 16:33:121#!/usr/bin/python
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
4# found in the LICENSE file.
5
6"""Top-level presubmit script for Chromium.
7
8See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
9details on the presubmit API built into gcl.
10"""
11
12
13import os
14
15
16# Files with these extensions will be considered source files
17SOURCE_FILE_EXTENSIONS = ['.c', '.cc', '.cpp', '.h', '.m', '.mm', '.py']
18
19
20def ReadFile(path):
21 """Given a path, returns the full contents of the file.
22
23 Reads files in binary format.
24 """
25 fo = open(path, 'rb')
26 try:
27 contents = fo.read()
28 finally:
29 fo.close()
30 return contents
31
32
33# Seam for unit testing
34_ReadFile = ReadFile
35
36
37def CheckChangeOnUpload(input_api, output_api):
38 return (CheckNoCrOrTabs(input_api, output_api) +
39 input_api.canned_checks.CheckDoNotSubmit(input_api, output_api))
40
41
42def CheckChangeOnCommit(input_api, output_api):
43 # No extra checks on commit for now
44 return CheckChangeOnUpload(input_api, output_api)
45
46
47def CheckNoCrOrTabs(input_api, output_api):
48 """Reports an error if source files use CR (or CRLF) or TAB.
49 """
50 cr_files = []
51 tab_files = []
52 results = []
53
54 for f in input_api.AffectedTextFiles(include_deletes=False):
55 path = f.LocalPath()
56 root, ext = os.path.splitext(path)
57 if ext in SOURCE_FILE_EXTENSIONS:
58 # Need to read the file ourselves since AffectedFile.NewContents()
59 # will normalize line endings.
60 contents = _ReadFile(path)
61 if '\r' in contents:
62 cr_files.append(path)
63 if '\t' in contents:
64 tab_files.append(path)
65 if cr_files:
66 results.append(output_api.PresubmitError(
67 'Found CR (or CRLF) line ending in these files, please use only LF:',
68 items=cr_files))
69 if tab_files:
70 results.append(output_api.PresubmitError(
71 'Found tabs in the following files, please use spaces',
72 items=tab_files))
73 return results