mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-03 10:56:52 +00:00
Add a checker for executables without shebangs
This commit is contained in:
parent
4a457a725e
commit
13991f09d2
6 changed files with 93 additions and 0 deletions
40
pre_commit_hooks/check_executables_have_shebangs.py
Normal file
40
pre_commit_hooks/check_executables_have_shebangs.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
"""Check that executable text files have a shebang."""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import pipes
|
||||
import sys
|
||||
|
||||
|
||||
def check_has_shebang(path):
|
||||
with open(path, 'rb') as f:
|
||||
first_bytes = f.read(2)
|
||||
|
||||
if first_bytes != b'#!':
|
||||
print(
|
||||
'{path}: marked executable but has no (or invalid) shebang!\n'
|
||||
" If it isn't supposed to be executable, try: chmod -x {quoted}\n"
|
||||
' If it is supposed to be executable, double-check its shebang.'.format(
|
||||
path=path,
|
||||
quoted=pipes.quote(path),
|
||||
),
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retv = 0
|
||||
|
||||
for filename in args.filenames:
|
||||
retv |= check_has_shebang(filename)
|
||||
|
||||
return retv
|
||||
Loading…
Add table
Add a link
Reference in a new issue