blob: edf7da4645931ce634e4bf4c71ffed509a57e4e6 [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
33
34ERRORS = 0
35
36
37def print_error(error, line):
38 global ERRORS
39 ERRORS = ERRORS + 1
40 print("%s: '%s'" % (error, line.rstrip('\n')))
41 print(" - %s: L%s" % (fileinput.filename(), fileinput.filelineno()))
42
43
44def check_no_trailing_whitespace(line):
45 if re.search('[ \t]+$', line):
46 print_error('E001: Trailing Whitespace', line)
47
48
49def check_indents(line):
50 m = re.search('^(?P<indent>[ \t]+)', line)
51 if m:
52 if re.search('\t', m.group('indent')):
53 print_error('E002: Tab indents', line)
54 if (len(m.group('indent')) % 4) != 0:
55 print_error('E003: Indent not multiple of 4', line)
56
57
Sean Dague02d7fe12013-10-22 11:31:21 -040058def starts_multiline(line):
59 m = re.search("[^<]<<\s*(?P<token>\w+)", line)
60 if m:
61 return m.group('token')
62 else:
63 return False
64
65
66def end_of_multiline(line, token):
67 if token:
68 return re.search("^%s\s*$" % token, line) is not None
69 return False
70
71
Sean Dague4fb255c2013-10-14 14:07:00 -040072def check_files(files):
Sean Dague02d7fe12013-10-22 11:31:21 -040073 in_multiline = False
74 logical_line = ""
75 token = False
Sean Dague4fb255c2013-10-14 14:07:00 -040076 for line in fileinput.input(files):
Sean Dague02d7fe12013-10-22 11:31:21 -040077 # NOTE(sdague): multiline processing of heredocs is interesting
78 if not in_multiline:
79 logical_line = line
80 token = starts_multiline(line)
81 if token:
82 in_multiline = True
83 continue
84 else:
85 logical_line = logical_line + line
86 if not end_of_multiline(line, token):
87 continue
88 else:
89 in_multiline = False
90
91 check_no_trailing_whitespace(logical_line)
92 check_indents(logical_line)
Sean Dague4fb255c2013-10-14 14:07:00 -040093
94
95def get_options():
96 parser = argparse.ArgumentParser(
97 description='A bash script style checker')
98 parser.add_argument('files', metavar='file', nargs='+',
99 help='files to scan for errors')
100 return parser.parse_args()
101
102
103def main():
104 opts = get_options()
105 check_files(opts.files)
106
107 if ERRORS > 0:
108 print("%d bash8 error(s) found" % ERRORS)
109 return 1
110 else:
111 return 0
112
113
114if __name__ == "__main__":
115 sys.exit(main())