#!/usr/bin/env python
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# bash8 - a pep8 equivalent for bash scripts
#
# this program attempts to be an automated style checker for bash scripts
# to fill the same part of code review that pep8 does in most OpenStack
# projects. It starts from humble beginnings, and will evolve over time.
#
# Currently Supported checks
#
# Errors
# - E001: check that lines do not end with trailing whitespace
# - E002: ensure that indents are only spaces, and not hard tabs
# - E003: ensure all indents are a multiple of 4 spaces

import argparse
import fileinput
import re
import sys


ERRORS = 0


def print_error(error, line):
    global ERRORS
    ERRORS = ERRORS + 1
    print("%s: '%s'" % (error, line.rstrip('\n')))
    print(" - %s: L%s" % (fileinput.filename(), fileinput.filelineno()))


def check_no_trailing_whitespace(line):
    if re.search('[ \t]+$', line):
        print_error('E001: Trailing Whitespace', line)


def check_indents(line):
    m = re.search('^(?P<indent>[ \t]+)', line)
    if m:
        if re.search('\t', m.group('indent')):
            print_error('E002: Tab indents', line)
        if (len(m.group('indent')) % 4) != 0:
            print_error('E003: Indent not multiple of 4', line)


def check_files(files):
    for line in fileinput.input(files):
        check_no_trailing_whitespace(line)
        check_indents(line)


def get_options():
    parser = argparse.ArgumentParser(
        description='A bash script style checker')
    parser.add_argument('files', metavar='file', nargs='+',
                        help='files to scan for errors')
    return parser.parse_args()


def main():
    opts = get_options()
    check_files(opts.files)

    if ERRORS > 0:
        print("%d bash8 error(s) found" % ERRORS)
        return 1
    else:
        return 0


if __name__ == "__main__":
    sys.exit(main())

@@ -1753,67 +1753,867 @@
 def 
-check_files(files):%0A    for line in fileinput.input(files):
+starts_multiline(line):%0A    m = re.search(%22%5B%5E%3C%5D%3C%3C%5Cs*(?P%3Ctoken%3E%5Cw+)%22, line)%0A    if m:%0A        return m.group('token')%0A    else:%0A        return False%0A%0A%0Adef end_of_multiline(line, token):%0A    if token:%0A        return re.search(%22%5E%25s%5Cs*$%22 %25 token, line) is not None%0A    return False%0A%0A%0Adef check_files(files):%0A    in_multiline = False%0A    logical_line = %22%22%0A    token = False%0A    for line in fileinput.input(files):%0A        # NOTE(sdague): multiline processing of heredocs is interesting%0A        if not in_multiline:%0A            logical_line = line%0A            token = starts_multiline(line)%0A            if token:%0A                in_multiline = True%0A                continue%0A        else:%0A            logical_line = logical_line + line%0A            if not end_of_multiline(line, token):%0A                continue%0A            else:%0A                in_multiline = False%0A
 %0A   
@@ -2642,24 +2642,32 @@
 _whitespace(
+logical_
 line)%0A      
@@ -2678,24 +2678,32 @@
 eck_indents(
+logical_
 line)%0A%0A%0Adef 

add support for heredoc folding of lines