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 |
| 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 | |
| 28 | import argparse |
| 29 | import fileinput |
| 30 | import re |
| 31 | import sys |
| 32 | |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 33 | ERRORS = 0 |
Sean Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame^] | 34 | IGNORE = None |
| 35 | |
| 36 | |
| 37 | def register_ignores(ignores): |
| 38 | global IGNORE |
| 39 | if ignores: |
| 40 | IGNORE='^(' + '|'.join(ignores.split(',')) + ')' |
| 41 | |
| 42 | |
| 43 | def should_ignore(error): |
| 44 | return IGNORE and re.search(IGNORE, error) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def 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 | |
| 54 | def check_no_trailing_whitespace(line): |
| 55 | if re.search('[ \t]+$', line): |
| 56 | print_error('E001: Trailing Whitespace', line) |
| 57 | |
| 58 | |
| 59 | def 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 Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 68 | def 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 | |
| 76 | def 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 Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 82 | def check_files(files): |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 83 | in_multiline = False |
| 84 | logical_line = "" |
| 85 | token = False |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 86 | for line in fileinput.input(files): |
Sean Dague | 02d7fe1 | 2013-10-22 11:31:21 -0400 | [diff] [blame] | 87 | # 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 Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def 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 Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame^] | 110 | parser.add_argument('-i', '--ignore', help='Rules to ignore') |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 111 | return parser.parse_args() |
| 112 | |
| 113 | |
| 114 | def main(): |
| 115 | opts = get_options() |
Sean Dague | 0656e12 | 2014-02-03 08:49:30 +0900 | [diff] [blame^] | 116 | register_ignores(opts.ignore) |
Sean Dague | 4fb255c | 2013-10-14 14:07:00 -0400 | [diff] [blame] | 117 | 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 | |
| 126 | if __name__ == "__main__": |
| 127 | sys.exit(main()) |