2018-03-21 20:02:06 +01:00
|
|
|
#!/usr/bin/env python
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2018-03-21 20:02:06 +01:00
|
|
|
|
|
|
|
import os
|
2020-02-21 22:34:21 +01:00
|
|
|
import re
|
2018-05-22 23:25:36 +02:00
|
|
|
import stat
|
2018-03-21 20:02:06 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-01-15 22:31:33 +01:00
|
|
|
standard_shebangs = set([
|
2018-03-21 20:02:06 +01:00
|
|
|
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',
|
|
|
|
])
|
|
|
|
|
2019-01-15 22:31:33 +01:00
|
|
|
integration_shebangs = set([
|
|
|
|
b'#!/bin/sh',
|
|
|
|
b'#!/usr/bin/env bash',
|
|
|
|
b'#!/usr/bin/env python',
|
|
|
|
])
|
|
|
|
|
2018-03-21 20:02:06 +01:00
|
|
|
module_shebangs = {
|
|
|
|
'': b'#!/usr/bin/python',
|
|
|
|
'.py': b'#!/usr/bin/python',
|
|
|
|
'.ps1': b'#!powershell',
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:21:41 +02:00
|
|
|
# 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'),
|
|
|
|
)
|
|
|
|
|
2018-03-21 20:02:06 +01:00
|
|
|
for path in sys.argv[1:] or sys.stdin.read().splitlines():
|
|
|
|
with open(path, 'rb') as path_fd:
|
|
|
|
shebang = path_fd.readline().strip()
|
2018-05-22 23:25:36 +02:00
|
|
|
mode = os.stat(path).st_mode
|
|
|
|
executable = (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) & mode
|
2018-03-21 20:02:06 +01:00
|
|
|
|
2018-05-22 23:25:36 +02:00
|
|
|
if not shebang or not shebang.startswith(b'#!'):
|
|
|
|
if executable:
|
|
|
|
print('%s:%d:%d: file without shebang should not be executable' % (path, 0, 0))
|
2018-03-21 20:02:06 +01:00
|
|
|
|
2018-10-15 20:21:41 +02:00
|
|
|
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
|
|
|
|
|
2018-03-21 20:02:06 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
is_module = False
|
2019-01-15 22:31:33 +01:00
|
|
|
is_integration = False
|
2018-03-21 20:02:06 +01:00
|
|
|
|
2019-07-10 02:31:04 +02:00
|
|
|
dirname = os.path.dirname(path)
|
|
|
|
|
2018-03-21 20:02:06 +01:00
|
|
|
if path.startswith('lib/ansible/modules/'):
|
|
|
|
is_module = True
|
2020-02-21 22:34:21 +01:00
|
|
|
elif re.search('^test/support/[^/]+/plugins/modules/', path):
|
|
|
|
is_module = True
|
|
|
|
elif re.search('^test/support/[^/]+/collections/ansible_collections/[^/]+/[^/]+/plugins/modules/', path):
|
|
|
|
is_module = True
|
2019-08-06 23:43:29 +02:00
|
|
|
elif path.startswith('test/lib/ansible_test/_data/'):
|
|
|
|
pass
|
|
|
|
elif path.startswith('lib/') or path.startswith('test/lib/'):
|
2018-05-22 23:25:36 +02:00
|
|
|
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
|
2019-08-29 04:16:15 +02:00
|
|
|
elif path.startswith('test/integration/targets/') or path.startswith('tests/integration/targets/'):
|
2019-01-15 22:31:33 +01:00
|
|
|
is_integration = True
|
|
|
|
|
2019-08-21 11:45:04 +02:00
|
|
|
if dirname.endswith('/library') or '/plugins/modules' in dirname or dirname in (
|
2019-07-12 22:17:20 +02:00
|
|
|
# non-standard module library directories
|
|
|
|
'test/integration/targets/module_precedence/lib_no_extension',
|
|
|
|
'test/integration/targets/module_precedence/lib_with_extension',
|
2018-03-21 20:02:06 +01:00
|
|
|
):
|
|
|
|
is_module = True
|
2020-03-19 17:37:37 +01:00
|
|
|
elif path.startswith('plugins/modules/'):
|
2019-07-10 02:31:04 +02:00
|
|
|
is_module = True
|
2018-03-21 20:02:06 +01:00
|
|
|
|
|
|
|
if is_module:
|
2018-05-22 23:25:36 +02:00
|
|
|
if executable:
|
|
|
|
print('%s:%d:%d: module should not be executable' % (path, 0, 0))
|
|
|
|
|
2018-03-21 20:02:06 +01:00
|
|
|
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:
|
2019-01-15 22:31:33 +01:00
|
|
|
if is_integration:
|
|
|
|
allowed = integration_shebangs
|
|
|
|
else:
|
|
|
|
allowed = standard_shebangs
|
|
|
|
|
2018-03-21 20:02:06 +01:00
|
|
|
if shebang not in allowed:
|
|
|
|
print('%s:%d:%d: unexpected non-module shebang: %s' % (path, 1, 1, shebang))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|