We use the one installed from pypi in the tox venv, so dump
the original. Fix up run_tests.sh.
Change-Id: I6b0aa1da9bfa4d742a7210e6ff800d72492a2178
50 | 50 |
deleted file mode 100755 |
... | ... |
@@ -1,215 +0,0 @@ |
1 |
-#!/usr/bin/env python |
|
2 |
-# |
|
3 |
-# Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 |
-# you may not use this file except in compliance with the License. |
|
5 |
-# You may obtain a copy of the License at |
|
6 |
-# |
|
7 |
-# http://www.apache.org/licenses/LICENSE-2.0 |
|
8 |
-# |
|
9 |
-# Unless required by applicable law or agreed to in writing, software |
|
10 |
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
11 |
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
12 |
-# License for the specific language governing permissions and limitations |
|
13 |
-# under the License. |
|
14 |
- |
|
15 |
-# bash8 - a pep8 equivalent for bash scripts |
|
16 |
-# |
|
17 |
-# this program attempts to be an automated style checker for bash scripts |
|
18 |
-# to fill the same part of code review that pep8 does in most OpenStack |
|
19 |
-# projects. It starts from humble beginnings, and will evolve over time. |
|
20 |
-# |
|
21 |
-# Currently Supported checks |
|
22 |
-# |
|
23 |
-# Errors |
|
24 |
-# Basic white space errors, for consistent indenting |
|
25 |
-# - E001: check that lines do not end with trailing whitespace |
|
26 |
-# - E002: ensure that indents are only spaces, and not hard tabs |
|
27 |
-# - E003: ensure all indents are a multiple of 4 spaces |
|
28 |
-# - E004: file did not end with a newline |
|
29 |
-# |
|
30 |
-# Structure errors |
|
31 |
-# |
|
32 |
-# A set of rules that help keep things consistent in control blocks. |
|
33 |
-# These are ignored on long lines that have a continuation, because |
|
34 |
-# unrolling that is kind of "interesting" |
|
35 |
-# |
|
36 |
-# - E010: *do* not on the same line as *for* |
|
37 |
-# - E011: *then* not on the same line as *if* |
|
38 |
-# - E012: heredoc didn't end before EOF |
|
39 |
- |
|
40 |
-import argparse |
|
41 |
-import fileinput |
|
42 |
-import re |
|
43 |
-import sys |
|
44 |
- |
|
45 |
-ERRORS = 0 |
|
46 |
-IGNORE = None |
|
47 |
- |
|
48 |
- |
|
49 |
-def register_ignores(ignores): |
|
50 |
- global IGNORE |
|
51 |
- if ignores: |
|
52 |
- IGNORE = '^(' + '|'.join(ignores.split(',')) + ')' |
|
53 |
- |
|
54 |
- |
|
55 |
-def should_ignore(error): |
|
56 |
- return IGNORE and re.search(IGNORE, error) |
|
57 |
- |
|
58 |
- |
|
59 |
-def print_error(error, line, |
|
60 |
- filename=None, filelineno=None): |
|
61 |
- if not filename: |
|
62 |
- filename = fileinput.filename() |
|
63 |
- if not filelineno: |
|
64 |
- filelineno = fileinput.filelineno() |
|
65 |
- global ERRORS |
|
66 |
- ERRORS = ERRORS + 1 |
|
67 |
- print("%s: '%s'" % (error, line.rstrip('\n'))) |
|
68 |
- print(" - %s: L%s" % (filename, filelineno)) |
|
69 |
- |
|
70 |
- |
|
71 |
-def not_continuation(line): |
|
72 |
- return not re.search('\\\\$', line) |
|
73 |
- |
|
74 |
- |
|
75 |
-def check_for_do(line): |
|
76 |
- if not_continuation(line): |
|
77 |
- match = re.match('^\s*(for|while|until)\s', line) |
|
78 |
- if match: |
|
79 |
- operator = match.group(1).strip() |
|
80 |
- if not re.search(';\s*do(\b|$)', line): |
|
81 |
- print_error('E010: Do not on same line as %s' % operator, |
|
82 |
- line) |
|
83 |
- |
|
84 |
- |
|
85 |
-def check_if_then(line): |
|
86 |
- if not_continuation(line): |
|
87 |
- if re.search('^\s*if \[', line): |
|
88 |
- if not re.search(';\s*then(\b|$)', line): |
|
89 |
- print_error('E011: Then non on same line as if', line) |
|
90 |
- |
|
91 |
- |
|
92 |
-def check_no_trailing_whitespace(line): |
|
93 |
- if re.search('[ \t]+$', line): |
|
94 |
- print_error('E001: Trailing Whitespace', line) |
|
95 |
- |
|
96 |
- |
|
97 |
-def check_indents(line): |
|
98 |
- m = re.search('^(?P<indent>[ \t]+)', line) |
|
99 |
- if m: |
|
100 |
- if re.search('\t', m.group('indent')): |
|
101 |
- print_error('E002: Tab indents', line) |
|
102 |
- if (len(m.group('indent')) % 4) != 0: |
|
103 |
- print_error('E003: Indent not multiple of 4', line) |
|
104 |
- |
|
105 |
-def check_function_decl(line): |
|
106 |
- failed = False |
|
107 |
- if line.startswith("function"): |
|
108 |
- if not re.search('^function [\w-]* \{$', line): |
|
109 |
- failed = True |
|
110 |
- else: |
|
111 |
- # catch the case without "function", e.g. |
|
112 |
- # things like '^foo() {' |
|
113 |
- if re.search('^\s*?\(\)\s*?\{', line): |
|
114 |
- failed = True |
|
115 |
- |
|
116 |
- if failed: |
|
117 |
- print_error('E020: Function declaration not in format ' |
|
118 |
- ' "^function name {$"', line) |
|
119 |
- |
|
120 |
- |
|
121 |
-def starts_multiline(line): |
|
122 |
- m = re.search("[^<]<<\s*(?P<token>\w+)", line) |
|
123 |
- if m: |
|
124 |
- return m.group('token') |
|
125 |
- else: |
|
126 |
- return False |
|
127 |
- |
|
128 |
- |
|
129 |
-def end_of_multiline(line, token): |
|
130 |
- if token: |
|
131 |
- return re.search("^%s\s*$" % token, line) is not None |
|
132 |
- return False |
|
133 |
- |
|
134 |
- |
|
135 |
-def check_files(files, verbose): |
|
136 |
- in_multiline = False |
|
137 |
- multiline_start = 0 |
|
138 |
- multiline_line = "" |
|
139 |
- logical_line = "" |
|
140 |
- token = False |
|
141 |
- prev_file = None |
|
142 |
- prev_line = "" |
|
143 |
- prev_lineno = 0 |
|
144 |
- |
|
145 |
- for line in fileinput.input(files): |
|
146 |
- if fileinput.isfirstline(): |
|
147 |
- # if in_multiline when the new file starts then we didn't |
|
148 |
- # find the end of a heredoc in the last file. |
|
149 |
- if in_multiline: |
|
150 |
- print_error('E012: heredoc did not end before EOF', |
|
151 |
- multiline_line, |
|
152 |
- filename=prev_file, filelineno=multiline_start) |
|
153 |
- in_multiline = False |
|
154 |
- |
|
155 |
- # last line of a previous file should always end with a |
|
156 |
- # newline |
|
157 |
- if prev_file and not prev_line.endswith('\n'): |
|
158 |
- print_error('E004: file did not end with a newline', |
|
159 |
- prev_line, |
|
160 |
- filename=prev_file, filelineno=prev_lineno) |
|
161 |
- |
|
162 |
- prev_file = fileinput.filename() |
|
163 |
- |
|
164 |
- if verbose: |
|
165 |
- print "Running bash8 on %s" % fileinput.filename() |
|
166 |
- |
|
167 |
- # NOTE(sdague): multiline processing of heredocs is interesting |
|
168 |
- if not in_multiline: |
|
169 |
- logical_line = line |
|
170 |
- token = starts_multiline(line) |
|
171 |
- if token: |
|
172 |
- in_multiline = True |
|
173 |
- multiline_start = fileinput.filelineno() |
|
174 |
- multiline_line = line |
|
175 |
- continue |
|
176 |
- else: |
|
177 |
- logical_line = logical_line + line |
|
178 |
- if not end_of_multiline(line, token): |
|
179 |
- continue |
|
180 |
- else: |
|
181 |
- in_multiline = False |
|
182 |
- |
|
183 |
- check_no_trailing_whitespace(logical_line) |
|
184 |
- check_indents(logical_line) |
|
185 |
- check_for_do(logical_line) |
|
186 |
- check_if_then(logical_line) |
|
187 |
- check_function_decl(logical_line) |
|
188 |
- |
|
189 |
- prev_line = logical_line |
|
190 |
- prev_lineno = fileinput.filelineno() |
|
191 |
- |
|
192 |
-def get_options(): |
|
193 |
- parser = argparse.ArgumentParser( |
|
194 |
- description='A bash script style checker') |
|
195 |
- parser.add_argument('files', metavar='file', nargs='+', |
|
196 |
- help='files to scan for errors') |
|
197 |
- parser.add_argument('-i', '--ignore', help='Rules to ignore') |
|
198 |
- parser.add_argument('-v', '--verbose', action='store_true', default=False) |
|
199 |
- return parser.parse_args() |
|
200 |
- |
|
201 |
- |
|
202 |
-def main(): |
|
203 |
- opts = get_options() |
|
204 |
- register_ignores(opts.ignore) |
|
205 |
- check_files(opts.files, opts.verbose) |
|
206 |
- |
|
207 |
- if ERRORS > 0: |
|
208 |
- print("%d bash8 error(s) found" % ERRORS) |
|
209 |
- return 1 |
|
210 |
- else: |
|
211 |
- return 0 |
|
212 |
- |
|
213 |
- |
|
214 |
-if __name__ == "__main__": |
|
215 |
- sys.exit(main()) |