Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f956f42

Browse files
committed
Replace our implementation of shebang parsing with identify's
1 parent a58d99a commit f956f42

2 files changed

Lines changed: 4 additions & 57 deletions

File tree

pre_commit/parse_shebang.py

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,21 @@
11
from __future__ import absolute_import
22
from __future__ import unicode_literals
33

4-
import io
54
import os.path
6-
import shlex
7-
import string
85

9-
10-
printable = frozenset(string.printable)
6+
from identify.identify import parse_shebang_from_file
117

128

139
class ExecutableNotFoundError(OSError):
1410
def to_output(self):
1511
return (1, self.args[0].encode('UTF-8'), b'')
1612

1713

18-
def parse_bytesio(bytesio):
19-
"""Parse the shebang from a file opened for reading binary."""
20-
if bytesio.read(2) != b'#!':
21-
return ()
22-
first_line = bytesio.readline()
23-
try:
24-
first_line = first_line.decode('US-ASCII')
25-
except UnicodeDecodeError:
26-
return ()
27-
28-
# Require only printable ascii
29-
for c in first_line:
30-
if c not in printable:
31-
return ()
32-
33-
cmd = tuple(shlex.split(first_line))
34-
if cmd[0] == '/usr/bin/env':
35-
cmd = cmd[1:]
36-
return cmd
37-
38-
3914
def parse_filename(filename):
40-
"""Parse the shebang given a filename."""
41-
if not os.path.exists(filename) or not os.access(filename, os.X_OK):
15+
if not os.path.exists(filename):
4216
return ()
43-
44-
with io.open(filename, 'rb') as f:
45-
return parse_bytesio(f)
17+
else:
18+
return parse_shebang_from_file(filename)
4619

4720

4821
def find_executable(exe, _environ=None):

tests/parse_shebang_test.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,10 @@
1515
from pre_commit.util import make_executable
1616

1717

18-
@pytest.mark.parametrize(
19-
('s', 'expected'),
20-
(
21-
(b'', ()),
22-
(b'#!/usr/bin/python', ('/usr/bin/python',)),
23-
(b'#!/usr/bin/env python', ('python',)),
24-
(b'#! /usr/bin/python', ('/usr/bin/python',)),
25-
(b'#!/usr/bin/foo python', ('/usr/bin/foo', 'python')),
26-
(b'\xf9\x93\x01\x42\xcd', ()),
27-
(b'#!\xf9\x93\x01\x42\xcd', ()),
28-
(b'#!\x00\x00\x00\x00', ()),
29-
),
30-
)
31-
def test_parse_bytesio(s, expected):
32-
assert parse_shebang.parse_bytesio(io.BytesIO(s)) == expected
33-
34-
3518
def test_file_doesnt_exist():
3619
assert parse_shebang.parse_filename('herp derp derp') == ()
3720

3821

39-
@pytest.mark.xfail(
40-
sys.platform == 'win32', reason='Windows says everything is X_OK',
41-
)
42-
def test_file_not_executable(tmpdir):
43-
x = tmpdir.join('f')
44-
x.write_text('#!/usr/bin/env python', encoding='UTF-8')
45-
assert parse_shebang.parse_filename(x.strpath) == ()
46-
47-
4822
def test_simple_case(tmpdir):
4923
x = tmpdir.join('f')
5024
x.write_text('#!/usr/bin/env python', encoding='UTF-8')

0 commit comments

Comments
 (0)