blob: 2623358182beba42b9da14b91910b23877d284d8 [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
24# - E001: check that lines do not end with trailing whitespace
25# - E002: ensure that indents are only spaces, and not hard tabs
26# - E003: ensure all indents are a multiple of 4 spaces
27
28import argparse
29import fileinput
30import re
31import sys
32
Sean Dague4fb255c2013-10-14 14:07:00 -040033ERRORS = 0
Sean Dague0656e122014-02-03 08:49:30 +090034IGNORE = None
35
36
37def register_ignores(ignores):
38 global IGNORE
39 if ignores:
40 IGNORE='^(' + '|'.join(ignores.split(',')) + ')'
41
42
43def should_ignore(error):
44 return IGNORE and re.search(IGNORE, error)
Sean Dague4fb255c2013-10-14 14:07:00 -040045
46
47def print_error(error, line):
48 global ERRORS
49 ERRORS = ERRORS + 1
50 print("%s: '%s'" % (error, line.rstrip('\n')))
51 print(" - %s: L%s" % (fileinput.filename(), fileinput.filelineno()))
52
53
54def check_no_trailing_whitespace(line):
55 if re.search('[ \t]+$', line):
56 print_error('E001: Trailing Whitespace', line)
57
58
59def check_indents(line):
60 m = re.search('^(?P<indent>[ \t]+)', line)
61 if m:
62 if re.search('\t', m.group('indent')):
63 print_error('E002: Tab indents', line)
64 if (len(m.group('indent')) % 4) != 0:
65 print_error('E003: Indent not multiple of 4', line)
66
67
Sean Dague02d7fe12013-10-22 11:31:21 -040068def starts_multiline(line):
69 m = re.search("[^<]<<\s*(?P<token>\w+)", line)
70 if m:
71 return m.group('token')
72 else:
73 return False
74
75
76def end_of_multiline(line, token):
77 if token:
78 return re.search("^%s\s*$" % token, line) is not None
79 return False
80
81
Sean Dague4fb255c2013-10-14 14:07:00 -040082def check_files(files):
Sean Dague02d7fe12013-10-22 11:31:21 -040083 in_multiline = False
84 logical_line = ""
85 token = False
Sean Dague4fb255c2013-10-14 14:07:00 -040086 for line in fileinput.input(files):
Sean Dague02d7fe12013-10-22 11:31:21 -040087 # NOTE(sdague): multiline processing of heredocs is interesting
88 if not in_multiline:
89 logical_line = line
90 token = starts_multiline(line)
91 if token:
92 in_multiline = True
93 continue
94 else:
95 logical_line = logical_line + line
96 if not end_of_multiline(line, token):
97 continue
98 else:
99 in_multiline = False
100
101 check_no_trailing_whitespace(logical_line)
102 check_indents(logical_line)
Sean Dague4fb255c2013-10-14 14:07:00 -0400103
104
105def get_options():
106 parser = argparse.ArgumentParser(
107 description='A bash script style checker')
108 parser.add_argument('files', metavar='file', nargs='+',
109 help='files to scan for errors')
Sean Dague0656e122014-02-03 08:49:30 +0900110 parser.add_argument('-i', '--ignore', help='Rules to ignore')
Sean Dague4fb255c2013-10-14 14:07:00 -0400111 return parser.parse_args()
112
113
114def main():
115 opts = get_options()
Sean Dague0656e122014-02-03 08:49:30 +0900116 register_ignores(opts.ignore)
Sean Dague4fb255c2013-10-14 14:07:00 -0400117 check_files(opts.files)
118
119 if ERRORS > 0:
120 print("%d bash8 error(s) found" % ERRORS)
121 return 1
122 else:
123 return 0
124
125
126if __name__ == "__main__":
127 sys.exit(main())