summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/python/qmk/cli/format/c.py22
-rwxr-xr-xlib/python/qmk/cli/format/python.py3
2 files changed, 14 insertions, 11 deletions
diff --git a/lib/python/qmk/cli/format/c.py b/lib/python/qmk/cli/format/c.py
index 568684ed56..fe2f97da94 100644
--- a/lib/python/qmk/cli/format/c.py
+++ b/lib/python/qmk/cli/format/c.py
@@ -1,6 +1,5 @@
"""Format C code according to QMK's style.
"""
-from os import path
from shutil import which
from subprocess import CalledProcessError, DEVNULL, Popen, PIPE
@@ -15,6 +14,12 @@ core_dirs = ('drivers', 'quantum', 'tests', 'tmk_core', 'platforms')
ignored = ('tmk_core/protocol/usb_hid', 'platforms/chibios/boards')
+def is_relative_to(file, other):
+ """Provide similar behavior to PurePath.is_relative_to in Python > 3.9
+ """
+ return str(normpath(file).resolve()).startswith(str(normpath(other).resolve()))
+
+
def find_clang_format():
"""Returns the path to clang-format.
"""
@@ -68,18 +73,19 @@ def cformat_run(files):
def filter_files(files, core_only=False):
"""Yield only files to be formatted and skip the rest
"""
+ files = list(map(normpath, filter(None, files)))
if core_only:
# Filter non-core files
for index, file in enumerate(files):
# The following statement checks each file to see if the file path is
# - in the core directories
# - not in the ignored directories
- if not any(str(file).startswith(i) for i in core_dirs) or any(str(file).startswith(i) for i in ignored):
- files[index] = None
+ if not any(is_relative_to(file, i) for i in core_dirs) or any(is_relative_to(file, i) for i in ignored):
+ del files[index]
cli.log.debug("Skipping non-core file %s, as '--core-only' is used.", file)
for file in files:
- if file and file.name.split('.')[-1] in c_file_suffixes:
+ if file.suffix[1:] in c_file_suffixes:
yield file
else:
cli.log.debug('Skipping file %s', file)
@@ -118,12 +124,8 @@ def format_c(cli):
print(git_diff.stderr)
return git_diff.returncode
- files = []
-
- for file in git_diff.stdout.strip().split('\n'):
- if not any([file.startswith(ignore) for ignore in ignored]):
- if path.exists(file) and file.split('.')[-1] in c_file_suffixes:
- files.append(file)
+ changed_files = git_diff.stdout.strip().split('\n')
+ files = list(filter_files(changed_files, True))
# Sanity check
if not files:
diff --git a/lib/python/qmk/cli/format/python.py b/lib/python/qmk/cli/format/python.py
index 47b5c45fd5..008622cac1 100755
--- a/lib/python/qmk/cli/format/python.py
+++ b/lib/python/qmk/cli/format/python.py
@@ -25,8 +25,9 @@ def yapf_run(files):
def filter_files(files):
"""Yield only files to be formatted and skip the rest
"""
+ files = list(map(normpath, filter(None, files)))
for file in files:
- if file and normpath(file).name.split('.')[-1] in py_file_suffixes:
+ if file.suffix[1:] in py_file_suffixes:
yield file
else:
cli.log.debug('Skipping file %s', file)