Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 1 | #!/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 Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 24 | # Basic white space errors, for consistent indenting |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 25 | # - 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 |
Ian Wienand | b8e2502 | 2014-02-21 16:14:29 +1100 | [diff] [blame] | 28 | # - E004: file did not end with a newline |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 29 | # |
| 30 | # Structure errors |
| 31 | # |
| 32 | # A set of rules that help keep things consistent in control blocks. |
| 33 | # These are ignored on long lines that have a continuation, because |
| 34 | # unrolling that is kind of "interesting" |
| 35 | # |
| 36 | # - E010: *do* not on the same line as *for* |
| 37 | # - E011: *then* not on the same line as *if* |
Ian Wienand | b8e2502 | 2014-02-21 16:14:29 +1100 | [diff] [blame] | 38 | # - E012: heredoc didn't end before EOF |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 39 | |
| 40 | import argparse |
| 41 | import fileinput |
| 42 | import re |
| 43 | import sys |
| 44 | |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 45 | ERRORS = 0 |
Sean Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame] | 46 | IGNORE = None |
| 47 | |
| 48 | |
| 49 | def register_ignores(ignores): |
| 50 | global IGNORE |
| 51 | if ignores: |
Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 52 | IGNORE = '^(' + '|'.join(ignores.split(',')) + ')' |
Sean Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def should_ignore(error): |
| 56 | return IGNORE and re.search(IGNORE, error) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 57 | |
| 58 | |
Ian Wienand | b8e2502 | 2014-02-21 16:14:29 +1100 | [diff] [blame] | 59 | def print_error(error, line, |
| 60 | filename=None, filelineno=None): |
| 61 | if not filename: |
| 62 | filename = fileinput.filename() |
| 63 | if not filelineno: |
| 64 | filelineno = fileinput.filelineno() |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 65 | global ERRORS |
| 66 | ERRORS = ERRORS + 1 |
| 67 | print("%s: '%s'" % (error, line.rstrip('\n'))) |
Ian Wienand | b8e2502 | 2014-02-21 16:14:29 +1100 | [diff] [blame] | 68 | print(" - %s: L%s" % (filename, filelineno)) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 69 | |
| 70 | |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 71 | def not_continuation(line): |
| 72 | return not re.search('\\\\$', line) |
| 73 | |
Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 74 | |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 75 | def check_for_do(line): |
| 76 | if not_continuation(line): |
Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 77 | match = re.match('^\s*(for|while|until)\s', line) |
| 78 | if match: |
| 79 | operator = match.group(1).strip() |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 80 | if not re.search(';\s*do(\b|$)', line): |
Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 81 | print_error('E010: Do not on same line as %s' % operator, |
| 82 | line) |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def check_if_then(line): |
| 86 | if not_continuation(line): |
| 87 | if re.search('^\s*if \[', line): |
| 88 | if not re.search(';\s*then(\b|$)', line): |
| 89 | print_error('E011: Then non on same line as if', line) |
| 90 | |
| 91 | |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 92 | def check_no_trailing_whitespace(line): |
| 93 | if re.search('[ \t]+$', line): |
| 94 | print_error('E001: Trailing Whitespace', line) |
| 95 | |
| 96 | |
| 97 | def check_indents(line): |
| 98 | m = re.search('^(?P<indent>[ \t]+)', line) |
| 99 | if m: |
| 100 | if re.search('\t', m.group('indent')): |
| 101 | print_error('E002: Tab indents', line) |
| 102 | if (len(m.group('indent')) % 4) != 0: |
| 103 | print_error('E003: Indent not multiple of 4', line) |
| 104 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 105 | def check_function_decl(line): |
| 106 | failed = False |
| 107 | if line.startswith("function"): |
| 108 | if not re.search('^function [\w-]* \{$', line): |
| 109 | failed = True |
| 110 | else: |
| 111 | # catch the case without "function", e.g. |
| 112 | # things like '^foo() {' |
| 113 | if re.search('^\s*?\(\)\s*?\{', line): |
| 114 | failed = True |
| 115 | |
| 116 | if failed: |
| 117 | print_error('E020: Function declaration not in format ' |
| 118 | ' "^function name {$"', line) |
| 119 | |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 120 | |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 121 | def starts_multiline(line): |
| 122 | m = re.search("[^<]<<\s*(?P<token>\w+)", line) |
| 123 | if m: |
| 124 | return m.group('token') |
| 125 | else: |
| 126 | return False |
| 127 | |
| 128 | |
| 129 | def end_of_multiline(line, token): |
| 130 | if token: |
| 131 | return re.search("^%s\s*$" % token, line) is not None |
| 132 | return False |
| 133 | |
| 134 | |
Sean Dague | b93ee25 | 2014-02-23 20:41:07 -0500 | [diff] [blame] | 135 | def check_files(files, verbose): |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 136 | in_multiline = False |
Ian Wienand | b8e2502 | 2014-02-21 16:14:29 +1100 | [diff] [blame] | 137 | multiline_start = 0 |
| 138 | multiline_line = "" |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 139 | logical_line = "" |
| 140 | token = False |
Ian Wienand | b8e2502 | 2014-02-21 16:14:29 +1100 | [diff] [blame] | 141 | prev_file = None |
| 142 | prev_line = "" |
| 143 | prev_lineno = 0 |
| 144 | |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 145 | for line in fileinput.input(files): |
Ian Wienand | b8e2502 | 2014-02-21 16:14:29 +1100 | [diff] [blame] | 146 | if fileinput.isfirstline(): |
| 147 | # if in_multiline when the new file starts then we didn't |
| 148 | # find the end of a heredoc in the last file. |
| 149 | if in_multiline: |
| 150 | print_error('E012: heredoc did not end before EOF', |
| 151 | multiline_line, |
| 152 | filename=prev_file, filelineno=multiline_start) |
| 153 | in_multiline = False |
| 154 | |
| 155 | # last line of a previous file should always end with a |
| 156 | # newline |
| 157 | if prev_file and not prev_line.endswith('\n'): |
| 158 | print_error('E004: file did not end with a newline', |
| 159 | prev_line, |
| 160 | filename=prev_file, filelineno=prev_lineno) |
| 161 | |
| 162 | prev_file = fileinput.filename() |
| 163 | |
| 164 | if verbose: |
| 165 | print "Running bash8 on %s" % fileinput.filename() |
| 166 | |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 167 | # NOTE(sdague): multiline processing of heredocs is interesting |
| 168 | if not in_multiline: |
| 169 | logical_line = line |
| 170 | token = starts_multiline(line) |
| 171 | if token: |
| 172 | in_multiline = True |
Ian Wienand | b8e2502 | 2014-02-21 16:14:29 +1100 | [diff] [blame] | 173 | multiline_start = fileinput.filelineno() |
| 174 | multiline_line = line |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 175 | continue |
| 176 | else: |
| 177 | logical_line = logical_line + line |
| 178 | if not end_of_multiline(line, token): |
| 179 | continue |
| 180 | else: |
| 181 | in_multiline = False |
| 182 | |
| 183 | check_no_trailing_whitespace(logical_line) |
| 184 | check_indents(logical_line) |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 185 | check_for_do(logical_line) |
| 186 | check_if_then(logical_line) |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 187 | check_function_decl(logical_line) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 188 | |
Ian Wienand | b8e2502 | 2014-02-21 16:14:29 +1100 | [diff] [blame] | 189 | prev_line = logical_line |
| 190 | prev_lineno = fileinput.filelineno() |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 191 | |
| 192 | def get_options(): |
| 193 | parser = argparse.ArgumentParser( |
| 194 | description='A bash script style checker') |
| 195 | parser.add_argument('files', metavar='file', nargs='+', |
| 196 | help='files to scan for errors') |
Sean Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame] | 197 | parser.add_argument('-i', '--ignore', help='Rules to ignore') |
Sean Dague | b93ee25 | 2014-02-23 20:41:07 -0500 | [diff] [blame] | 198 | parser.add_argument('-v', '--verbose', action='store_true', default=False) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 199 | return parser.parse_args() |
| 200 | |
| 201 | |
| 202 | def main(): |
| 203 | opts = get_options() |
Sean Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame] | 204 | register_ignores(opts.ignore) |
Sean Dague | b93ee25 | 2014-02-23 20:41:07 -0500 | [diff] [blame] | 205 | check_files(opts.files, opts.verbose) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 206 | |
| 207 | if ERRORS > 0: |
| 208 | print("%d bash8 error(s) found" % ERRORS) |
| 209 | return 1 |
| 210 | else: |
| 211 | return 0 |
| 212 | |
| 213 | |
| 214 | if __name__ == "__main__": |
| 215 | sys.exit(main()) |