1*35238bceSAndroid Build Coastguard Worker# -*- coding: utf-8 -*- 2*35238bceSAndroid Build Coastguard Worker 3*35238bceSAndroid Build Coastguard Worker#------------------------------------------------------------------------- 4*35238bceSAndroid Build Coastguard Worker# drawElements Quality Program utilities 5*35238bceSAndroid Build Coastguard Worker# -------------------------------------- 6*35238bceSAndroid Build Coastguard Worker# 7*35238bceSAndroid Build Coastguard Worker# Copyright 2016 The Android Open Source Project 8*35238bceSAndroid Build Coastguard Worker# 9*35238bceSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 10*35238bceSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 11*35238bceSAndroid Build Coastguard Worker# You may obtain a copy of the License at 12*35238bceSAndroid Build Coastguard Worker# 13*35238bceSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 14*35238bceSAndroid Build Coastguard Worker# 15*35238bceSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 16*35238bceSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 17*35238bceSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18*35238bceSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 19*35238bceSAndroid Build Coastguard Worker# limitations under the License. 20*35238bceSAndroid Build Coastguard Worker# 21*35238bceSAndroid Build Coastguard Worker#------------------------------------------------------------------------- 22*35238bceSAndroid Build Coastguard Worker 23*35238bceSAndroid Build Coastguard Workerimport sys 24*35238bceSAndroid Build Coastguard Workerfrom common import isTextFile 25*35238bceSAndroid Build Coastguard Workerfrom fnmatch import fnmatch 26*35238bceSAndroid Build Coastguard Worker 27*35238bceSAndroid Build Coastguard WorkerLICENSE_APACHE2 = 0 28*35238bceSAndroid Build Coastguard WorkerLICENSE_MIT = 1 29*35238bceSAndroid Build Coastguard WorkerLICENSE_MULTIPLE = 2 30*35238bceSAndroid Build Coastguard WorkerLICENSE_UNKNOWN = 3 31*35238bceSAndroid Build Coastguard Worker 32*35238bceSAndroid Build Coastguard WorkerLICENSE_KEYS = [ 33*35238bceSAndroid Build Coastguard Worker # \note Defined this way to avoid triggering license check error on this file 34*35238bceSAndroid Build Coastguard Worker ("P" + "ermission is hereby granted, free of charge", LICENSE_MIT), 35*35238bceSAndroid Build Coastguard Worker ("L" + "icensed under the Apache License, Version 2.0", LICENSE_APACHE2), 36*35238bceSAndroid Build Coastguard Worker] 37*35238bceSAndroid Build Coastguard Worker 38*35238bceSAndroid Build Coastguard WorkerSOURCE_FILES = ["*.py", "*.java", "*.c", "*.h", "*.cpp", "*.hpp"] 39*35238bceSAndroid Build Coastguard Worker 40*35238bceSAndroid Build Coastguard Workerdef readFile (file): 41*35238bceSAndroid Build Coastguard Worker f = open(file, 'rt') 42*35238bceSAndroid Build Coastguard Worker c = f.read() 43*35238bceSAndroid Build Coastguard Worker f.close() 44*35238bceSAndroid Build Coastguard Worker return c 45*35238bceSAndroid Build Coastguard Worker 46*35238bceSAndroid Build Coastguard Workerdef getFileLicense (file): 47*35238bceSAndroid Build Coastguard Worker contents = readFile(file) 48*35238bceSAndroid Build Coastguard Worker detected = LICENSE_UNKNOWN 49*35238bceSAndroid Build Coastguard Worker 50*35238bceSAndroid Build Coastguard Worker for searchStr, license in LICENSE_KEYS: 51*35238bceSAndroid Build Coastguard Worker if contents.find(searchStr) != -1: 52*35238bceSAndroid Build Coastguard Worker if detected != LICENSE_UNKNOWN: 53*35238bceSAndroid Build Coastguard Worker detected = LICENSE_MULTIPLE 54*35238bceSAndroid Build Coastguard Worker else: 55*35238bceSAndroid Build Coastguard Worker detected = license 56*35238bceSAndroid Build Coastguard Worker 57*35238bceSAndroid Build Coastguard Worker return detected 58*35238bceSAndroid Build Coastguard Worker 59*35238bceSAndroid Build Coastguard Workerdef checkFileLicense (file): 60*35238bceSAndroid Build Coastguard Worker license = getFileLicense(file) 61*35238bceSAndroid Build Coastguard Worker 62*35238bceSAndroid Build Coastguard Worker if license == LICENSE_MIT: 63*35238bceSAndroid Build Coastguard Worker print("%s: contains MIT license" % file) 64*35238bceSAndroid Build Coastguard Worker elif license == LICENSE_MULTIPLE: 65*35238bceSAndroid Build Coastguard Worker print("%s: contains multiple licenses" % file) 66*35238bceSAndroid Build Coastguard Worker elif license == LICENSE_UNKNOWN: 67*35238bceSAndroid Build Coastguard Worker print("%s: missing/unknown license" % file) 68*35238bceSAndroid Build Coastguard Worker 69*35238bceSAndroid Build Coastguard Worker return license == LICENSE_APACHE2 70*35238bceSAndroid Build Coastguard Worker 71*35238bceSAndroid Build Coastguard Workerdef isSourceFile (file): 72*35238bceSAndroid Build Coastguard Worker for ptrn in SOURCE_FILES: 73*35238bceSAndroid Build Coastguard Worker if fnmatch(file, ptrn): 74*35238bceSAndroid Build Coastguard Worker return True 75*35238bceSAndroid Build Coastguard Worker return False 76*35238bceSAndroid Build Coastguard Worker 77*35238bceSAndroid Build Coastguard Workerdef checkLicense (files): 78*35238bceSAndroid Build Coastguard Worker error = False 79*35238bceSAndroid Build Coastguard Worker for file in files: 80*35238bceSAndroid Build Coastguard Worker if isTextFile(file) and isSourceFile(file): 81*35238bceSAndroid Build Coastguard Worker if not checkFileLicense(file): 82*35238bceSAndroid Build Coastguard Worker error = True 83*35238bceSAndroid Build Coastguard Worker 84*35238bceSAndroid Build Coastguard Worker return not error 85