aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2021-11-17 23:02:45 +0000
committerGitHub <noreply@github.com>2021-11-17 23:02:45 +0000
commitcd50fdf7ee6cdb1a5025f4e3e9540c0fd00499a7 (patch)
tree47e7c6405b52374a79ae328ed3accbaa1e8c925b /lib
parentb5d24f9efb316351c73763eeac5fcc11b98e0ba1 (diff)
downloadqmk_firmware-cd50fdf7ee6cdb1a5025f4e3e9540c0fd00499a7.tar.gz
qmk_firmware-cd50fdf7ee6cdb1a5025f4e3e9540c0fd00499a7.zip
Add diff logic to python format subcommand (#15156)
* Add diff logic to python format subcommand * Update test * Add in filter per format-c * fix tests * Update new workflow
Diffstat (limited to 'lib')
-rwxr-xr-xlib/python/qmk/cli/format/python.py66
-rw-r--r--lib/python/qmk/tests/test_cli_commands.py4
2 files changed, 56 insertions, 14 deletions
diff --git a/lib/python/qmk/cli/format/python.py b/lib/python/qmk/cli/format/python.py
index 00612f97e..95868d18a 100755
--- a/lib/python/qmk/cli/format/python.py
+++ b/lib/python/qmk/cli/format/python.py
@@ -4,23 +4,65 @@ from subprocess import CalledProcessError, DEVNULL
4 4
5from milc import cli 5from milc import cli
6 6
7from qmk.path import normpath
7 8
8@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.") 9py_file_suffixes = ('py',)
9@cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True) 10py_dirs = ['lib/python']
10def format_python(cli): 11
11 """Format python code according to QMK's style. 12
12 """ 13def yapf_run(files):
13 edit = '--diff' if cli.args.dry_run else '--in-place' 14 edit = '--diff' if cli.args.dry_run else '--in-place'
14 yapf_cmd = ['yapf', '-vv', '--recursive', edit, 'bin/qmk', 'lib/python'] 15 yapf_cmd = ['yapf', '-vv', '--recursive', edit, *files]
15 try: 16 try:
16 cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL) 17 cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL)
17 cli.log.info('Python code in `bin/qmk` and `lib/python` is correctly formatted.') 18 cli.log.info('Successfully formatted the python code.')
18 return True
19 19
20 except CalledProcessError: 20 except CalledProcessError:
21 if cli.args.dry_run: 21 cli.log.error(f'Python code in {",".join(py_dirs)} incorrectly formatted!')
22 cli.log.error('Python code in `bin/qmk` and `lib/python` incorrectly formatted!') 22 return False
23
24
25def filter_files(files):
26 """Yield only files to be formatted and skip the rest
27 """
28
29 for file in files:
30 if file and file.name.split('.')[-1] in py_file_suffixes:
31 yield file
23 else: 32 else:
24 cli.log.error('Error formatting python code!') 33 cli.log.debug('Skipping file %s', file)
34
35
36@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.")
37@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
38@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all files.')
39@cli.argument('files', nargs='*', arg_only=True, type=normpath, help='Filename(s) to format.')
40@cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True)
41def format_python(cli):
42 """Format python code according to QMK's style.
43 """
44 # Find the list of files to format
45 if cli.args.files:
46 files = list(filter_files(cli.args.files))
47
48 if not files:
49 cli.log.error('No Python files in filelist: %s', ', '.join(map(str, cli.args.files)))
50 exit(0)
51
52 if cli.args.all_files:
53 cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
54
55 elif cli.args.all_files:
56 files = py_dirs
57
58 else:
59 git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *py_dirs]
60 git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)
61 files = list(filter(None, git_diff.stdout.split('\n')))
62
63 # Sanity check
64 if not files:
65 cli.log.error('No changed files detected. Use "qmk format-python -a" to format all files')
66 return False
25 67
26 return False 68 return yapf_run(files)
diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py
index b39fe5e46..0dad5d5fc 100644
--- a/lib/python/qmk/tests/test_cli_commands.py
+++ b/lib/python/qmk/tests/test_cli_commands.py
@@ -81,9 +81,9 @@ def test_hello():
81 81
82 82
83def test_format_python(): 83def test_format_python():
84 result = check_subcommand('format-python', '--dry-run') 84 result = check_subcommand('format-python', '-n', '-a')
85 check_returncode(result) 85 check_returncode(result)
86 assert 'Python code in `bin/qmk` and `lib/python` is correctly formatted.' in result.stdout 86 assert 'Successfully formatted the python code.' in result.stdout
87 87
88 88
89def test_list_keyboards(): 89def test_list_keyboards():