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