Merge pull request #11466 from electron/cppcheck-diff-mode

add changed-files-only mode to cpplint
This commit is contained in:
Charles Kerr 2017-12-20 10:55:49 -06:00 committed by GitHub
commit d3eeb84208
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

120
script/cpplint.py vendored
View file

@ -1,73 +1,101 @@
#!/usr/bin/env python #!/usr/bin/env python
import fnmatch import argparse
import os import os
import sys import sys
from lib.config import enable_verbose_mode
from lib.util import execute from lib.util import execute
IGNORE_FILES = [ IGNORE_FILES = set(os.path.join(*components) for components in [
os.path.join('atom', 'browser', 'mac', 'atom_application.h'), ['atom', 'browser', 'mac', 'atom_application.h'],
os.path.join('atom', 'browser', 'mac', 'atom_application_delegate.h'), ['atom', 'browser', 'mac', 'atom_application_delegate.h'],
os.path.join('atom', 'browser', 'resources', 'win', 'resource.h'), ['atom', 'browser', 'resources', 'win', 'resource.h'],
os.path.join('atom', 'browser', 'ui', 'cocoa', 'atom_menu_controller.h'), ['atom', 'browser', 'ui', 'cocoa', 'atom_menu_controller.h'],
os.path.join('atom', 'browser', 'ui', 'cocoa', 'atom_touch_bar.h'), ['atom', 'browser', 'ui', 'cocoa', 'atom_touch_bar.h'],
os.path.join('atom', 'browser', 'ui', 'cocoa', ['atom', 'browser', 'ui', 'cocoa', 'touch_bar_forward_declarations.h'],
'touch_bar_forward_declarations.h'), ['atom', 'browser', 'ui', 'cocoa', 'NSColor+Hex.h'],
os.path.join('atom', 'browser', 'ui', 'cocoa', 'NSColor+Hex.h'), ['atom', 'browser', 'ui', 'cocoa', 'NSString+ANSI.h'],
os.path.join('atom', 'browser', 'ui', 'cocoa', 'NSString+ANSI.h'), ['atom', 'common', 'api', 'api_messages.h'],
os.path.join('atom', 'common', 'api', 'api_messages.h'), ['atom', 'common', 'common_message_generator.cc'],
os.path.join('atom', 'common', 'common_message_generator.cc'), ['atom', 'common', 'common_message_generator.h'],
os.path.join('atom', 'common', 'common_message_generator.h'), ['atom', 'node', 'osfhandle.cc'],
os.path.join('brightray', 'browser', 'mac', ['brightray', 'browser', 'mac', 'bry_inspectable_web_contents_view.h'],
'bry_inspectable_web_contents_view.h'), ['brightray', 'browser', 'mac', 'event_dispatching_window.h'],
os.path.join('brightray', 'browser', 'mac', 'event_dispatching_window.h'), ['brightray', 'browser', 'mac', 'notification_center_delegate.h'],
os.path.join('brightray', 'browser', 'mac', ['brightray', 'browser', 'win', 'notification_presenter_win7.h'],
'notification_center_delegate.h'), ['brightray', 'browser', 'win', 'win32_desktop_notifications', 'common.h'],
os.path.join('brightray', 'browser', 'win', 'notification_presenter_win7.h'), ['brightray', 'browser', 'win', 'win32_desktop_notifications',
os.path.join('brightray', 'browser', 'win', 'win32_desktop_notifications', 'desktop_notification_controller.cc'],
'common.h'), ['brightray', 'browser', 'win', 'win32_desktop_notifications',
os.path.join('brightray', 'browser', 'win', 'win32_desktop_notifications', 'desktop_notification_controller.h'],
'desktop_notification_controller.cc'), ['brightray', 'browser', 'win', 'win32_desktop_notifications', 'toast.h'],
os.path.join('brightray', 'browser', 'win', 'win32_desktop_notifications', ['brightray', 'browser', 'win', 'win32_notification.h']
'desktop_notification_controller.h'), ])
os.path.join('brightray', 'browser', 'win', 'win32_desktop_notifications',
'toast.h'),
os.path.join('brightray', 'browser', 'win', 'win32_notification.h')
]
SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
def main(): def main():
parser = argparse.ArgumentParser(
description="Run cpplint on Electron's C++ files",
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument(
'-c', '--only-changed',
action='store_true',
default=False,
dest='only_changed',
help='only run on changed files'
)
parser.add_argument(
'-v', '--verbose',
action='store_true',
default=False,
dest='verbose',
help='show cpplint output'
)
args = parser.parse_args()
if not os.path.isfile(cpplint_path()): if not os.path.isfile(cpplint_path()):
print("[INFO] Skipping cpplint, dependencies has not been bootstrapped") print("[INFO] Skipping cpplint, dependencies has not been bootstrapped")
return return
if args.verbose:
enable_verbose_mode()
os.chdir(SOURCE_ROOT) os.chdir(SOURCE_ROOT)
atom_files = list_files('atom', files = find_files(['atom', 'brightray'], is_cpp_file)
['app', 'browser', 'common', 'renderer', 'utility'], files -= IGNORE_FILES
['*.cc', '*.h']) if args.only_changed:
call_cpplint(list(set(atom_files) - set(IGNORE_FILES))) files &= find_changed_files()
call_cpplint(files)
brightray_files = list_files('brightray', ['browser', 'common'],
['*.cc', '*.h'])
call_cpplint(list(set(brightray_files) - set(IGNORE_FILES)))
def list_files(parent, directories, filters): def find_files(roots, test):
matches = [] matches = set()
for directory in directories: for root in roots:
for root, _, filenames, in os.walk(os.path.join(parent, directory)): for parent, _, children, in os.walk(root):
for f in filters: for child in children:
for filename in fnmatch.filter(filenames, f): filename = os.path.join(parent, child)
matches.append(os.path.join(root, filename)) if test(filename):
matches.add(filename)
return matches return matches
def is_cpp_file(filename):
return filename.endswith('.cc') or filename.endswith('.h')
def find_changed_files():
return set(execute(['git', 'diff', '--name-only']).splitlines())
def call_cpplint(files): def call_cpplint(files):
if files:
cpplint = cpplint_path() cpplint = cpplint_path()
execute([sys.executable, cpplint] + files) execute([sys.executable, cpplint] + list(files))
def cpplint_path(): def cpplint_path():