Skip to content

Commit

Permalink
Merge pull request #29 from antmicro/add-hashbang-check
Browse files Browse the repository at this point in the history
Check shebang in bash scripts
  • Loading branch information
mithro authored Jun 9, 2021
2 parents fab6b84 + ed56a16 commit df3714c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
38 changes: 38 additions & 0 deletions checks/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ def report_error(error_message):
return [error_message]


def shell_check_shebang(filename, header_lines):

# Need to have at least one line for the rest of the checks to fail.
while len(header_lines) < 1:
header_lines.append('')

shebang_lines = ['#!/bin/bash\n', '#! /bin/bash\n', '#!/usr/bin/env bash\n', '#! /usr/bin/env bash\n']
for shebang_line in shebang_lines:
if header_lines[0] == shebang_line:
return []

return report_file_error(
'Incorrect shebang (#!) line',
filename, 1,
shebang_lines[0], header_lines[0],
)


def python_check_shebang(filename, header_lines):
r"""Checks using correct python3 shebang line.
Expand Down Expand Up @@ -206,6 +224,23 @@ def python_check_coding(filename, header_lines):
return []


def shell_checks(pname):
"""Checks shell scripts are valid.
Checks performed:
* Has the correct shebang (`#!`) starting the file.
"""
assert isinstance(pname, pathlib.Path), (pname, type(pname))
assert pname.is_file(), (pname, pname.stat())

fdebug(pname, 'Running shell checks.')
data = read_header(pname)

errors = []
errors += shell_check_shebang(pname, data)
return errors


def python_checks(pname):
"""Checks python files are valid.
Expand Down Expand Up @@ -474,6 +509,9 @@ def main(args):
if ftype == 'Python':
ferrors += python_checks(fpath)

if ftype == 'Shell':
ferrors += shell_checks(fpath)

if ferrors:
errors[fpath] = ferrors

Expand Down
10 changes: 10 additions & 0 deletions checks/tests/shell/test-shebang-in-wrong-line.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (C) 2020 The SymbiFlow Authors.
#
# Use of this source code is governed by a ISC-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/ISC
#
# SPDX-License-Identifier: ISC

#!/bin/bash
exit 0

0 comments on commit df3714c

Please sign in to comment.