c.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """Format C code according to QMK's style.
  2. """
  3. from shutil import which
  4. from subprocess import CalledProcessError, DEVNULL, Popen, PIPE
  5. from argcomplete.completers import FilesCompleter
  6. from milc import cli
  7. from qmk.path import normpath
  8. from qmk.c_parse import c_source_files
  9. c_file_suffixes = ('c', 'h', 'cpp')
  10. core_dirs = ('drivers', 'quantum', 'tests', 'tmk_core', 'platforms')
  11. ignored = ('tmk_core/protocol/usb_hid', 'platforms/chibios/boards')
  12. def is_relative_to(file, other):
  13. """Provide similar behavior to PurePath.is_relative_to in Python > 3.9
  14. """
  15. return str(normpath(file).resolve()).startswith(str(normpath(other).resolve()))
  16. def find_clang_format():
  17. """Returns the path to clang-format.
  18. """
  19. for clang_version in range(20, 6, -1):
  20. binary = f'clang-format-{clang_version}'
  21. if which(binary):
  22. return binary
  23. return 'clang-format'
  24. def find_diffs(files):
  25. """Run clang-format and diff it against a file.
  26. """
  27. found_diffs = False
  28. for file in files:
  29. cli.log.debug('Checking for changes in %s', file)
  30. clang_format = Popen([find_clang_format(), file], stdout=PIPE, stderr=PIPE, universal_newlines=True)
  31. diff = cli.run(['diff', '-u', f'--label=a/{file}', f'--label=b/{file}', str(file), '-'], stdin=clang_format.stdout, capture_output=True)
  32. if diff.returncode != 0:
  33. print(diff.stdout)
  34. found_diffs = True
  35. return found_diffs
  36. def cformat_run(files):
  37. """Spawn clang-format subprocess with proper arguments
  38. """
  39. # Determine which version of clang-format to use
  40. clang_format = [find_clang_format(), '-i']
  41. try:
  42. cli.run([*clang_format, *map(str, files)], check=True, capture_output=False, stdin=DEVNULL)
  43. cli.log.info('Successfully formatted the C code.')
  44. return True
  45. except CalledProcessError as e:
  46. cli.log.error('Error formatting C code!')
  47. cli.log.debug('%s exited with returncode %s', e.cmd, e.returncode)
  48. cli.log.debug('STDOUT:')
  49. cli.log.debug(e.stdout)
  50. cli.log.debug('STDERR:')
  51. cli.log.debug(e.stderr)
  52. return False
  53. def filter_files(files, core_only=False):
  54. """Yield only files to be formatted and skip the rest
  55. """
  56. files = list(map(normpath, filter(None, files)))
  57. if core_only:
  58. # Filter non-core files
  59. for index, file in enumerate(files):
  60. # The following statement checks each file to see if the file path is
  61. # - in the core directories
  62. # - not in the ignored directories
  63. if not any(is_relative_to(file, i) for i in core_dirs) or any(is_relative_to(file, i) for i in ignored):
  64. del files[index]
  65. cli.log.debug("Skipping non-core file %s, as '--core-only' is used.", file)
  66. for file in files:
  67. if file.suffix[1:] in c_file_suffixes:
  68. yield file
  69. else:
  70. cli.log.debug('Skipping file %s', file)
  71. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.")
  72. @cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
  73. @cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.')
  74. @cli.argument('--core-only', arg_only=True, action='store_true', help='Format core files only.')
  75. @cli.argument('files', nargs='*', arg_only=True, type=normpath, completer=FilesCompleter('.c'), help='Filename(s) to format.')
  76. @cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True)
  77. def format_c(cli):
  78. """Format C code according to QMK's style.
  79. """
  80. # Find the list of files to format
  81. if cli.args.files:
  82. files = list(filter_files(cli.args.files, cli.args.core_only))
  83. if not files:
  84. cli.log.error('No C files in filelist: %s', ', '.join(map(str, cli.args.files)))
  85. exit(0)
  86. if cli.args.all_files:
  87. cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
  88. elif cli.args.all_files:
  89. all_files = c_source_files(core_dirs)
  90. files = list(filter_files(all_files, True))
  91. else:
  92. git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *core_dirs]
  93. git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)
  94. if git_diff.returncode != 0:
  95. cli.log.error("Error running %s", git_diff_cmd)
  96. print(git_diff.stderr)
  97. return git_diff.returncode
  98. changed_files = git_diff.stdout.strip().split('\n')
  99. files = list(filter_files(changed_files, True))
  100. # Sanity check
  101. if not files:
  102. cli.log.error('No changed files detected. Use "qmk format-c -a" to format all core files')
  103. return False
  104. # Run clang-format on the files we've found
  105. if cli.args.dry_run:
  106. return not find_diffs(files)
  107. else:
  108. return cformat_run(files)