cpplint.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. import argparse
  3. import os
  4. import sys
  5. from lib.config import enable_verbose_mode
  6. from lib.util import execute
  7. IGNORE_FILES = set(os.path.join(*components) for components in [
  8. ['atom', 'browser', 'mac', 'atom_application.h'],
  9. ['atom', 'browser', 'mac', 'atom_application_delegate.h'],
  10. ['atom', 'browser', 'resources', 'win', 'resource.h'],
  11. ['atom', 'browser', 'ui', 'cocoa', 'atom_menu_controller.h'],
  12. ['atom', 'browser', 'ui', 'cocoa', 'atom_ns_window.h'],
  13. ['atom', 'browser', 'ui', 'cocoa', 'atom_ns_window_delegate.h'],
  14. ['atom', 'browser', 'ui', 'cocoa', 'atom_preview_item.h'],
  15. ['atom', 'browser', 'ui', 'cocoa', 'atom_touch_bar.h'],
  16. ['atom', 'browser', 'ui', 'cocoa', 'touch_bar_forward_declarations.h'],
  17. ['atom', 'browser', 'ui', 'cocoa', 'NSColor+Hex.h'],
  18. ['atom', 'browser', 'ui', 'cocoa', 'NSString+ANSI.h'],
  19. ['atom', 'common', 'api', 'api_messages.h'],
  20. ['atom', 'common', 'common_message_generator.cc'],
  21. ['atom', 'common', 'common_message_generator.h'],
  22. ['atom', 'common', 'node_includes.h'],
  23. ['atom', 'node', 'osfhandle.cc'],
  24. ['brightray', 'browser', 'mac', 'bry_inspectable_web_contents_view.h'],
  25. ['brightray', 'browser', 'mac', 'event_dispatching_window.h'],
  26. ['brightray', 'browser', 'mac', 'notification_center_delegate.h'],
  27. ['brightray', 'browser', 'win', 'notification_presenter_win7.h'],
  28. ['brightray', 'browser', 'win', 'win32_desktop_notifications', 'common.h'],
  29. ['brightray', 'browser', 'win', 'win32_desktop_notifications',
  30. 'desktop_notification_controller.cc'],
  31. ['brightray', 'browser', 'win', 'win32_desktop_notifications',
  32. 'desktop_notification_controller.h'],
  33. ['brightray', 'browser', 'win', 'win32_desktop_notifications', 'toast.h'],
  34. ['brightray', 'browser', 'win', 'win32_notification.h']
  35. ])
  36. SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
  37. def main():
  38. parser = argparse.ArgumentParser(
  39. description="Run cpplint on Electron's C++ files",
  40. formatter_class=argparse.RawTextHelpFormatter
  41. )
  42. parser.add_argument(
  43. '-c', '--only-changed',
  44. action='store_true',
  45. default=False,
  46. dest='only_changed',
  47. help='only run on changed files'
  48. )
  49. parser.add_argument(
  50. '-v', '--verbose',
  51. action='store_true',
  52. default=False,
  53. dest='verbose',
  54. help='show cpplint output'
  55. )
  56. args = parser.parse_args()
  57. if not os.path.isfile(cpplint_path()):
  58. print("[INFO] Skipping cpplint, dependencies has not been bootstrapped")
  59. return
  60. if args.verbose:
  61. enable_verbose_mode()
  62. os.chdir(SOURCE_ROOT)
  63. files = find_files(['atom', 'brightray'], is_cpp_file)
  64. files -= IGNORE_FILES
  65. if args.only_changed:
  66. files &= find_changed_files()
  67. call_cpplint(files)
  68. def find_files(roots, test):
  69. matches = set()
  70. for root in roots:
  71. for parent, _, children, in os.walk(root):
  72. for child in children:
  73. filename = os.path.join(parent, child)
  74. if test(filename):
  75. matches.add(filename)
  76. return matches
  77. def is_cpp_file(filename):
  78. return filename.endswith('.cc') or filename.endswith('.h')
  79. def find_changed_files():
  80. return set(execute(['git', 'diff', '--name-only']).splitlines())
  81. def call_cpplint(files):
  82. if files:
  83. cpplint = cpplint_path()
  84. execute([sys.executable, cpplint] + list(files))
  85. def cpplint_path():
  86. return os.path.join(SOURCE_ROOT, 'vendor', 'depot_tools', 'cpplint.py')
  87. if __name__ == '__main__':
  88. sys.exit(main())