Repository: openstack/bashate Branch: master Commit: ec2f24009152 Files: 68 Total size: 78.7 KB Directory structure: gitextract_das1educ/ ├── .coveragerc ├── .gitignore ├── .gitreview ├── .mailmap ├── .pre-commit-hooks.yaml ├── .stestr.conf ├── .zuul.d/ │ └── jobs.yaml ├── CONTRIBUTING.rst ├── HACKING.rst ├── LICENSE ├── README.rst ├── bashate/ │ ├── __init__.py │ ├── __main__.py │ ├── bashate.py │ ├── messages.py │ └── tests/ │ ├── __init__.py │ ├── base.py │ ├── samples/ │ │ ├── .E005_excluded │ │ ├── E001_bad.sh │ │ ├── E002_bad.sh │ │ ├── E003_bad.sh │ │ ├── E003_good.sh │ │ ├── E004_bad.sh │ │ ├── E005_bad │ │ ├── E006_bad.sh │ │ ├── E006_bad_custom_length.sh │ │ ├── E006_good.sh │ │ ├── E010_bad.sh │ │ ├── E010_good.sh │ │ ├── E011_bad.sh │ │ ├── E011_good.sh │ │ ├── E012_bad.sh │ │ ├── E040_syntax_error.sh │ │ ├── E041_bad.sh │ │ ├── E042_bad.sh │ │ ├── E043_bad.sh │ │ ├── E044_bad.sh │ │ ├── comments.sh │ │ ├── for_loops.sh │ │ ├── heredoc_ignore.sh │ │ └── legacy_sample.sh │ └── test_bashate.py ├── bindep.txt ├── doc/ │ ├── requirements.txt │ └── source/ │ ├── conf.py │ ├── contributor/ │ │ ├── contributing.rst │ │ └── index.rst │ ├── index.rst │ ├── install/ │ │ └── index.rst │ ├── man/ │ │ └── bashate.rst │ ├── readme.rst │ └── user/ │ ├── index.rst │ └── usage.rst ├── playbooks/ │ └── bashate-devstack-master/ │ └── run.yaml ├── releasenotes/ │ ├── notes/ │ │ ├── 0-dash-6-748e729ee001df73.yaml │ │ ├── adopt-pycodestyle-output-format-f4d6e35dadfcb6f9.yaml │ │ ├── custom-line-length-083e8077951a8ead.yaml │ │ ├── drop-python2.7-064c5e5bcfb4539c.yaml │ │ ├── heredoc-ignore-905b29053652f90e.yaml │ │ ├── python-3-4e30b9f2b9e2dcb7.yaml │ │ └── start-using-reno-eaaafddb3fbf2010.yaml │ └── source/ │ ├── conf.py │ └── index.rst ├── requirements.txt ├── setup.cfg ├── setup.py ├── test-requirements.txt └── tox.ini ================================================ FILE CONTENTS ================================================ ================================================ FILE: .coveragerc ================================================ [run] branch = True source = bashate omit = bashate/tests/* [report] ignore_errors = True ================================================ FILE: .gitignore ================================================ *.py[cod] # C extensions *.so # Packages *.egg *.egg-info dist build eggs parts bin var sdist develop-eggs .installed.cfg lib lib64 # Installer logs pip-log.txt # Unit test / coverage reports .coverage .tox nosetests.xml .stestr/ cover/ # Translations *.mo # Mr Developer .mr.developer.cfg .project .pydevproject # Complexity output/*.html output/*/index.html # Sphinx doc/build # pbr generates these AUTHORS ChangeLog # Editors *~ .*.swp ================================================ FILE: .gitreview ================================================ [gerrit] host=review.opendev.org port=29418 project=openstack/bashate.git ================================================ FILE: .mailmap ================================================ # Format is: # # ================================================ FILE: .pre-commit-hooks.yaml ================================================ --- # For use with pre-commit. # See usage instructions at https://pre-commit.com - id: bashate name: bashate description: This hook runs bashate for linting shell scripts entry: bashate language: python types: [shell] ================================================ FILE: .stestr.conf ================================================ [DEFAULT] test_path=./bashate/tests ================================================ FILE: .zuul.d/jobs.yaml ================================================ - project: templates: - openstack-python3-jobs - publish-openstack-docs-pti - release-notes-jobs-python3 check: jobs: - openstack-tox-pep8 - openstack-tox-py39 - openstack-tox-py310 - openstack-tox-py311 - openstack-tox-py312 - bashate-devstack gate: jobs: - openstack-tox-pep8 - openstack-tox-py39 - openstack-tox-py310 - openstack-tox-py311 - openstack-tox-py312 - bashate-devstack - job: name: bashate-devstack run: playbooks/bashate-devstack-master/run.yaml required-projects: - openstack/devstack ================================================ FILE: CONTRIBUTING.rst ================================================ The source repository for this project can be found at: https://opendev.org/openstack/bashate Pull requests submitted through GitHub are not monitored. To start contributing to OpenStack, follow the steps in the contribution guide to set up and use Gerrit: https://docs.openstack.org/contributors/code-and-documentation/quick-start.html Bugs should be filed on Launchpad: https://bugs.launchpad.net/bash8 For more specific information about contributing to this repository, see the Bashate contributor guide: https://docs.openstack.org/bashate/latest/contributor/contributing.html ================================================ FILE: HACKING.rst ================================================ bashate Style Commandments =============================================== Read the OpenStack Style Commandments http://docs.openstack.org/developer/hacking/ ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. ================================================ FILE: README.rst ================================================ =============================== bashate =============================== 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 started from humble beginnings in the DevStack project, and will continue to evolve over time. The output format aims to follow `pycodestyle (pep8) default output format `_. - Free software: Apache license - Documentation: https://docs.openstack.org/bashate - Source: https://opendev.org/openstack/bashate/ - Bugs: https://bugs.launchpad.net/bash8 - Release notes: https://docs.openstack.org/releasenotes/bashate/index.html - Contributing: https://docs.openstack.org/bashate/latest/contributor/index.html Currently Supported Checks -------------------------- Errors ~~~~~~ Basic white space errors, for consistent indenting - 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 - E004: file did not end with a newline - E005: file does not begin with #! or have a .sh prefix - E006: check for lines longer than 79 columns Structure Errors ~~~~~~~~~~~~~~~~ A set of rules that help keep things consistent in control blocks. These are ignored on long lines that have a continuation, because unrolling that is kind of "interesting" - E010: *do* not on the same line as *for* - E011: *then* not on the same line as *if* or *elif* - E012: heredoc didn't end before EOF - E020: Function declaration not in format ``^function name {$`` Obsolete, deprecated or unsafe syntax ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Rules to identify obsolete, deprecated or unsafe syntax that should not be used - E040: Syntax errors reported by `bash -n` - E041: Usage of $[ for arithmetic is deprecated for $(( - E042: local declaration hides errors - E043: arithmetic compound has inconsistent return semantics - E044: Use [[ for =~,<,> comparisions ================================================ FILE: bashate/__init__.py ================================================ # 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. import pbr.version __version__ = pbr.version.VersionInfo( 'bashate').version_string_with_vcs() ================================================ FILE: bashate/__main__.py ================================================ from bashate import main import sys if __name__ == "__main__": sys.exit(main()) ================================================ FILE: bashate/bashate.py ================================================ #!/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. import argparse import fileinput import os import re import shlex import subprocess import sys import bashate from bashate import messages MESSAGES = messages.MESSAGES def is_continuation(line): return re.search(r'\\\s*$', line) def check_for_do(line, report): if not is_continuation(line): match = re.match(r'^\s*(for|while|until)\s', line) if match: operator = match.group(1).strip() if operator == "for": # "for i in ..." and "for ((" is bash, but # "for (" is likely from an embedded awk script, # so skip it if re.search(r'for \([^\(]', line): return if not re.search(r';\s*do$', line): report.print_error((MESSAGES['E010'].msg % operator), line) def check_if_then(line, report): if not is_continuation(line): if re.search(r'^\s*(el)?if \[', line): if not re.search(r';\s*then$', line): report.print_error(MESSAGES['E011'].msg, line) def check_no_trailing_whitespace(line, report): if re.search(r'[ \t]+$', line): report.print_error(MESSAGES['E001'].msg, line) def check_no_long_lines(line, report, max_line_length): if len(line.rstrip("\r\n")) > max_line_length: report.print_error(MESSAGES['E006'].msg, line) def check_indents(logical_line, report): # this is rather complex to handle argument offset indenting; # primarily done by emacs. If there is an argument, it will try # to line up the following arguments underneath it, e.g. # foobar_cmd bar baz \ # moo boo # Thus the offset in this case might not be a strict multiple of 4 # Find the offset of the first argument of the command (if it has # one) m = re.search(r'^(?P[ \t]+)?(?P\S+)(?P\s+)(?P\S+)', logical_line[0]) arg_offset = None if m: arg_offset = len(m.group('indent')) if m.group('indent') else 0 arg_offset += len(m.group('cmd')) + len(m.group('ws')) # go through each line for lineno, line in enumerate(logical_line): m = re.search(r'^(?P[ \t]+)', line) if m: # no tabs, only spaces if re.search(r'\t', m.group('indent')): report.print_error(MESSAGES['E002'].msg, line) offset = len(m.group('indent')) # the first line and lines without an argument should be # offset by 4 spaces if (lineno == 0) or (arg_offset is None): if (offset % 4) != 0: report.print_error(MESSAGES['E003'].msg, line) else: # other lines are allowed to line up with the first # argument, or be multiple-of 4 spaces if offset != arg_offset and (offset % 4) != 0: report.print_error(MESSAGES['E003'].msg, line) def check_function_decl(line, report): failed = False if line.startswith("function"): if not re.search(r'^function [\w-]* \{$', line): failed = True else: # catch the case without "function", e.g. # things like '^foo() {' if re.search(r'^\s*?\(\)\s*?\{', line): failed = True if failed: report.print_error(MESSAGES['E020'].msg, line) def starts_heredoc(line): # note, watch out for <\w+)([\'\"]?)", line) return m.group('token') if m else False def end_of_heredoc(line, token): return token and re.search(r"^%s\s*$" % token, line) def check_arithmetic(line, report): if "$[" in line: report.print_error(MESSAGES['E041'].msg, line) def check_bare_arithmetic(line, report): if line.lstrip().startswith("(("): report.print_error(MESSAGES['E043'].msg, line) def check_local_subshell(line, report): # XXX: should we increase the string checking to see if the $( is # anywhere with a string being set? Risk of false positives?x if line.lstrip().startswith('local ') and \ any(s in line for s in ('=$(', '=`', '="$(', '="`')): report.print_error(MESSAGES['E042'].msg, line) def check_hashbang(line, filename, report): # this check only runs on the first line # maybe this should check for shell? if (not filename.endswith(".sh") and not line.startswith("#!") and not os.path.basename(filename).startswith('.')): report.print_error(MESSAGES['E005'].msg, line) def check_conditional_expression(line, report): # We're really starting to push the limits of what we can do without # a complete bash syntax parser here. For example # > [[ $foo =~ " [ " ]] && [[ $bar =~ " ] " ]] # would be valid but mess up a simple regex matcher for "[.*]". # Let alone dealing with multiple-line-spanning etc... # # So we'll KISS and just look for simple, one line, # > if [ $foo =~ "bar" ]; then # type statements, which are the vast majority of typo errors. # # shlex is pretty helpful in getting us something we can walk to # find this pattern. It does however have issues with # unterminated quotes on multi-line strings (e.g.) # # foo="bar <-- we only see this bit in "line" # baz" # # So we're just going to ignore parser failures here and move on. # Possibly in the future we could pull such multi-line strings # into "logical_line" below, and pass that here and have shlex # break that up. try: toks = shlex.shlex(line) toks.wordchars = "[]=~" toks = list(toks) except ValueError: return in_single_bracket = False for tok in toks: if tok == '[': in_single_bracket = True elif tok in ('=~', '<', '>') and in_single_bracket: report.print_error(MESSAGES['E044'].msg, line) elif tok == ']': in_single_bracket = False def check_syntax(filename, report): # run the file through "bash -n" to catch basic syntax errors and # other warnings matches = [] # sample lines we want to match: # foo.sh: line 4: warning: \ # here-document at line 1 delimited by end-of-file (wanted `EOF') # foo.sh: line 9: syntax error: unexpected end of file # foo.sh: line 7: syntax error near unexpected token `}' # # i.e. consistency with ":"'s isn't constant, so just do our # best... r = re.compile( '^(?P.*): line (?P[0-9]+): (?P.*)') # we are parsing the error message, so force it to ignore the # system locale so we don't get messages in another language bash_environment = os.environ bash_environment['LC_ALL'] = 'C' proc = subprocess.Popen( ['bash', '-n', filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=bash_environment, universal_newlines=True) outputs = proc.communicate() for line in outputs[1].split('\n'): m = r.match(line) if m: matches.append(m) for m in matches: if 'syntax error' in m.group('error'): msg = '%s: %s' % (MESSAGES['E040'].msg, m.group('error')) report.print_error(msg, filename=filename, filelineno=int(m.group('lineno'))) # Matching output from bash warning about here-documents not # ending. # FIXME: are there other warnings that might come out # with "bash -n"? A quick scan of the source code suggests # no, but there might be other interesting things we could # catch. if 'warning:' in m.group('error'): if 'delimited by end-of-file' in m.group('error'): start = re.match('^.*line (?P[0-9]+).*$', m.group('error')) report.print_error( MESSAGES['E012'].msg % int(start.group('start')), filename=filename, filelineno=int(m.group('lineno'))) class BashateRun(object): def __init__(self): self.error_count = 0 self.error_list = None self.ignore_list = None self.warning_count = 0 self.warning_list = None def register_ignores(self, ignores): if ignores: self.ignore_list = '^(' + '|'.join(ignores.split(',')) + ')' def register_warnings(self, warnings): if warnings: self.warning_list = '^(' + '|'.join(warnings.split(',')) + ')' def register_errors(self, errors): if errors: self.error_list = '^(' + '|'.join(errors.split(',')) + ')' def should_ignore(self, error): return self.ignore_list and re.search(self.ignore_list, error) def should_warn(self, error): # if in the errors list, overrides warning level if self.error_list and re.search(self.error_list, error): return False if messages.is_default_warning(error): return True return self.warning_list and re.search(self.warning_list, error) def print_error(self, error, line='', filename=None, filelineno=None): if self.should_ignore(error): return warn = self.should_warn(error) if not filename: filename = fileinput.filename() if not filelineno: filelineno = fileinput.filelineno() if warn: self.warning_count = self.warning_count + 1 else: self.error_count = self.error_count + 1 self.log_error(error, line, filename, filelineno, warn) def log_error(self, error, line, filename, filelineno, warn=False): # following pycodestyle/pep8 default output format # https://github.com/PyCQA/pycodestyle/blob/master/pycodestyle.py#L108 print("%(filename)s:%(filelineno)s:1: %(error)s" % {'filename': filename, 'filelineno': filelineno, 'error': error.replace(":", "", 1)}) def check_files(self, files, verbose, max_line_length=79): logical_line = "" token = False # NOTE(mrodden): magic; replace with proper # report class when necessary report = self for fname in files: # reset world in_heredoc = False in_continuation = False # simple syntax checking, as files can pass style but still cause # syntax errors when you try to run them. check_syntax(fname, report) for line in fileinput.input(fname): if fileinput.isfirstline(): check_hashbang(line, fileinput.filename(), report) if verbose: print("Running bashate on %s" % fileinput.filename()) # Don't run any tests on comment lines (but remember # inside a heredoc this might be part of the syntax of # an embedded script, just ignore that) if line.lstrip().startswith('#') and not in_heredoc: logical_line = [line] check_indents(logical_line, report) continue # Strip trailing comments. From bash: # # a word beginning with # causes that word and all # remaining characters on that line to be ignored. # ... # A character that, when unquoted, separates # words. One of the following: | & ; ( ) < > space # tab # # for simplicity, we strip inline comments by # matching just '#'. if not in_heredoc: ll_split = line.split(' #', 1) if len(ll_split) > 1: line = ll_split[0].rstrip() # see if this starts a heredoc if not in_heredoc: token = starts_heredoc(line) if token: in_heredoc = True logical_line = [line] continue # see if this starts a continuation if not in_continuation: if is_continuation(line): in_continuation = True logical_line = [line] continue # if we are in a heredoc or continuation, just loop # back and keep buffering the lines into # "logical_line" until the end of the # heredoc/continuation. if in_heredoc: logical_line.append(line) if not end_of_heredoc(line, token): continue else: in_heredoc = False # FIXME: if we want to do something with # heredocs in the future, then the whole thing # is now stored in logical_line. for now, # skip continue elif in_continuation: logical_line.append(line) if is_continuation(line): continue else: in_continuation = False else: logical_line = [line] check_indents(logical_line, report) # at this point, logical_line is an array that holds # the whole continuation. XXX : historically, we've # just handled every line in a continuation # separatley. Stick with what works... for line in logical_line: check_no_trailing_whitespace(line, report) check_no_long_lines(line, report, max_line_length) check_for_do(line, report) check_if_then(line, report) check_function_decl(line, report) check_arithmetic(line, report) check_local_subshell(line, report) check_bare_arithmetic(line, report) check_conditional_expression(line, report) # finished processing the file # last line should always end with a newline if not line.endswith('\n'): report.print_error(MESSAGES['E004'].msg, line) def main(args=None): if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser( description='A bash script style checker') parser.add_argument('files', metavar='file', nargs='*', help='files to scan for errors') parser.add_argument('-i', '--ignore', help='Rules to ignore') parser.add_argument('-w', '--warn', help='Rules to always warn (rather than error)') parser.add_argument('-e', '--error', help='Rules to always error (rather than warn)') parser.add_argument('--max-line-length', default=79, type=int, help='Max line length') parser.add_argument('-v', '--verbose', action='store_true', default=False) parser.add_argument('--version', action='store_true', help='show bashate version number and exit', default=False) parser.add_argument('-s', '--show', action='store_true', default=False) opts = parser.parse_args(args) if opts.version: print("bashate: %s" % bashate.__version__) sys.exit(0) if opts.show: messages.print_messages() sys.exit(0) files = opts.files if not files: parser.print_usage() return 1 run = BashateRun() run.register_ignores(opts.ignore) run.register_warnings(opts.warn) run.register_errors(opts.error) try: run.check_files(files, opts.verbose, opts.max_line_length) except IOError as e: print("bashate: %s" % e) return 1 if run.warning_count > 0: print("%d bashate warning(s) found" % run.warning_count) if run.error_count > 0: print("%d bashate error(s) found" % run.error_count) return 1 return 0 if __name__ == "__main__": sys.exit(main()) ================================================ FILE: bashate/messages.py ================================================ # 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. import re import textwrap class _Message(object): """An individual bashate message. This should be accessed via the MESSAGES dict keyed by msg_id, e.g. from bashate.messages import MESSAGES print(MESSAGES['E123'].msg) :param msg_id: The unique message id (E...) :param msg_str: The short message string, as displayed in program output :param long_msg: A longer more involved message, designed for documentation """ def __init__(self, msg_id, msg_str, long_msg, default): self.msg_id = msg_id self.msg_str = msg_str # clean-up from """ to a plain string if long_msg: self.long_msg = textwrap.dedent(long_msg) self.long_msg = self.long_msg.strip() else: self.long_msg = None self.default = default @property def msg(self): # For historical reasons, the code relies on "id: msg" so build # that up as .msg property for quick access. return "%s: %s" % (self.msg_id, self.msg_str) _messages = { 'E001': { 'msg': 'Trailing Whitespace', 'long_msg': None, 'default': 'E' }, 'E002': { 'msg': 'Tab indents', 'long_msg': """ Spaces are preferred to tabs in source files. """, 'default': 'E' }, 'E003': { 'msg': 'Indent not multiple of 4', 'long_msg': """ Four spaces should be used to offset logical blocks. """, 'default': 'E' }, 'E004': { 'msg': 'File did not end with a newline', 'long_msg': """ It is conventional to have a single newline ending files. """, 'default': 'E' }, 'E005': { 'msg': 'File does not begin with #! or have .sh prefix', 'long_msg': """ This can be useful for tools that use either the interpreter directive or the file-exension to select highlighting mode, syntax mode or determine MIME-type, such as file, gerrit and editors. """, 'default': 'W' }, 'E006': { 'msg': 'Line too long', 'long_msg': """ This check mimics the widely accepted convention from PEP8 and many other places that lines longer than a standard terminal width (default=79 columns) can not only cause problems when reading/writing code, but also often indicates a bad smell, e.g. too many levels of indentation due to overly complex functions which require refactoring into smaller chunks. """, 'default': 'W' }, 'E010': { 'msg': 'The "do" should be on same line as %s', 'long_msg': """ Ensure consistency of "do" directive being on the same line as it's command. For example: for i in $(seq 1 100); do echo "hi" done will trigger this error """, 'default': 'E' }, 'E011': { 'msg': 'Then keyword is not on same line as if or elif keyword', 'long_msg': """ Similar to E010, this ensures consistency of if/elif statements """, 'default': 'E' }, 'E012': { 'msg': 'here-document at line %d delimited by end-of-file', 'long_msg': """ This check ensures the closure of heredocs (<