mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.9 KiB
Python
116 lines
3.9 KiB
Python
7 years ago
|
#!/usr/bin/env python
|
||
5 years ago
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
7 years ago
|
|
||
|
import os
|
||
7 years ago
|
import stat
|
||
7 years ago
|
import sys
|
||
|
|
||
|
|
||
|
def main():
|
||
6 years ago
|
standard_shebangs = set([
|
||
7 years ago
|
b'#!/bin/bash -eu',
|
||
|
b'#!/bin/bash -eux',
|
||
|
b'#!/bin/sh',
|
||
|
b'#!/usr/bin/env bash',
|
||
|
b'#!/usr/bin/env fish',
|
||
|
b'#!/usr/bin/env pwsh',
|
||
|
b'#!/usr/bin/env python',
|
||
|
b'#!/usr/bin/make -f',
|
||
|
])
|
||
|
|
||
6 years ago
|
integration_shebangs = set([
|
||
|
b'#!/bin/sh',
|
||
|
b'#!/usr/bin/env bash',
|
||
|
b'#!/usr/bin/env python',
|
||
|
])
|
||
|
|
||
7 years ago
|
module_shebangs = {
|
||
|
'': b'#!/usr/bin/python',
|
||
|
'.py': b'#!/usr/bin/python',
|
||
|
'.ps1': b'#!powershell',
|
||
|
}
|
||
|
|
||
6 years ago
|
# see https://unicode.org/faq/utf_bom.html#bom1
|
||
|
byte_order_marks = (
|
||
|
(b'\x00\x00\xFE\xFF', 'UTF-32 (BE)'),
|
||
|
(b'\xFF\xFE\x00\x00', 'UTF-32 (LE)'),
|
||
|
(b'\xFE\xFF', 'UTF-16 (BE)'),
|
||
|
(b'\xFF\xFE', 'UTF-16 (LE)'),
|
||
|
(b'\xEF\xBB\xBF', 'UTF-8'),
|
||
|
)
|
||
|
|
||
7 years ago
|
for path in sys.argv[1:] or sys.stdin.read().splitlines():
|
||
|
with open(path, 'rb') as path_fd:
|
||
|
shebang = path_fd.readline().strip()
|
||
7 years ago
|
mode = os.stat(path).st_mode
|
||
|
executable = (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) & mode
|
||
7 years ago
|
|
||
7 years ago
|
if not shebang or not shebang.startswith(b'#!'):
|
||
|
if executable:
|
||
|
print('%s:%d:%d: file without shebang should not be executable' % (path, 0, 0))
|
||
7 years ago
|
|
||
6 years ago
|
for mark, name in byte_order_marks:
|
||
|
if shebang.startswith(mark):
|
||
|
print('%s:%d:%d: file starts with a %s byte order mark' % (path, 0, 0, name))
|
||
|
break
|
||
|
|
||
7 years ago
|
continue
|
||
|
|
||
|
is_module = False
|
||
6 years ago
|
is_integration = False
|
||
7 years ago
|
|
||
5 years ago
|
dirname = os.path.dirname(path)
|
||
|
|
||
7 years ago
|
if path.startswith('lib/ansible/modules/'):
|
||
|
is_module = True
|
||
5 years ago
|
elif path.startswith('test/lib/ansible_test/_data/'):
|
||
|
pass
|
||
|
elif path.startswith('lib/') or path.startswith('test/lib/'):
|
||
7 years ago
|
if executable:
|
||
|
print('%s:%d:%d: should not be executable' % (path, 0, 0))
|
||
|
|
||
|
if shebang:
|
||
|
print('%s:%d:%d: should not have a shebang' % (path, 0, 0))
|
||
|
|
||
|
continue
|
||
7 years ago
|
elif path.startswith('test/integration/targets/'):
|
||
6 years ago
|
is_integration = True
|
||
|
|
||
5 years ago
|
if dirname.endswith('/library') or dirname.endswith('/plugins/modules') or dirname in (
|
||
5 years ago
|
# non-standard module library directories
|
||
|
'test/integration/targets/module_precedence/lib_no_extension',
|
||
|
'test/integration/targets/module_precedence/lib_with_extension',
|
||
7 years ago
|
):
|
||
|
is_module = True
|
||
5 years ago
|
elif dirname == 'plugins/modules':
|
||
|
is_module = True
|
||
7 years ago
|
|
||
|
if is_module:
|
||
7 years ago
|
if executable:
|
||
|
print('%s:%d:%d: module should not be executable' % (path, 0, 0))
|
||
|
|
||
7 years ago
|
ext = os.path.splitext(path)[1]
|
||
|
expected_shebang = module_shebangs.get(ext)
|
||
|
expected_ext = ' or '.join(['"%s"' % k for k in module_shebangs])
|
||
|
|
||
|
if expected_shebang:
|
||
|
if shebang == expected_shebang:
|
||
|
continue
|
||
|
|
||
|
print('%s:%d:%d: expected module shebang "%s" but found: %s' % (path, 1, 1, expected_shebang, shebang))
|
||
|
else:
|
||
|
print('%s:%d:%d: expected module extension %s but found: %s' % (path, 0, 0, expected_ext, ext))
|
||
|
else:
|
||
6 years ago
|
if is_integration:
|
||
|
allowed = integration_shebangs
|
||
|
else:
|
||
|
allowed = standard_shebangs
|
||
|
|
||
7 years ago
|
if shebang not in allowed:
|
||
|
print('%s:%d:%d: unexpected non-module shebang: %s' % (path, 1, 1, shebang))
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|