From cd50fdf7ee6cdb1a5025f4e3e9540c0fd00499a7 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Wed, 17 Nov 2021 23:02:45 +0000 Subject: 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 --- lib/python/qmk/cli/format/python.py | 66 +++++++++++++++++++++++++------ lib/python/qmk/tests/test_cli_commands.py | 4 +- 2 files changed, 56 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/python/qmk/cli/format/python.py b/lib/python/qmk/cli/format/python.py index 00612f97ec..95868d18a0 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 from milc import cli +from qmk.path import normpath -@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.") -@cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True) -def format_python(cli): - """Format python code according to QMK's style. - """ +py_file_suffixes = ('py',) +py_dirs = ['lib/python'] + + +def yapf_run(files): edit = '--diff' if cli.args.dry_run else '--in-place' - yapf_cmd = ['yapf', '-vv', '--recursive', edit, 'bin/qmk', 'lib/python'] + yapf_cmd = ['yapf', '-vv', '--recursive', edit, *files] try: cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL) - cli.log.info('Python code in `bin/qmk` and `lib/python` is correctly formatted.') - return True + cli.log.info('Successfully formatted the python code.') except CalledProcessError: - if cli.args.dry_run: - cli.log.error('Python code in `bin/qmk` and `lib/python` incorrectly formatted!') + cli.log.error(f'Python code in {",".join(py_dirs)} incorrectly formatted!') + return False + + +def filter_files(files): + """Yield only files to be formatted and skip the rest + """ + + for file in files: + if file and file.name.split('.')[-1] in py_file_suffixes: + yield file else: - cli.log.error('Error formatting python code!') + cli.log.debug('Skipping file %s', file) + + +@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.") +@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.') +@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all files.') +@cli.argument('files', nargs='*', arg_only=True, type=normpath, help='Filename(s) to format.') +@cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True) +def format_python(cli): + """Format python code according to QMK's style. + """ + # Find the list of files to format + if cli.args.files: + files = list(filter_files(cli.args.files)) + + if not files: + cli.log.error('No Python files in filelist: %s', ', '.join(map(str, cli.args.files))) + exit(0) + + if cli.args.all_files: + cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files))) + + elif cli.args.all_files: + files = py_dirs + + else: + git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *py_dirs] + git_diff = cli.run(git_diff_cmd, stdin=DEVNULL) + files = list(filter(None, git_diff.stdout.split('\n'))) + + # Sanity check + if not files: + cli.log.error('No changed files detected. Use "qmk format-python -a" to format all files') + return False - return False + 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 b39fe5e46d..0dad5d5fc4 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(): def test_format_python(): - result = check_subcommand('format-python', '--dry-run') + result = check_subcommand('format-python', '-n', '-a') check_returncode(result) - assert 'Python code in `bin/qmk` and `lib/python` is correctly formatted.' in result.stdout + assert 'Successfully formatted the python code.' in result.stdout def test_list_keyboards(): -- cgit v1.2.3 From aee10ccc5c80e71977cbbccb5b86059c87ba7d48 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 19 Nov 2021 04:55:07 +1100 Subject: [CLI] `list-keymaps`/`list-layouts`: Check keyboard passed in (#15204) * [CLI] `list-keymaps`/`list-layouts`: Check keyboard passed in * Update lib/python/qmk/cli/list/keymaps.py Co-authored-by: Joel Challis * Update lib/python/qmk/cli/list/layouts.py Co-authored-by: Joel Challis Co-authored-by: Joel Challis --- lib/python/qmk/cli/list/keymaps.py | 5 +++++ lib/python/qmk/cli/list/layouts.py | 5 +++++ 2 files changed, 10 insertions(+) (limited to 'lib') diff --git a/lib/python/qmk/cli/list/keymaps.py b/lib/python/qmk/cli/list/keymaps.py index d79ab75b58..d2ef136c06 100644 --- a/lib/python/qmk/cli/list/keymaps.py +++ b/lib/python/qmk/cli/list/keymaps.py @@ -13,5 +13,10 @@ from qmk.keyboard import keyboard_completer, keyboard_folder def list_keymaps(cli): """List the keymaps for a specific keyboard """ + if not cli.config.list_keymaps.keyboard: + cli.log.error('Missing required arguments: --keyboard') + cli.subcommands['list-keymaps'].print_help() + return False + for name in qmk.keymap.list_keymaps(cli.config.list_keymaps.keyboard): print(name) diff --git a/lib/python/qmk/cli/list/layouts.py b/lib/python/qmk/cli/list/layouts.py index 8e07afeeca..df593dc390 100644 --- a/lib/python/qmk/cli/list/layouts.py +++ b/lib/python/qmk/cli/list/layouts.py @@ -13,6 +13,11 @@ from qmk.info import info_json def list_layouts(cli): """List the layouts for a specific keyboard """ + if not cli.config.list_layouts.keyboard: + cli.log.error('Missing required arguments: --keyboard') + cli.subcommands['list-layouts'].print_help() + return False + info_data = info_json(cli.config.list_layouts.keyboard) for name in sorted(info_data.get('community_layouts', [])): print(name) -- cgit v1.2.3