blob: 7552e0d642f742860b115ad257d9fe009b5cbb8f [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 Dague4fb255c2013-10-14 14:07:00 -0400113def check_files(files):
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 Dague02d7fe12013-10-22 11:31:21 -0400118 # NOTE(sdague): multiline processing of heredocs is interesting
119 if not in_multiline:
120 logical_line = line
121 token = starts_multiline(line)
122 if token:
123 in_multiline = True
124 continue
125 else:
126 logical_line = logical_line + line
127 if not end_of_multiline(line, token):
128 continue
129 else:
130 in_multiline = False
131
132 check_no_trailing_whitespace(logical_line)
133 check_indents(logical_line)
Sean Dague16dd8b32014-02-03 09:10:54 +0900134 check_for_do(logical_line)
135 check_if_then(logical_line)
Sean Dague4fb255c2013-10-14 14:07:00 -0400136
137
138def get_options():
139 parser = argparse.ArgumentParser(
140 description='A bash script style checker')
141 parser.add_argument('files', metavar='file', nargs='+',
142 help='files to scan for errors')
Sean Dague0656e122014-02-03 08:49:30 +0900143 parser.add_argument('-i', '--ignore', help='Rules to ignore')
Sean Dague4fb255c2013-10-14 14:07:00 -0400144 return parser.parse_args()
145
146
147def main():
148 opts = get_options()
Sean Dague0656e122014-02-03 08:49:30 +0900149 register_ignores(opts.ignore)
Sean Dague4fb255c2013-10-14 14:07:00 -0400150 check_files(opts.files)
151
152 if ERRORS > 0:
153 print("%d bash8 error(s) found" % ERRORS)
154 return 1
155 else:
156 return 0
157
158
159if __name__ == "__main__":
160 sys.exit(main())