precommit: Add check for Python shebang

This commit is contained in:
Ole Schütt 2021-11-24 18:09:55 +01:00 committed by Ole Schütt
parent 6c3d14b30c
commit 76e1f5a4cb
5 changed files with 11 additions and 4 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# vim: set ts=4 sw=4 tw=0 :
from tempfile import NamedTemporaryFile

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# vim: set ts=4 sw=4 tw=0 :
from distutils.core import setup

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# vim: set ts=4 sw=4 tw=0 :
import unittest

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import unittest
from tempfile import NamedTemporaryFile

View file

@ -3,6 +3,7 @@
# author: Ole Schuett & Tiziano Müller
import argparse
import os
import pathlib
import re
import sys
@ -125,6 +126,7 @@ def check_file(path: pathlib.Path) -> typing.List[str]:
fn_ext = path.suffix
abspath = path.resolve()
basefn = path.name
is_executable = os.access(abspath, os.X_OK)
if not PORTABLE_FILENAME_RE.match(str(path)):
warnings += [f"Filename '{path}' not portable"]
@ -155,6 +157,11 @@ def check_file(path: pathlib.Path) -> typing.List[str]:
if fn_ext in C_EXTENSIONS and not content.startswith(BANNER_C.format(year, spdx)):
warnings += [f"{path}: Copyright banner malformed"]
# check shebang
PY_SHEBANG = "#!/usr/bin/env python3"
if fn_ext == ".py" and is_executable and not content.startswith(f"{PY_SHEBANG}\n"):
warnings += [f"{path}: Wrong shebang, please use '{PY_SHEBANG}'"]
# find all flags
flags = set()
line_continuation = False