Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check shebang in bash scripts #29

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]

wtatarski marked this conversation as resolved.
Show resolved Hide resolved

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