Skip to content
Open
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
23 changes: 11 additions & 12 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,34 @@ environment:
- TOXENV: lint
PYTHON_VERSION: 3.10
APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004
- TOXENV: py39
- TOXENV: py39-windows
PYTHON_VERSION: 3.9
APPVEYOR_BUILD_WORKER_IMAGE: "Visual Studio 2019"
- TOXENV: py37
PYTHON_VERSION: 3.7
- TOXENV: py311-macos
PYTHON_VERSION: 3.11
APPVEYOR_BUILD_WORKER_IMAGE: macOS
- TOXENV: py38
- TOXENV: py38-linux
PYTHON_VERSION: 3.8
APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004
- TOXENV: py39
- TOXENV: py39-linux
PYTHON_VERSION: 3.9
APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004
- TOXENV: py310
- TOXENV: py310-linux
PYTHON_VERSION: 3.10
APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004
- TOXENV: py311
- TOXENV: py311-linux
PYTHON_VERSION: 3.11
APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004

build: false

install:
# AppVeyor requires us to manually source a virtualenv for non-Windows tests. This line is allowed to fail (and should
# fail) on Windows images.
- source ~/venv${PYTHON_VERSION}/bin/activate || (exit 0)
- pip install -U tox
- sh: source ~/venv${PYTHON_VERSION}/bin/activate
- cmd: set PATH=C:\Python39;C:\Python39\Scripts;%PATH%
- python -m pip install -U tox

test_script:
- tox -vv
- python -m tox -vv

branches:
only:
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ repos:
Normally, after flake8-executable is installed, invoking flake8 will also run this plugin. For more
details, check out the [Flake8 plugin page][].

### Nix-shell scripts

EXE003 also accepts a `nix-shell` shebang when its option lines select a Python
interpreter with `-i`, as described in the [Nix reference manual][nix-shell]:

```python
#!/usr/bin/env nix-shell
#! nix-shell -i python3
#! nix-shell --packages python3
print("Hello world")
```

The first line selects `nix-shell`; the subsequent directives provide its interpreter
and dependencies. Merely including a Python package without selecting a Python
interpreter does not suppress EXE003. The file must still have executable permission
to satisfy EXE001.

[nix-shell]: https://nix.dev/manual/nix/stable/command-ref/nix-shell.html#use-as-a--interpreter

## Copyright and License

Copyright (c) 2019 Hong Xu <hong@topbug.net>
Expand Down
25 changes: 24 additions & 1 deletion flake8_executable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from abc import ABC
import os
import re
import shlex
from typing import Any, Iterable, List, Tuple, Optional, Union

from ._version import version as __version__ # type: ignore
Expand Down Expand Up @@ -113,6 +114,28 @@ def __init__(self,
self.filename = filename
self.lines = lines

@staticmethod
def _nix_shell_uses_python(shebang: str, lines: List[str]) -> bool:
if not re.match(r'^\s*#!\s*(?:\S*/)?(?:env\s+)?nix-shell(?:\s|$)', shebang):
return False

# nix-shell combines options from all of its directive lines, with the last -i taking effect.
args: List[str] = []
for line in lines:
match = re.match(r'^#!\s*nix-shell\s+(.*)$', line)
if match:
try:
args.extend(shlex.split(match.group(1)))
except ValueError:
return False

interpreter = ''
arguments = iter(args)
for argument in arguments:
if argument == '-i':
interpreter = next(arguments, '')
return 'python' in interpreter

def run(self) -> Optional[Iterable[Error]]:
# Get lines if its not already read
if self.lines is None:
Expand All @@ -139,7 +162,7 @@ def run(self) -> Optional[Iterable[Error]]:
if not is_executable: # pragma: no cover windows. No execution of this branch on Windows
if EXE001.should_check(filename=self.filename):
yield EXE001(line_number=shebang_lineno)()
if 'python' not in shebang_line:
if 'python' not in shebang_line and not self._nix_shell_uses_python(shebang_line, self.lines):
if EXE003.should_check():
yield EXE003(line_number=shebang_lineno, shebang=shebang_line.strip())()
if shebang_lineno > 1:
Expand Down
2 changes: 2 additions & 0 deletions requirements/lint.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
bandit==1.7.4
# Bandit 1.7.4 imports pbr but does not declare it as a dependency.
pbr==5.11.1
flake8==6.0.0
flake8-bugbear==21.11.29
flake8-comprehensions==3.10.1
Expand Down
51 changes: 51 additions & 0 deletions tests/test_flake8_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,57 @@ def test_stdin_negative_empty(self, filename):
ec = ExecutableChecker(filename=filename, lines=[])
assert len(tuple(ec.run())) == 0

@pytest.mark.parametrize('shebang', [
'#!/usr/bin/env nix-shell',
'#! /usr/bin/env nix-shell',
'#!/nix/var/nix/profiles/default/bin/nix-shell',
])
@pytest.mark.parametrize('options', [
'#! nix-shell -i python3 --packages python3',
'#!nix-shell -i python3\n#!nix-shell --packages python3',
'#! nix-shell --packages python3\n#! nix-shell -i "python3"',
"#! nix-shell -i python3 -p 'python3.withPackages (ps: [ ps.requests ])'",
])
def test_nix_shell_python(self, shebang, options):
lines = (shebang + '\n' + options + '\nprint("hello")\n').splitlines(True)
assert tuple(ExecutableChecker(filename='-', lines=lines).run()) == ()

@pytest.mark.parametrize('options', [
'',
'#! nix-shell --packages python3',
'#! nix-shell -i bash --packages python3',
'#! nix-shell -i python3\n#! nix-shell -i bash',
'#! nix-shell -i',
'#! nix-shell -i "python3',
'# ordinary comment mentioning -i python3',
])
def test_nix_shell_without_python_interpreter(self, options):
shebang = '#!/usr/bin/env nix-shell'
lines = (shebang + '\n' + options + '\n').splitlines(True)
assert tuple(ExecutableChecker(filename='-', lines=lines).run()) == (
EXE003(line_number=1, shebang=shebang)(),
)

@pytest.mark.parametrize('shebang', ['#!/bin/bash', '#!/usr/bin/env nix-shell-wrapper'])
def test_nix_shell_options_require_nix_shell_shebang(self, shebang):
lines = [shebang + '\n', '#! nix-shell -i python3\n']
assert tuple(ExecutableChecker(filename='-', lines=lines).run()) == (
EXE003(line_number=1, shebang=shebang)(),
)

def test_nix_shell_misplaced_shebang(self):
lines = ['# comment\n', '#!/usr/bin/env nix-shell\n', '#! nix-shell -i python3\n']
assert tuple(ExecutableChecker(filename='-', lines=lines).run()) == (EXE005(line_number=2)(),)

@pytest.mark.skipif(WIN32, reason="Windows doesn't support EXE001")
@pytest.mark.parametrize('executable', [True, False])
def test_nix_shell_permissions(self, tmp_path, executable):
script = tmp_path / 'script.py'
script.write_text('#!/usr/bin/env nix-shell\n#! nix-shell -i python3\n')
script.chmod(0o755 if executable else 0o644)
errors = tuple(ExecutableChecker(filename=str(script)).run())
assert errors == (() if executable else (EXE001(line_number=1)(),))

def test_cli(self):
"Test the flake8 CLI interface and ensure there's no crash."
import flake8.main.application
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
[tox]
minversion = 3.20
envlist = py{37,38,39,310,311}-{linux,macos,windows}
skipsdist = True

[testenv]
download = True
Expand Down