Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the py, pydsl, and pyobjects interpreters for salt #60

Closed
Closed
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
19 changes: 8 additions & 11 deletions identify/identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ def tags_from_path(path):
t = tags_from_filename(os.path.basename(path))
if len(t) > 0:
tags.update(t)
else:
if executable:
shebang = parse_shebang_from_file(path)
if len(shebang) > 0:
tags.update(tags_from_interpreter(shebang[0]))

if file_is_text(path):
tags.add(TEXT)
else:
tags.add(BINARY)
shebang = parse_shebang_from_file(path)
if len(shebang) > 0:
tags.update(tags_from_interpreter(shebang[0]))

if file_is_text(path):
tags.add(TEXT)
else:
tags.add(BINARY)

assert {TEXT, BINARY} & tags, tags
assert {EXECUTABLE, NON_EXECUTABLE} & tags, tags
Expand Down Expand Up @@ -155,8 +154,6 @@ def parse_shebang_from_file(path):
"""Parse the shebang given a file path."""
if not os.path.lexists(path):
raise ValueError('{} does not exist.'.format(path))
if not os.access(path, os.X_OK):
return ()

with open(path, 'rb') as f:
return parse_shebang(f)
3 changes: 3 additions & 0 deletions identify/interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
'node': {'javascript'},
'nodejs': {'javascript'},
'perl': {'perl'},
'py': {'python'},
'pydsl': {'python', 'pydsl'},
'pyobjects': {'python', 'pyobjects'},
'python': {'python'},
'python2': {'python', 'python2'},
'python3': {'python', 'python3'},
Expand Down
39 changes: 37 additions & 2 deletions tests/identify_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,39 @@ def test_tags_from_path_file_with_shebang_non_executable(tmpdir):
x = tmpdir.join('test')
x.write_text('#!/usr/bin/env python\nimport sys\n', encoding='UTF-8')
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'non-executable',
'file', 'text', 'non-executable', 'python',
}


def test_tags_from_path_file_with_shebang_non_executable_sls_py(tmpdir):
x = tmpdir.join('test.sls')
x.write_text('#! py\nimport sys\n', encoding='UTF-8')
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'non-executable', 'python', 'salt',
}


def test_tags_from_path_file_with_shebang_non_executable_sls_pydsl(tmpdir):
x = tmpdir.join('test.sls')
x.write_text('#! pydsl\nimport sys\n', encoding='UTF-8')
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'non-executable', 'python', 'salt', 'pydsl',
}


def test_tags_from_path_file_with_shebang_non_executable_sls_pyobjects(tmpdir):
x = tmpdir.join('test.sls')
x.write_text('#! pyobjects\nimport sys\n', encoding='UTF-8')
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'non-executable', 'python', 'salt', 'pyobjects',
}


def test_tags_from_path_file_with_shebang_non_executable_sls_yaml(tmpdir):
x = tmpdir.join('test.sls')
x.write_text('foo:\n - bar\n', encoding='UTF-8')
assert identify.tags_from_path(x.strpath) == {
'file', 'text', 'non-executable', 'salt',
}


Expand Down Expand Up @@ -109,6 +141,9 @@ def test_tags_from_filename(filename, expected):
@pytest.mark.parametrize(
('interpreter', 'expected'),
(
('py', {'python'}),
('pydsl', {'python', 'pydsl'}),
('pyobjects', {'python', 'pyobjects'}),
('python', {'python'}),
('python3', {'python3', 'python'}),
('python3.5.2', {'python3', 'python'}),
Expand Down Expand Up @@ -184,7 +219,7 @@ def test_parse_shebang_from_file_does_not_exist():
def test_parse_shebang_from_file_nonexecutable(tmpdir):
x = tmpdir.join('f')
x.write_text('#!/usr/bin/env python', encoding='UTF-8')
assert identify.parse_shebang_from_file(x.strpath) == ()
assert identify.parse_shebang_from_file(x.strpath) == ('python',)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is inappropriate -- this test is intentionally demonstrating that files that are non-executable are never checked for shebangs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed that functionality in this PR as well since it is absolutely not required for a python file to be executable. This logic makes identify flag python files that are not executable as not python

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So to respond to your comment since I removed that logic I have also removed the test for it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, and I'm telling you that's unacceptable

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I'm sorry I only saw the inline comment and not the comment to the PR itself :) my fault

OK, why do you say this is unacceptable?

I thought I had left a comment about possibly adding some logic for only looking at the content for specific file types but I don't actually see that comment in here so my apologies, but yes for performances sake I would go that way myself.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I had left the comment in #61

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific way you want to continue with this? Conditional inspection would also be nice for .mk files since those are not only used for Makefiles but python config files for Check-MK's version of nagios.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something similar to #63?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice! Ok, I'll try that out for these and update asap. Thanks



def test_parse_shebang_from_file_simple(tmpdir):
Expand Down