cformat.py 901 B

123456789101112131415161718192021222324252627
  1. """Format C code according to QMK's style.
  2. """
  3. import os
  4. import subprocess
  5. from milc import cli
  6. @cli.entrypoint("Format C code according to QMK's style.")
  7. def main(cli):
  8. """Format C code according to QMK's style.
  9. """
  10. clang_format = ['clang-format', '-i']
  11. code_files = []
  12. for dir in ['drivers', 'quantum', 'tests', 'tmk_core']:
  13. for dirpath, dirnames, filenames in os.walk(dir):
  14. if 'tmk_core/protocol/usb_hid' in dirpath:
  15. continue
  16. for name in filenames:
  17. if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'):
  18. code_files.append(os.path.join(dirpath, name))
  19. try:
  20. subprocess.run(clang_format + code_files, check=True)
  21. cli.log.info('Successfully formatted the C code.')
  22. except subprocess.CalledProcessError:
  23. cli.log.error('Error formatting C code!')