aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2021-11-14 23:06:22 +0000
committerGitHub <noreply@github.com>2021-11-14 23:06:22 +0000
commitec0bb791f89c1ba53a9dcc3ca02ec6ad9823d2a1 (patch)
treeeac65380fc087cabb45ce842942efc3456bfa854 /lib/python/qmk
parentb785305080fe6454d1a5c8a818c538d9e693090f (diff)
downloadqmk_firmware-ec0bb791f89c1ba53a9dcc3ca02ec6ad9823d2a1.tar.gz
qmk_firmware-ec0bb791f89c1ba53a9dcc3ca02ec6ad9823d2a1.zip
Partially reinstate CI formatting process (#15155)
* Partially reinstate CI formatting process * Fix exit code on clean run * Fix exit code on clean run
Diffstat (limited to 'lib/python/qmk')
-rw-r--r--lib/python/qmk/cli/format/text.py64
1 files changed, 47 insertions, 17 deletions
diff --git a/lib/python/qmk/cli/format/text.py b/lib/python/qmk/cli/format/text.py
index e7e07b729..6dd451189 100644
--- a/lib/python/qmk/cli/format/text.py
+++ b/lib/python/qmk/cli/format/text.py
@@ -1,27 +1,57 @@
1"""Ensure text files have the proper line endings. 1"""Ensure text files have the proper line endings.
2""" 2"""
3from subprocess import CalledProcessError 3from itertools import islice
4from subprocess import DEVNULL
4 5
5from milc import cli 6from milc import cli
6 7
8from qmk.path import normpath
7 9
10
11def _get_chunks(it, size):
12 """Break down a collection into smaller parts
13 """
14 it = iter(it)
15 return iter(lambda: tuple(islice(it, size)), ())
16
17
18def dos2unix_run(files):
19 """Spawn multiple dos2unix subprocess avoiding too long commands on formatting everything
20 """
21 for chunk in _get_chunks(files, 10):
22 dos2unix = cli.run(['dos2unix', *chunk])
23
24 if dos2unix.returncode:
25 return False
26
27
28@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
29@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all files.')
30@cli.argument('files', nargs='*', arg_only=True, type=normpath, help='Filename(s) to format.')
8@cli.subcommand("Ensure text files have the proper line endings.", hidden=True) 31@cli.subcommand("Ensure text files have the proper line endings.", hidden=True)
9def format_text(cli): 32def format_text(cli):
10 """Ensure text files have the proper line endings. 33 """Ensure text files have the proper line endings.
11 """ 34 """
12 try: 35 # Find the list of files to format
13 file_list_cmd = cli.run(['git', 'ls-files', '-z'], check=True) 36 if cli.args.files:
14 except CalledProcessError as e: 37 files = list(cli.args.files)
15 cli.log.error('Could not get file list: %s', e) 38
16 exit(1) 39 if cli.args.all_files:
17 except Exception as e: 40 cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
18 cli.log.error('Unhandled exception: %s: %s', e.__class__.__name__, e) 41
19 cli.log.exception(e) 42 elif cli.args.all_files:
20 exit(1) 43 git_ls_cmd = ['git', 'ls-files']
21 44 git_ls = cli.run(git_ls_cmd, stdin=DEVNULL)
22 dos2unix = cli.run(['xargs', '-0', 'dos2unix'], stdin=None, input=file_list_cmd.stdout) 45 files = list(filter(None, git_ls.stdout.split('\n')))
23 46
24 if dos2unix.returncode != 0: 47 else:
25 print(dos2unix.stderr) 48 git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch]
26 49 git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)
27 return dos2unix.returncode 50 files = list(filter(None, git_diff.stdout.split('\n')))
51
52 # Sanity check
53 if not files:
54 cli.log.error('No changed files detected. Use "qmk format-text -a" to format all files')
55 return False
56
57 return dos2unix_run(files)