Skip to content

Commit

Permalink
precommit: Add check for Python shebang
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Nov 24, 2021
1 parent 6c3d14b commit 76e1f5a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/start/python/sample_force_env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# vim: set ts=4 sw=4 tw=0 :

from tempfile import NamedTemporaryFile
Expand Down
2 changes: 1 addition & 1 deletion src/start/python/setup.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/start/python/test/test_force_env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# vim: set ts=4 sw=4 tw=0 :

import unittest
Expand Down
2 changes: 1 addition & 1 deletion src/start/python/test/test_standalone.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import unittest
from tempfile import NamedTemporaryFile
Expand Down
7 changes: 7 additions & 0 deletions tools/precommit/check_file_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# author: Ole Schuett & Tiziano Müller

import argparse
import os
import pathlib
import re
import sys
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 76e1f5a

Please sign in to comment.