summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorQMK Bot <hello@qmk.fm>2022-01-03 21:55:23 +0000
committerQMK Bot <hello@qmk.fm>2022-01-03 21:55:23 +0000
commit73e372d920dfedce3c026f05886f416529e94d3c (patch)
tree0fb3f4486fccddafb1065e9111614a1c52422cbc /lib
parent1f1d2ffd29faf90b2a899fec71fc09f7a09481dd (diff)
parentaea71554234cce71f9f9d4087253e53d2729f9a7 (diff)
Merge remote-tracking branch 'origin/master' into develop
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)