forked from firebase/firebase-ios-sdk
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_lint.py
executable file
·218 lines (153 loc) · 5.29 KB
/
check_lint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
# Copyright 2019 Google
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Lints source files for conformance with the style guide that applies.
Currently supports linting Objective-C, Objective-C++, C++, and Python source.
"""
importargparse
importlogging
importos
importsubprocess
importsys
importtextwrap
fromlibimportchecker
fromlibimportcommand_trace
fromlibimportgit
fromlibimportsource
_logger=logging.getLogger('lint')
_dry_run=False
_CPPLINT_OBJC_FILTERS= [
# Objective-C uses #import and does not use header guards
'-build/header_guard',
# Inline definitions of Objective-C blocks confuse
'-readability/braces',
# C-style casts are acceptable in Objective-C++
'-readability/casting',
# Objective-C needs use type 'long' for interop between types like NSInteger
# and printf-style functions.
'-runtime/int',
# cpplint is generally confused by Objective-C mixing with C++.
# * Objective-C method invocations in a for loop make it think its a
# range-for
# * Objective-C dictionary literals confuse brace spacing
# * Empty category declarations ("@interface Foo ()") look like function
# invocations
'-whitespace',
]
_CPPLINT_OBJC_OPTIONS= [
# cpplint normally excludes Objective-C++
'--extensions=h,m,mm',
# Objective-C style allows longer lines
'--linelength=100',
'--filter='+','.join(_CPPLINT_OBJC_FILTERS),
]
defmain():
global_dry_run
parser=argparse.ArgumentParser(description='Lint source files.')
parser.add_argument('--dry-run', '-n', action='store_true',
help='Show what the linter would do without doing it')
parser.add_argument('--all', action='store_true',
help='run the linter over all known sources')
parser.add_argument('rev_or_files', nargs='*',
help='A single revision that specifies a point in time '
'from which to look for changes. Defaults to '
'origin/master. Alternatively, a list of specific '
'files or git pathspecs to lint.')
args=command_trace.parse_args(parser)
ifargs.dry_run:
_dry_run=True
command_trace.enable_tracing()
pool=checker.Pool()
sources=_unique(source.CC_DIRS+source.OBJC_DIRS+source.PYTHON_DIRS)
patterns=git.make_patterns(sources)
files=git.find_changed_or_files(args.all, args.rev_or_files, patterns)
check(pool, files)
pool.exit()
defcheck(pool, files):
group=source.categorize_files(files)
forkind, filesingroup.kinds.items():
forchunkinchecker.shard(files):
ifnotchunk:
continue
linter=_linters[kind]
pool.submit(linter, chunk)
deflint_cc(files):
return_run_cpplint([], files)
deflint_objc(files):
return_run_cpplint(_CPPLINT_OBJC_OPTIONS, files)
def_run_cpplint(options, files):
scripts_dir=os.path.dirname(os.path.abspath(__file__))
cpplint=os.path.join(scripts_dir, 'cpplint.py')
command= [sys.executable, cpplint, '--quiet']
command.extend(options)
command.extend(files)
return_read_output(command)
_flake8_warned=False
deflint_py(files):
flake8=which('flake8')
ifflake8isNone:
global_flake8_warned
ifnot_flake8_warned:
_flake8_warned=True
_logger.warn(textwrap.dedent(
"""
Could not find flake8 on $PATH; skipping python lint.
Install with:
pip install --user flake8
"""))
return
command= [flake8]
command.extend(files)
return_read_output(command)
def_read_output(command):
command_trace.log(command)
if_dry_run:
returnchecker.Result(0, '')
proc=subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output=proc.communicate('')[0]
sc=proc.wait()
returnchecker.Result(sc, output)
_linters= {
'cc': lint_cc,
'objc': lint_objc,
'py': lint_py,
}
def_unique(items):
returnlist(set(items))
defwhich(executable):
"""Finds the executable with the given name.
Returns:
The fully qualified path to the executable or None if the executable isn't
found.
"""
ifexecutable.startswith('/'):
returnexecutable
path=os.environ['PATH'].split(os.pathsep)
forexecutable_with_extin_executable_names(executable):
forentryinpath:
joined=os.path.join(entry, executable_with_ext)
ifos.path.isfile(joined) andos.access(joined, os.X_OK):
returnjoined
returnNone
def_executable_names(executable):
"""Yields a sequence of all possible executable names."""
ifos.name=='nt':
pathext=os.environ.get('PATHEXT', '').split(os.pathsep)
forextinpathext:
yieldexecutable+ext
else:
yieldexecutable
if__name__=='__main__':
main()