Browse code

add bash8 tool (like pep8, but way hackier)

unlike our python code, we have no automatic style checking for
bash. For the most part, it's not a big deal, but errant whitespace
or incorrect indenting is sometimes annoying to have to -1 people's
patches for. Instead of constantly picking it up in manual review
maybe we can do better.

This is an uber hacky script which could be used to do just that.

./tools/bash8.py file1 file2 file3 ...

And it will show issues found with the files at hand. Lightly
tested in the existing devstack tree, it exposes a few issues that
we might want to think about.

This should be python 3 compatible, and includes argparse to provide
a basic '-h' support to explain how the command should be run.

Change-Id: I5009fa5852595c2953a548e430e5e1ce06ae94e0

Sean Dague authored on 2013/10/15 03:07:00
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,84 @@
0
+#!/usr/bin/env python
1
+#
2
+# Licensed under the Apache License, Version 2.0 (the "License");
3
+# you may not use this file except in compliance with the License.
4
+# You may obtain a copy of the License at
5
+#
6
+#    http://www.apache.org/licenses/LICENSE-2.0
7
+#
8
+# Unless required by applicable law or agreed to in writing, software
9
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
+# License for the specific language governing permissions and limitations
12
+# under the License.
13
+
14
+# bash8 - a pep8 equivalent for bash scripts
15
+#
16
+# this program attempts to be an automated style checker for bash scripts
17
+# to fill the same part of code review that pep8 does in most OpenStack
18
+# projects. It starts from humble beginnings, and will evolve over time.
19
+#
20
+# Currently Supported checks
21
+#
22
+# Errors
23
+# - E001: check that lines do not end with trailing whitespace
24
+# - E002: ensure that indents are only spaces, and not hard tabs
25
+# - E003: ensure all indents are a multiple of 4 spaces
26
+
27
+import argparse
28
+import fileinput
29
+import re
30
+import sys
31
+
32
+
33
+ERRORS = 0
34
+
35
+
36
+def print_error(error, line):
37
+    global ERRORS
38
+    ERRORS = ERRORS + 1
39
+    print("%s: '%s'" % (error, line.rstrip('\n')))
40
+    print(" - %s: L%s" % (fileinput.filename(), fileinput.filelineno()))
41
+
42
+
43
+def check_no_trailing_whitespace(line):
44
+    if re.search('[ \t]+$', line):
45
+        print_error('E001: Trailing Whitespace', line)
46
+
47
+
48
+def check_indents(line):
49
+    m = re.search('^(?P<indent>[ \t]+)', line)
50
+    if m:
51
+        if re.search('\t', m.group('indent')):
52
+            print_error('E002: Tab indents', line)
53
+        if (len(m.group('indent')) % 4) != 0:
54
+            print_error('E003: Indent not multiple of 4', line)
55
+
56
+
57
+def check_files(files):
58
+    for line in fileinput.input(files):
59
+        check_no_trailing_whitespace(line)
60
+        check_indents(line)
61
+
62
+
63
+def get_options():
64
+    parser = argparse.ArgumentParser(
65
+        description='A bash script style checker')
66
+    parser.add_argument('files', metavar='file', nargs='+',
67
+                        help='files to scan for errors')
68
+    return parser.parse_args()
69
+
70
+
71
+def main():
72
+    opts = get_options()
73
+    check_files(opts.files)
74
+
75
+    if ERRORS > 0:
76
+        print("%d bash8 error(s) found" % ERRORS)
77
+        return 1
78
+    else:
79
+        return 0
80
+
81
+
82
+if __name__ == "__main__":
83
+    sys.exit(main())