blob: 621696cedf911c5ca1976f4103dbeeb3a508ee86 [file] [log] [blame]
Ville Skyttä391ae302021-01-08 17:36:55 +02001"""Check that text files with a shebang are executable."""
Anthony Sottile8f615292022-01-15 19:24:05 -05002from __future__ import annotations
3
Ville Skyttä391ae302021-01-08 17:36:55 +02004import argparse
5import shlex
6import sys
Ville Skyttä391ae302021-01-08 17:36:55 +02007from typing import Sequence
Ville Skyttä391ae302021-01-08 17:36:55 +02008
9from pre_commit_hooks.check_executables_have_shebangs import EXECUTABLE_VALUES
10from pre_commit_hooks.check_executables_have_shebangs import git_ls_files
11from pre_commit_hooks.check_executables_have_shebangs import has_shebang
12
13
Anthony Sottile8f615292022-01-15 19:24:05 -050014def check_shebangs(paths: list[str]) -> int:
Ville Skyttä391ae302021-01-08 17:36:55 +020015 # Cannot optimize on non-executability here if we intend this check to
16 # work on win32 -- and that's where problems caused by non-executability
17 # (elsewhere) are most likely to arise from.
18 return _check_git_filemode(paths)
19
20
21def _check_git_filemode(paths: Sequence[str]) -> int:
Anthony Sottile8f615292022-01-15 19:24:05 -050022 seen: set[str] = set()
Ville Skyttä391ae302021-01-08 17:36:55 +020023 for ls_file in git_ls_files(paths):
24 is_executable = any(b in EXECUTABLE_VALUES for b in ls_file.mode[-3:])
25 if not is_executable and has_shebang(ls_file.filename):
26 _message(ls_file.filename)
27 seen.add(ls_file.filename)
28
29 return int(bool(seen))
30
31
32def _message(path: str) -> None:
33 print(
34 f'{path}: has a shebang but is not marked executable!\n'
35 f' If it is supposed to be executable, try: '
36 f'`chmod +x {shlex.quote(path)}`\n'
MDWbd70bc12022-05-26 17:31:24 +020037 f' If on Windows, you may also need to: '
38 f'`git add --chmod=+x {shlex.quote(path)}`\n'
Ville Skyttä391ae302021-01-08 17:36:55 +020039 f' If it not supposed to be executable, double-check its shebang '
40 f'is wanted.\n',
41 file=sys.stderr,
42 )
43
44
Anthony Sottile8f615292022-01-15 19:24:05 -050045def main(argv: Sequence[str] | None = None) -> int:
Ville Skyttä391ae302021-01-08 17:36:55 +020046 parser = argparse.ArgumentParser(description=__doc__)
47 parser.add_argument('filenames', nargs='*')
48 args = parser.parse_args(argv)
49
50 return check_shebangs(args.filenames)
51
52
53if __name__ == '__main__':
Anthony Sottile39ab2ed2021-10-23 13:23:50 -040054 raise SystemExit(main())