blob: ca0abd964a923c05008ea42316bccee4ae68d2da [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
Sean Dague16dd8b32014-02-03 09:10:54 +090028#
29# Structure errors
30#
31# A set of rules that help keep things consistent in control blocks.
32# These are ignored on long lines that have a continuation, because
33# unrolling that is kind of "interesting"
34#
35# - E010: *do* not on the same line as *for*
36# - E011: *then* not on the same line as *if*
Sean Dague4fb255c2013-10-14 14:07:00 -040037
38import argparse
39import fileinput
40import re
41import sys
42
Sean Dague4fb255c2013-10-14 14:07:00 -040043ERRORS = 0
Sean Dague0656e122014-02-03 08:49:30 +090044IGNORE = None
45
46
47def register_ignores(ignores):
48 global IGNORE
49 if ignores:
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010050 IGNORE = '^(' + '|'.join(ignores.split(',')) + ')'
Sean Dague0656e122014-02-03 08:49:30 +090051
52
53def should_ignore(error):
54 return IGNORE and re.search(IGNORE, error)
Sean Dague4fb255c2013-10-14 14:07:00 -040055
56
57def print_error(error, line):
58 global ERRORS
59 ERRORS = ERRORS + 1
60 print("%s: '%s'" % (error, line.rstrip('\n')))
61 print(" - %s: L%s" % (fileinput.filename(), fileinput.filelineno()))
62
63
Sean Dague16dd8b32014-02-03 09:10:54 +090064def not_continuation(line):
65 return not re.search('\\\\$', line)
66
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010067
Sean Dague16dd8b32014-02-03 09:10:54 +090068def check_for_do(line):
69 if not_continuation(line):
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010070 match = re.match('^\s*(for|while|until)\s', line)
71 if match:
72 operator = match.group(1).strip()
Sean Dague16dd8b32014-02-03 09:10:54 +090073 if not re.search(';\s*do(\b|$)', line):
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010074 print_error('E010: Do not on same line as %s' % operator,
75 line)
Sean Dague16dd8b32014-02-03 09:10:54 +090076
77
78def check_if_then(line):
79 if not_continuation(line):
80 if re.search('^\s*if \[', line):
81 if not re.search(';\s*then(\b|$)', line):
82 print_error('E011: Then non on same line as if', line)
83
84
Sean Dague4fb255c2013-10-14 14:07:00 -040085def check_no_trailing_whitespace(line):
86 if re.search('[ \t]+$', line):
87 print_error('E001: Trailing Whitespace', line)
88
89
90def check_indents(line):
91 m = re.search('^(?P<indent>[ \t]+)', line)
92 if m:
93 if re.search('\t', m.group('indent')):
94 print_error('E002: Tab indents', line)
95 if (len(m.group('indent')) % 4) != 0:
96 print_error('E003: Indent not multiple of 4', line)
97
98
Sean Dague02d7fe12013-10-22 11:31:21 -040099def starts_multiline(line):
100 m = re.search("[^<]<<\s*(?P<token>\w+)", line)
101 if m:
102 return m.group('token')
103 else:
104 return False
105
106
107def end_of_multiline(line, token):
108 if token:
109 return re.search("^%s\s*$" % token, line) is not None
110 return False
111
112
Sean Dagueb93ee252014-02-23 20:41:07 -0500113def check_files(files, verbose):
Sean Dague02d7fe12013-10-22 11:31:21 -0400114 in_multiline = False
115 logical_line = ""
116 token = False
Sean Dague4fb255c2013-10-14 14:07:00 -0400117 for line in fileinput.input(files):
Sean Dagueb93ee252014-02-23 20:41:07 -0500118 if verbose and fileinput.isfirstline():
119 print "Running bash8 on %s" % fileinput.filename()
Sean Dague02d7fe12013-10-22 11:31:21 -0400120 # NOTE(sdague): multiline processing of heredocs is interesting
121 if not in_multiline:
122 logical_line = line
123 token = starts_multiline(line)
124 if token:
125 in_multiline = True
126 continue
127 else:
128 logical_line = logical_line + line
129 if not end_of_multiline(line, token):
130 continue
131 else:
132 in_multiline = False
133
134 check_no_trailing_whitespace(logical_line)
135 check_indents(logical_line)
Sean Dague16dd8b32014-02-03 09:10:54 +0900136 check_for_do(logical_line)
137 check_if_then(logical_line)
Sean Dague4fb255c2013-10-14 14:07:00 -0400138
139
140def get_options():
141 parser = argparse.ArgumentParser(
142 description='A bash script style checker')
143 parser.add_argument('files', metavar='file', nargs='+',
144 help='files to scan for errors')
Sean Dague0656e122014-02-03 08:49:30 +0900145 parser.add_argument('-i', '--ignore', help='Rules to ignore')
Sean Dagueb93ee252014-02-23 20:41:07 -0500146 parser.add_argument('-v', '--verbose', action='store_true', default=False)
Sean Dague4fb255c2013-10-14 14:07:00 -0400147 return parser.parse_args()
148
149
150def main():
151 opts = get_options()
Sean Dague0656e122014-02-03 08:49:30 +0900152 register_ignores(opts.ignore)
Sean Dagueb93ee252014-02-23 20:41:07 -0500153 check_files(opts.files, opts.verbose)
Sean Dague4fb255c2013-10-14 14:07:00 -0400154
155 if ERRORS > 0:
156 print("%d bash8 error(s) found" % ERRORS)
157 return 1
158 else:
159 return 0
160
161
162if __name__ == "__main__":
163 sys.exit(main())