From 0a8a99c17bf243e0037ffab0540549ebe75626c9 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Thu, 12 Nov 2020 14:11:54 +0800 Subject: [PATCH] pre-commit: recognize Windows excecutable files with git --- tools/ci/check_executables.py | 4 ++++ tools/ci/idf_ci_utils.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/tools/ci/check_executables.py b/tools/ci/check_executables.py index 0b0f1435c8..4f1e21e871 100755 --- a/tools/ci/check_executables.py +++ b/tools/ci/check_executables.py @@ -18,6 +18,8 @@ import argparse import os from sys import exit +from idf_ci_utils import is_executable + def _strip_each_item(iterable): res = [] @@ -44,6 +46,8 @@ def check_executable_list(): def check_executables(files): ret = 0 for fn in files: + if not is_executable(fn): + continue if fn not in known_executables: print('"{}" is not in {}'.format(fn, EXECUTABLE_LIST_FN)) ret = 1 diff --git a/tools/ci/idf_ci_utils.py b/tools/ci/idf_ci_utils.py index 4522454b1b..ee92b5db0d 100644 --- a/tools/ci/idf_ci_utils.py +++ b/tools/ci/idf_ci_utils.py @@ -19,6 +19,7 @@ import logging import os import subprocess +import sys IDF_PATH = os.getenv('IDF_PATH', os.path.join(os.path.dirname(__file__), '..', '..')) @@ -44,3 +45,27 @@ def get_submodule_dirs(full_path=False): # type: (bool) -> list logging.warning(str(e)) return dirs + + +def _check_git_filemode(full_path): # type: (str) -> bool + try: + stdout = subprocess.check_output(['git', 'ls-files', '--stage', full_path]).strip().decode('utf-8') + except subprocess.CalledProcessError: + return True + + mode = stdout.split(' ', 1)[0] # e.g. 100644 for a rw-r--r-- + if any([int(i, 8) & 1 for i in mode[-3:]]): + return False + return True + + +def is_executable(full_path): # type: (str) -> bool + """ + os.X_OK will always return true on windows. Use git to check file mode. + :param full_path: file full path + :return: True is it's an executable file + """ + if sys.platform == 'win32': + return _check_git_filemode(full_path) + else: + return os.access(full_path, os.X_OK)