blob: f89b24169c0f5ff2a8f18bbbb67fdfcce4dcc3fa [file] [log] [blame]
Sean Dague4fb255c2013-10-14 14:07:00 -04001#!/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
Sean Dague16dd8b32014-02-03 09:10:54 +090024# Basic white space errors, for consistent indenting
Sean Dague4fb255c2013-10-14 14:07:00 -040025# - 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
Ian Wienandb8e25022014-02-21 16:14:29 +110028# - E004: file did not end with a newline
Sean Dague16dd8b32014-02-03 09:10:54 +090029#
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*
Ian Wienandb8e25022014-02-21 16:14:29 +110038# - E012: heredoc didn't end before EOF
Sean Dague4fb255c2013-10-14 14:07:00 -040039
40import argparse
41import fileinput
42import re
43import sys
44
Sean Dague4fb255c2013-10-14 14:07:00 -040045ERRORS = 0
Sean Dague0656e122014-02-03 08:49:30 +090046IGNORE = None
47
48
49def register_ignores(ignores):
50 global IGNORE
51 if ignores:
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010052 IGNORE = '^(' + '|'.join(ignores.split(',')) + ')'
Sean Dague0656e122014-02-03 08:49:30 +090053
54
55def should_ignore(error):
56 return IGNORE and re.search(IGNORE, error)
Sean Dague4fb255c2013-10-14 14:07:00 -040057
58
Ian Wienandb8e25022014-02-21 16:14:29 +110059def 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()
Sean Dague4fb255c2013-10-14 14:07:00 -040065 global ERRORS
66 ERRORS = ERRORS + 1
67 print("%s: '%s'" % (error, line.rstrip('\n')))
Ian Wienandb8e25022014-02-21 16:14:29 +110068 print(" - %s: L%s" % (filename, filelineno))
Sean Dague4fb255c2013-10-14 14:07:00 -040069
70
Sean Dague16dd8b32014-02-03 09:10:54 +090071def not_continuation(line):
72 return not re.search('\\\\$', line)
73
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010074
Sean Dague16dd8b32014-02-03 09:10:54 +090075def check_for_do(line):
76 if not_continuation(line):
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010077 match = re.match('^\s*(for|while|until)\s', line)
78 if match:
79 operator = match.group(1).strip()
Sean Dague16dd8b32014-02-03 09:10:54 +090080 if not re.search(';\s*do(\b|$)', line):
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010081 print_error('E010: Do not on same line as %s' % operator,
82 line)
Sean Dague16dd8b32014-02-03 09:10:54 +090083
84
85def 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
Sean Dague4fb255c2013-10-14 14:07:00 -040092def check_no_trailing_whitespace(line):
93 if re.search('[ \t]+$', line):
94 print_error('E001: Trailing Whitespace', line)
95
96
97def 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
Sean Dague02d7fe12013-10-22 11:31:21 -0400106def starts_multiline(line):
107 m = re.search("[^<]<<\s*(?P<token>\w+)", line)
108 if m:
109 return m.group('token')
110 else:
111 return False
112
113
114def end_of_multiline(line, token):
115 if token:
116 return re.search("^%s\s*$" % token, line) is not None
117 return False
118
119
Sean Dagueb93ee252014-02-23 20:41:07 -0500120def check_files(files, verbose):
Sean Dague02d7fe12013-10-22 11:31:21 -0400121 in_multiline = False
Ian Wienandb8e25022014-02-21 16:14:29 +1100122 multiline_start = 0
123 multiline_line = ""
Sean Dague02d7fe12013-10-22 11:31:21 -0400124 logical_line = ""
125 token = False
Ian Wienandb8e25022014-02-21 16:14:29 +1100126 prev_file = None
127 prev_line = ""
128 prev_lineno = 0
129
Sean Dague4fb255c2013-10-14 14:07:00 -0400130 for line in fileinput.input(files):
Ian Wienandb8e25022014-02-21 16:14:29 +1100131 if fileinput.isfirstline():
132 # if in_multiline when the new file starts then we didn't
133 # find the end of a heredoc in the last file.
134 if in_multiline:
135 print_error('E012: heredoc did not end before EOF',
136 multiline_line,
137 filename=prev_file, filelineno=multiline_start)
138 in_multiline = False
139
140 # last line of a previous file should always end with a
141 # newline
142 if prev_file and not prev_line.endswith('\n'):
143 print_error('E004: file did not end with a newline',
144 prev_line,
145 filename=prev_file, filelineno=prev_lineno)
146
147 prev_file = fileinput.filename()
148
149 if verbose:
150 print "Running bash8 on %s" % fileinput.filename()
151
Sean Dague02d7fe12013-10-22 11:31:21 -0400152 # NOTE(sdague): multiline processing of heredocs is interesting
153 if not in_multiline:
154 logical_line = line
155 token = starts_multiline(line)
156 if token:
157 in_multiline = True
Ian Wienandb8e25022014-02-21 16:14:29 +1100158 multiline_start = fileinput.filelineno()
159 multiline_line = line
Sean Dague02d7fe12013-10-22 11:31:21 -0400160 continue
161 else:
162 logical_line = logical_line + line
163 if not end_of_multiline(line, token):
164 continue
165 else:
166 in_multiline = False
167
168 check_no_trailing_whitespace(logical_line)
169 check_indents(logical_line)
Sean Dague16dd8b32014-02-03 09:10:54 +0900170 check_for_do(logical_line)
171 check_if_then(logical_line)
Sean Dague4fb255c2013-10-14 14:07:00 -0400172
Ian Wienandb8e25022014-02-21 16:14:29 +1100173 prev_line = logical_line
174 prev_lineno = fileinput.filelineno()
Sean Dague4fb255c2013-10-14 14:07:00 -0400175
176def get_options():
177 parser = argparse.ArgumentParser(
178 description='A bash script style checker')
179 parser.add_argument('files', metavar='file', nargs='+',
180 help='files to scan for errors')
Sean Dague0656e122014-02-03 08:49:30 +0900181 parser.add_argument('-i', '--ignore', help='Rules to ignore')
Sean Dagueb93ee252014-02-23 20:41:07 -0500182 parser.add_argument('-v', '--verbose', action='store_true', default=False)
Sean Dague4fb255c2013-10-14 14:07:00 -0400183 return parser.parse_args()
184
185
186def main():
187 opts = get_options()
Sean Dague0656e122014-02-03 08:49:30 +0900188 register_ignores(opts.ignore)
Sean Dagueb93ee252014-02-23 20:41:07 -0500189 check_files(opts.files, opts.verbose)
Sean Dague4fb255c2013-10-14 14:07:00 -0400190
191 if ERRORS > 0:
192 print("%d bash8 error(s) found" % ERRORS)
193 return 1
194 else:
195 return 0
196
197
198if __name__ == "__main__":
199 sys.exit(main())