1#!/usr/bin/python3 2# 3# Copyright 2017, The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# TODO We should unify this with build/Android.cpplint.mk. 18 19import os 20import pathlib 21import subprocess 22import sys 23 24IGNORED_FILES = {"runtime/elf.h", "runtime/openjdkjvmti/include/jvmti.h"} 25 26INTERESTING_SUFFIXES = {".h", ".cc"} 27 28CPPLINT_FLAGS = [ 29 '--filter=-whitespace/line_length,-build/include,-readability/function,-readability/streams,-readability/todo,-runtime/references,-runtime/sizeof,-runtime/threadsafe_fn,-runtime/printf', 30 '--quiet', 31] 32 33def is_interesting(f): 34 """ 35 Returns true if this is a file we want to run through cpplint before uploading. False otherwise. 36 """ 37 path = pathlib.Path(f) 38 return f not in IGNORED_FILES and path.suffix in INTERESTING_SUFFIXES and path.exists() 39 40def get_changed_files(commit): 41 """ 42 Gets the files changed in the given commit. 43 """ 44 return subprocess.check_output( 45 ["git", 'diff-tree', '--no-commit-id', '--name-only', '-r', commit], 46 stderr=subprocess.STDOUT, 47 universal_newlines=True).split() 48 49def run_cpplint(files): 50 """ 51 Runs cpplint on the given files. 52 """ 53 if len(files) == 0: 54 return 55 sys.exit(subprocess.call(['tools/cpplint.py'] + CPPLINT_FLAGS + files)) 56 57def main(): 58 if 'PREUPLOAD_COMMIT' in os.environ: 59 commit = os.environ['PREUPLOAD_COMMIT'] 60 else: 61 print("WARNING: Not running as a pre-upload hook. Assuming commit to check = 'HEAD'") 62 commit = "HEAD" 63 files_to_check = [f for f in get_changed_files(commit) if is_interesting(f)] 64 run_cpplint(files_to_check) 65 66if __name__ == '__main__': 67 main() 68