blob: 9fb51ecc9e7ee31fa5e485299a3ce5dd93fb3561 [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:
50 IGNORE='^(' + '|'.join(ignores.split(',')) + ')'
51
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
67def check_for_do(line):
68 if not_continuation(line):
69 if re.search('^\s*for ', line):
70 if not re.search(';\s*do(\b|$)', line):
71 print_error('E010: Do not on same line as for', line)
72
73
74def check_if_then(line):
75 if not_continuation(line):
76 if re.search('^\s*if \[', line):
77 if not re.search(';\s*then(\b|$)', line):
78 print_error('E011: Then non on same line as if', line)
79
80
Sean Dague4fb255c2013-10-14 14:07:00 -040081def check_no_trailing_whitespace(line):
82 if re.search('[ \t]+$', line):
83 print_error('E001: Trailing Whitespace', line)
84
85
86def check_indents(line):
87 m = re.search('^(?P<indent>[ \t]+)', line)
88 if m:
89 if re.search('\t', m.group('indent')):
90 print_error('E002: Tab indents', line)
91 if (len(m.group('indent')) % 4) != 0:
92 print_error('E003: Indent not multiple of 4', line)
93
94
Sean Dague02d7fe12013-10-22 11:31:21 -040095def starts_multiline(line):
96 m = re.search("[^<]<<\s*(?P<token>\w+)", line)
97 if m:
98 return m.group('token')
99 else:
100 return False
101
102
103def end_of_multiline(line, token):
104 if token:
105 return re.search("^%s\s*$" % token, line) is not None
106 return False
107
108
Sean Dague4fb255c2013-10-14 14:07:00 -0400109def check_files(files):
Sean Dague02d7fe12013-10-22 11:31:21 -0400110 in_multiline = False
111 logical_line = ""
112 token = False
Sean Dague4fb255c2013-10-14 14:07:00 -0400113 for line in fileinput.input(files):
Sean Dague02d7fe12013-10-22 11:31:21 -0400114 # NOTE(sdague): multiline processing of heredocs is interesting
115 if not in_multiline:
116 logical_line = line
117 token = starts_multiline(line)
118 if token:
119 in_multiline = True
120 continue
121 else:
122 logical_line = logical_line + line
123 if not end_of_multiline(line, token):
124 continue
125 else:
126 in_multiline = False
127
128 check_no_trailing_whitespace(logical_line)
129 check_indents(logical_line)
Sean Dague16dd8b32014-02-03 09:10:54 +0900130 check_for_do(logical_line)
131 check_if_then(logical_line)
Sean Dague4fb255c2013-10-14 14:07:00 -0400132
133
134def get_options():
135 parser = argparse.ArgumentParser(
136 description='A bash script style checker')
137 parser.add_argument('files', metavar='file', nargs='+',
138 help='files to scan for errors')
Sean Dague0656e122014-02-03 08:49:30 +0900139 parser.add_argument('-i', '--ignore', help='Rules to ignore')
Sean Dague4fb255c2013-10-14 14:07:00 -0400140 return parser.parse_args()
141
142
143def main():
144 opts = get_options()
Sean Dague0656e122014-02-03 08:49:30 +0900145 register_ignores(opts.ignore)
Sean Dague4fb255c2013-10-14 14:07:00 -0400146 check_files(opts.files)
147
148 if ERRORS > 0:
149 print("%d bash8 error(s) found" % ERRORS)
150 return 1
151 else:
152 return 0
153
154
155if __name__ == "__main__":
156 sys.exit(main())