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