From 7947328de48cbea36cadf2d41b0b401da761ba37 Mon Sep 17 00:00:00 2001 From: Ricardo Martincoski Date: Sun, 26 Dec 2021 15:49:16 -0300 Subject: [PATCH] utils/checkpackagelib: warn about executable files Currently there are no .mk, Config.in, .patch or .hash files with executable permissions in the tree. But we don't want to have that. So warn when a file checked by check-package has executable permission. This check will be reused when testing SysV init scripts in the tree. Signed-off-by: Ricardo Martincoski [Arnout: use context manager for temp dir so it gets deleted] Signed-off-by: Arnout Vandecappelle (Essensium/Mind) --- utils/checkpackagelib/lib_config.py | 1 + utils/checkpackagelib/lib_hash.py | 1 + utils/checkpackagelib/lib_mk.py | 1 + utils/checkpackagelib/lib_patch.py | 1 + utils/checkpackagelib/test_tool.py | 41 +++++++++++++++++++++++++++++ utils/checkpackagelib/tool.py | 8 ++++++ 6 files changed, 53 insertions(+) create mode 100644 utils/checkpackagelib/test_tool.py create mode 100644 utils/checkpackagelib/tool.py diff --git a/utils/checkpackagelib/lib_config.py b/utils/checkpackagelib/lib_config.py index c348eec399..b05831f2c3 100644 --- a/utils/checkpackagelib/lib_config.py +++ b/utils/checkpackagelib/lib_config.py @@ -10,6 +10,7 @@ from checkpackagelib.lib import ConsecutiveEmptyLines # noqa: F401 from checkpackagelib.lib import EmptyLastLine # noqa: F401 from checkpackagelib.lib import NewlineAtEof # noqa: F401 from checkpackagelib.lib import TrailingSpace # noqa: F401 +from checkpackagelib.tool import NotExecutable # noqa: F401 def _empty_or_comment(text): diff --git a/utils/checkpackagelib/lib_hash.py b/utils/checkpackagelib/lib_hash.py index 1c595e84b8..5968c809bf 100644 --- a/utils/checkpackagelib/lib_hash.py +++ b/utils/checkpackagelib/lib_hash.py @@ -10,6 +10,7 @@ from checkpackagelib.lib import ConsecutiveEmptyLines # noqa: F401 from checkpackagelib.lib import EmptyLastLine # noqa: F401 from checkpackagelib.lib import NewlineAtEof # noqa: F401 from checkpackagelib.lib import TrailingSpace # noqa: F401 +from checkpackagelib.tool import NotExecutable # noqa: F401 def _empty_line_or_comment(text): diff --git a/utils/checkpackagelib/lib_mk.py b/utils/checkpackagelib/lib_mk.py index 88e4b15c7c..153754b6f1 100644 --- a/utils/checkpackagelib/lib_mk.py +++ b/utils/checkpackagelib/lib_mk.py @@ -13,6 +13,7 @@ from checkpackagelib.lib import EmptyLastLine # noqa: F401 from checkpackagelib.lib import NewlineAtEof # noqa: F401 from checkpackagelib.lib import TrailingSpace # noqa: F401 from checkpackagelib.lib import Utf8Characters # noqa: F401 +from checkpackagelib.tool import NotExecutable # noqa: F401 # used in more than one check start_conditional = ["ifdef", "ifeq", "ifndef", "ifneq"] diff --git a/utils/checkpackagelib/lib_patch.py b/utils/checkpackagelib/lib_patch.py index e4e914b7f0..caee36158f 100644 --- a/utils/checkpackagelib/lib_patch.py +++ b/utils/checkpackagelib/lib_patch.py @@ -8,6 +8,7 @@ import re from checkpackagelib.base import _CheckFunction from checkpackagelib.lib import NewlineAtEof # noqa: F401 +from checkpackagelib.tool import NotExecutable # noqa: F401 class ApplyOrder(_CheckFunction): diff --git a/utils/checkpackagelib/test_tool.py b/utils/checkpackagelib/test_tool.py new file mode 100644 index 0000000000..51c8e0cb27 --- /dev/null +++ b/utils/checkpackagelib/test_tool.py @@ -0,0 +1,41 @@ +import os +import pytest +import re +import tempfile +import checkpackagelib.tool as m + +workdir_regex = re.compile(r'/tmp/tmp[^/]*-checkpackagelib-test-tool') + + +def check_file(tool, filename, string, permissions=None): + with tempfile.TemporaryDirectory(suffix='-checkpackagelib-test-tool') as workdir: + script = os.path.join(workdir, filename) + with open(script, 'wb') as f: + f.write(string.encode()) + if permissions: + os.chmod(script, permissions) + obj = tool(script) + result = obj.run() + if result is None: + return [] + return [workdir_regex.sub('dir', r) for r in result] + + +NotExecutable = [ + ('664', + 'package.mk', + 0o664, + '', + []), + ('775', + 'package.mk', + 0o775, + '', + ["dir/package.mk:0: This file does not need to be executable"]), + ] + + +@pytest.mark.parametrize('testname,filename,permissions,string,expected', NotExecutable) +def test_NotExecutable(testname, filename, permissions, string, expected): + warnings = check_file(m.NotExecutable, filename, string, permissions) + assert warnings == expected diff --git a/utils/checkpackagelib/tool.py b/utils/checkpackagelib/tool.py new file mode 100644 index 0000000000..f2007be1ff --- /dev/null +++ b/utils/checkpackagelib/tool.py @@ -0,0 +1,8 @@ +import os +from checkpackagelib.base import _Tool + + +class NotExecutable(_Tool): + def run(self): + if os.access(self.filename, os.X_OK): + return ["{}:0: This file does not need to be executable".format(self.filename)]