text.py (1918B)
1 """Ensure text files have the proper line endings. 2 """ 3 from itertools import islice 4 from subprocess import DEVNULL 5 6 from milc import cli 7 8 from qmk.path import normpath 9 10 11 def _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 18 def dos2unix_run(files): 19 """Spawn multiple dos2unix subprocess avoiding too long commands on formatting everything 20 """ 21 for chunk in _get_chunks([normpath(file).as_posix() for file in 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.') 31 @cli.subcommand("Ensure text files have the proper line endings.", hidden=True) 32 def format_text(cli): 33 """Ensure text files have the proper line endings. 34 """ 35 # Find the list of files to format 36 if cli.args.files: 37 files = list(cli.args.files) 38 39 if cli.args.all_files: 40 cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files))) 41 42 elif cli.args.all_files: 43 git_ls_cmd = ['git', 'ls-files'] 44 git_ls = cli.run(git_ls_cmd, stdin=DEVNULL) 45 files = list(filter(None, git_ls.stdout.split('\n'))) 46 47 else: 48 git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch] 49 git_diff = cli.run(git_diff_cmd, stdin=DEVNULL) 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)