[edk2-devel] [PATCH v2] BaseTools/PatchCheck.py: Add LicenseCheck

Zhang, Shenglei posted 1 patch 3 years, 11 months ago
Failed in applying to current master (apply log)
There is a newer version of this series
BaseTools/Scripts/PatchCheck.py | 71 ++++++++++++++++++++++++++++++++-
1 file changed, 70 insertions(+), 1 deletion(-)
[edk2-devel] [PATCH v2] BaseTools/PatchCheck.py: Add LicenseCheck
Posted by Zhang, Shenglei 3 years, 11 months ago
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2691
For files to be added to the tree, this feature will check
whether it has BSD plus patent license. If not, licenses listed in
Readme are also accepted but warning will be reported out.
Otherwise, it should be error.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Shenglei Zhang <shenglei.zhang@intel.com>
---

v2: Update handling methods for different licenses.

 BaseTools/Scripts/PatchCheck.py | 71 ++++++++++++++++++++++++++++++++-
 1 file changed, 70 insertions(+), 1 deletion(-)

diff --git a/BaseTools/Scripts/PatchCheck.py b/BaseTools/Scripts/PatchCheck.py
index 13da6967785d..c96009034aa7 100755
--- a/BaseTools/Scripts/PatchCheck.py
+++ b/BaseTools/Scripts/PatchCheck.py
@@ -491,6 +491,72 @@ class GitDiffCheck:
             print(prefix, line)
             count += 1
 
+class LicenseCheck():
+
+    def __init__(self, diff):
+        self.ok = True
+        self.startcheck = False
+        self.license = True
+        lines = diff.splitlines(True)
+        count = len(lines)
+        line_index = 0
+        for line in lines:
+            if line.startswith('--- /dev/null'):
+                nextline = lines[line_index + 1]
+                added_file = self.Readdedfileformat.search(nextline).group(1)
+                added_file_extension = os.path.splitext(added_file)[1]
+                if added_file_extension in self.file_extension_list:
+                    self.startcheck = True
+                    self.license = False
+            if self.startcheck and self.license_format_preflix in line:
+                if self.bsd2_patent in line or self.bsd3_patent in line:
+                    self.license = True
+                else:
+                    for optional_license in self.license_optional_list:
+                        if optional_license in line:
+                            self.license = True
+                            self.warning(added_file)                            
+            if line_index + 1 == count or lines[line_index + 1].startswith('diff --') and self.startcheck:
+                if not self.license:
+                    self.error(added_file)
+                self.startcheck = False
+                self.license = True
+            line_index = line_index + 1
+
+    def warning(self, *err):
+        count = 0
+        for line in err:
+            prefix = (' *', '  ')[count > 0]
+            error_format = 'Warning: License accepted but not BSD plus patent license in'
+            print(prefix, error_format, line)
+            count += 1
+
+    def error(self, *err):
+        if self.ok and Verbose.level > Verbose.ONELINE:
+            print('License error!')
+        self.ok = False
+        if Verbose.level < Verbose.NORMAL:
+            return
+        count = 0
+        for line in err:
+            prefix = (' *', '  ')[count > 0]
+            error_format = 'No accepted license in:'
+            print(prefix, error_format, line)
+            count += 1
+
+    license_format_preflix = 'SPDX-License-Identifier'
+
+    bsd2_patent = 'BSD-2-Clause-Patent'
+
+    bsd3_patent = 'BSD-3-Clause-Patent'
+
+    license_optional_list = ['BSD-2-Clause', 'BSD-3-Clause', 'MIT', 'Python-2.0', 'Zlib']
+
+    Readdedfileformat = re.compile(r'\+\+\+ b\/(.*)\n')
+
+    file_extension_list = [".c", ".h", ".inf", ".dsc", ".dec", ".py", ".bat", ".sh", ".uni", ".yaml", ".fdf", ".inc"]
+
+
 class CheckOnePatch:
     """Checks the contents of a git email formatted patch.
 
@@ -508,12 +574,15 @@ class CheckOnePatch:
         msg_check = CommitMessageCheck(self.commit_subject, self.commit_msg)
         msg_ok = msg_check.ok
 
+        license_check = LicenseCheck(self.diff)
+        license_ok = license_check.ok
+
         diff_ok = True
         if self.diff is not None:
             diff_check = GitDiffCheck(self.diff)
             diff_ok = diff_check.ok
 
-        self.ok = email_ok and msg_ok and diff_ok
+        self.ok = email_ok and msg_ok and diff_ok and license_ok
 
         if Verbose.level == Verbose.ONELINE:
             if self.ok:
-- 
2.18.0.windows.1


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#59272): https://edk2.groups.io/g/devel/message/59272
Mute This Topic: https://groups.io/mt/74156794/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-