c.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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', 'hpp')
  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. for file in files:
  58. if core_only:
  59. # The following statement checks each file to see if the file path is
  60. # - in the core directories
  61. # - not in the ignored directories
  62. if not any(is_relative_to(file, i) for i in core_dirs) or any(is_relative_to(file, i) for i in ignored):
  63. cli.log.debug("Skipping non-core file %s, as '--core-only' is used.", file)
  64. continue
  65. if file.suffix[1:] in c_file_suffixes:
  66. yield file
  67. else:
  68. cli.log.debug('Skipping file %s', file)
  69. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.")
  70. @cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
  71. @cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.')
  72. @cli.argument('--core-only', arg_only=True, action='store_true', help='Format core files only.')
  73. @cli.argument('files', nargs='*', arg_only=True, type=normpath, completer=FilesCompleter('.c'), help='Filename(s) to format.')
  74. @cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True)
  75. def format_c(cli):
  76. """Format C code according to QMK's style.
  77. """
  78. # Find the list of files to format
  79. if cli.args.files:
  80. files = list(filter_files(cli.args.files, cli.args.core_only))
  81. if not files:
  82. cli.log.error('No C files in filelist: %s', ', '.join(map(str, cli.args.files)))
  83. exit(0)
  84. if cli.args.all_files:
  85. cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
  86. elif cli.args.all_files:
  87. all_files = c_source_files(core_dirs)
  88. files = list(filter_files(all_files, True))
  89. else:
  90. git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *core_dirs]
  91. git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)
  92. if git_diff.returncode != 0:
  93. cli.log.error("Error running %s", git_diff_cmd)
  94. print(git_diff.stderr)
  95. return git_diff.returncode
  96. changed_files = git_diff.stdout.strip().split('\n')
  97. files = list(filter_files(changed_files, True))
  98. # Sanity check
  99. if not files:
  100. cli.log.error('No changed files detected. Use "qmk format-c -a" to format all core files')
  101. return False
  102. # Run clang-format on the files we've found
  103. if cli.args.dry_run:
  104. return not find_diffs(files)
  105. else:
  106. return cformat_run(files)