From fcf1847051b2e1dcd18013720c6f138bbed2ed92 Mon Sep 17 00:00:00 2001 From: wwx1101975 Date: Sun, 9 Oct 2022 16:04:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=8C=E8=BF=9B=E5=88=B6?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ac/acl/binary/check_binary_file.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/ac/acl/binary/check_binary_file.py b/src/ac/acl/binary/check_binary_file.py index c1d448e..61f2e3f 100644 --- a/src/ac/acl/binary/check_binary_file.py +++ b/src/ac/acl/binary/check_binary_file.py @@ -23,6 +23,8 @@ from src.ac.framework.ac_result import FAILED, SUCCESS from src.ac.common.gitee_repo import GiteeRepo from pyrpm.spec import Spec, replace_macros +from src.utils.shell_cmd import shell_cmd + logger = logging.getLogger("ac") @@ -117,9 +119,25 @@ class CheckBinaryFile(BaseCheck): for single_file in files: file_suffixes = os.path.splitext(single_file)[1] if file_suffixes in self.BINARY_LIST: - binary_file.append(single_file) + if self._get_file_type(os.path.join(root, single_file)): + binary_file.append(single_file) if binary_file: binary_list.append({root.split("code")[-1]: binary_file}) return binary_list - + @staticmethod + def _get_file_type(filepath): + """ + run the file command to check whether a file is a binary file + :param filepath: + :return: + """ + file_cmd = "file -b {}".format(filepath) + ret, out, _ = shell_cmd(file_cmd) + if ret: + logger.error("file command error, %s", ret) + return False + if out and "text" not in str(out).lower(): + logger.debug("%s not a text file", filepath) + return True + return False -- Gitee