Add license check to scripts/src_util/check_all.py
authorPyry Haulos <phaulos@google.com>
Fri, 11 Nov 2016 15:57:59 +0000 (07:57 -0800)
committerPyry Haulos <phaulos@google.com>
Fri, 11 Nov 2016 16:06:56 +0000 (08:06 -0800)
Change-Id: Id63ff1e351d01bc72253f298e373bfba963e2735

scripts/src_util/check_all.py
scripts/src_util/check_license.py [new file with mode: 0644]

index f666a78..7faabf1 100644 (file)
@@ -25,6 +25,7 @@ from argparse import ArgumentParser
 from common import getChangedFiles, getAllProjectFiles
 from check_include_guards import checkIncludeGuards
 from check_whitespace import checkWhitespace
+from check_license import checkLicense
 
 if __name__ == "__main__":
     parser = ArgumentParser()
@@ -41,6 +42,7 @@ if __name__ == "__main__":
     error = not all([
         checkWhitespace(files),
         checkIncludeGuards(files),
+        checkLicense(files),
         #todo checkRedundantIncludeGuards(files),
         ])
 
diff --git a/scripts/src_util/check_license.py b/scripts/src_util/check_license.py
new file mode 100644 (file)
index 0000000..bc5a4e1
--- /dev/null
@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+
+#-------------------------------------------------------------------------
+# drawElements Quality Program utilities
+# --------------------------------------
+#
+# Copyright 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#-------------------------------------------------------------------------
+
+import sys
+from common import isTextFile
+from fnmatch import fnmatch
+
+LICENSE_APACHE2  = 0
+LICENSE_MIT      = 1
+LICENSE_MULTIPLE = 2
+LICENSE_UNKNOWN  = 3
+
+LICENSE_KEYS   = [
+       # \note Defined this way to avoid triggering license check error on this file
+       ("P" + "ermission is hereby granted, free of charge",    LICENSE_MIT),
+       ("L" + "icensed under the Apache License, Version 2.0",  LICENSE_APACHE2),
+]
+
+SOURCE_FILES   = ["*.py", "*.java", "*.c", "*.h", "*.cpp", "*.hpp"]
+
+def readFile (file):
+       f = open(file, 'rb')
+       c = f.read()
+       f.close()
+       return c
+
+def getFileLicense (file):
+       contents        = readFile(file)
+       detected        = LICENSE_UNKNOWN
+
+       for searchStr, license in LICENSE_KEYS:
+               if contents.find(searchStr) != -1:
+                       if detected != LICENSE_UNKNOWN:
+                               detected = LICENSE_MULTIPLE
+                       else:
+                               detected = license
+
+       return detected
+
+def checkFileLicense (file):
+       license = getFileLicense(file)
+
+       if license == LICENSE_MIT:
+               print "%s: contains MIT license" % file
+       elif license == LICENSE_MULTIPLE:
+               print "%s: contains multiple licenses" % file
+       elif license == LICENSE_UNKNOWN:
+               print "%s: missing/unknown license"
+
+       return license == LICENSE_APACHE2
+
+def isSourceFile (file):
+       for ptrn in SOURCE_FILES:
+               if fnmatch(file, ptrn):
+                       return True
+       return False
+
+def checkLicense (files):
+       error = False
+       for file in files:
+               if isTextFile(file) and isSourceFile(file):
+                       if not checkFileLicense(file):
+                               error = True
+
+       return not error