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: |
| 50 | IGNORE='^(' + '|'.join(ignores.split(',')) + ')' |
| 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 | |
| 67 | def 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 | |
| 74 | def 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 Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 81 | def check_no_trailing_whitespace(line): |
| 82 | if re.search('[ \t]+$', line): |
| 83 | print_error('E001: Trailing Whitespace', line) |
| 84 | |
| 85 | |
| 86 | def 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 Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 95 | def 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 | |
| 103 | def 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 Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 109 | def check_files(files): |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 110 | in_multiline = False |
| 111 | logical_line = "" |
| 112 | token = False |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 113 | for line in fileinput.input(files): |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 114 | # 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 Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame^] | 130 | check_for_do(logical_line) |
| 131 | check_if_then(logical_line) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 132 | |
| 133 | |
| 134 | def 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 Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame] | 139 | parser.add_argument('-i', '--ignore', help='Rules to ignore') |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 140 | return parser.parse_args() |
| 141 | |
| 142 | |
| 143 | def main(): |
| 144 | opts = get_options() |
Sean Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame] | 145 | register_ignores(opts.ignore) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 146 | 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 | |
| 155 | if __name__ == "__main__": |
| 156 | sys.exit(main()) |