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 |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 28 | # |
| 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 Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 37 | |
| 38 | import argparse |
| 39 | import fileinput |
| 40 | import re |
| 41 | import sys |
| 42 | |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 43 | ERRORS = 0 |
Sean Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame] | 44 | IGNORE = None |
| 45 | |
| 46 | |
| 47 | def register_ignores(ignores): |
| 48 | global IGNORE |
| 49 | if ignores: |
Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 50 | IGNORE = '^(' + '|'.join(ignores.split(',')) + ')' |
Sean Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame] | 51 | |
| 52 | |
| 53 | def should_ignore(error): |
| 54 | return IGNORE and re.search(IGNORE, error) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 55 | |
| 56 | |
| 57 | def 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 Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 64 | def not_continuation(line): |
| 65 | return not re.search('\\\\$', line) |
| 66 | |
Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 67 | |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 68 | def check_for_do(line): |
| 69 | if not_continuation(line): |
Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 70 | match = re.match('^\s*(for|while|until)\s', line) |
| 71 | if match: |
| 72 | operator = match.group(1).strip() |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 73 | if not re.search(';\s*do(\b|$)', line): |
Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 74 | print_error('E010: Do not on same line as %s' % operator, |
| 75 | line) |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def 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 Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 85 | def check_no_trailing_whitespace(line): |
| 86 | if re.search('[ \t]+$', line): |
| 87 | print_error('E001: Trailing Whitespace', line) |
| 88 | |
| 89 | |
| 90 | def 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 Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 99 | def 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 | |
| 107 | def 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 Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 113 | def check_files(files): |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 114 | in_multiline = False |
| 115 | logical_line = "" |
| 116 | token = False |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 117 | for line in fileinput.input(files): |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 118 | # 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 Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 134 | check_for_do(logical_line) |
| 135 | check_if_then(logical_line) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 136 | |
| 137 | |
| 138 | def 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 Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame] | 143 | parser.add_argument('-i', '--ignore', help='Rules to ignore') |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 144 | return parser.parse_args() |
| 145 | |
| 146 | |
| 147 | def main(): |
| 148 | opts = get_options() |
Sean Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame] | 149 | register_ignores(opts.ignore) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 150 | 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 | |
| 159 | if __name__ == "__main__": |
| 160 | sys.exit(main()) |