]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/PatchCheck.py: Extract email check code to EmailAddressCheck
authorPhilippe Mathieu-Daude <philmd@redhat.com>
Thu, 9 Jan 2020 10:55:43 +0000 (18:55 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 10 Jan 2020 04:06:42 +0000 (04:06 +0000)
As we are going to reuse this code out of the CommitMessageCheck
class, extract it in a new class: EmailAddressCheck.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Reviewed-by: Bob Feng <bob.c.feng@intel.com>
Signed-off-by: Philippe Mathieu-Daude <philmd@redhat.com>
BaseTools/Scripts/PatchCheck.py

index 9668025798da2ba0a3523ad3bb3b017a348b2343..3b6d77081e7e08ae852d6e5a1d2d09bf7ca28dda 100755 (executable)
@@ -2,6 +2,7 @@
 #  Check a patch for various format issues\r
 #\r
 #  Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (C) 2020, Red Hat, Inc.<BR>\r
 #\r
 #  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
@@ -22,6 +23,58 @@ class Verbose:
     SILENT, ONELINE, NORMAL = range(3)\r
     level = NORMAL\r
 \r
+class EmailAddressCheck:\r
+    """Checks an email address."""\r
+\r
+    def __init__(self, email):\r
+        self.ok = True\r
+\r
+        if email is None:\r
+            self.error('Email address is missing!')\r
+            return\r
+\r
+        self.check_email_address(email)\r
+\r
+    def error(self, *err):\r
+        if self.ok and Verbose.level > Verbose.ONELINE:\r
+            print('The email address is not valid:')\r
+        self.ok = False\r
+        if Verbose.level < Verbose.NORMAL:\r
+            return\r
+        count = 0\r
+        for line in err:\r
+            prefix = (' *', '  ')[count > 0]\r
+            print(prefix, line)\r
+            count += 1\r
+\r
+    email_re1 = re.compile(r'(?:\s*)(.*?)(\s*)<(.+)>\s*$',\r
+                           re.MULTILINE|re.IGNORECASE)\r
+\r
+    def check_email_address(self, email):\r
+        email = email.strip()\r
+        mo = self.email_re1.match(email)\r
+        if mo is None:\r
+            self.error("Email format is invalid: " + email.strip())\r
+            return\r
+\r
+        name = mo.group(1).strip()\r
+        if name == '':\r
+            self.error("Name is not provided with email address: " +\r
+                       email)\r
+        else:\r
+            quoted = len(name) > 2 and name[0] == '"' and name[-1] == '"'\r
+            if name.find(',') >= 0 and not quoted:\r
+                self.error('Add quotes (") around name with a comma: ' +\r
+                           name)\r
+\r
+        if mo.group(2) == '':\r
+            self.error("There should be a space between the name and " +\r
+                       "email address: " + email)\r
+\r
+        if mo.group(3).find(' ') >= 0:\r
+            self.error("The email address cannot contain a space: " +\r
+                       mo.group(3))\r
+\r
 class CommitMessageCheck:\r
     """Checks the contents of a git commit message."""\r
 \r
@@ -121,38 +174,10 @@ class CommitMessageCheck:
             if s[2] != ' ':\r
                 self.error("There should be a space after '" + sig + ":'")\r
 \r
-            self.check_email_address(s[3])\r
+            EmailAddressCheck(s[3])\r
 \r
         return sigs\r
 \r
-    email_re1 = re.compile(r'(?:\s*)(.*?)(\s*)<(.+)>\s*$',\r
-                           re.MULTILINE|re.IGNORECASE)\r
-\r
-    def check_email_address(self, email):\r
-        email = email.strip()\r
-        mo = self.email_re1.match(email)\r
-        if mo is None:\r
-            self.error("Email format is invalid: " + email.strip())\r
-            return\r
-\r
-        name = mo.group(1).strip()\r
-        if name == '':\r
-            self.error("Name is not provided with email address: " +\r
-                       email)\r
-        else:\r
-            quoted = len(name) > 2 and name[0] == '"' and name[-1] == '"'\r
-            if name.find(',') >= 0 and not quoted:\r
-                self.error('Add quotes (") around name with a comma: ' +\r
-                           name)\r
-\r
-        if mo.group(2) == '':\r
-            self.error("There should be a space between the name and " +\r
-                       "email address: " + email)\r
-\r
-        if mo.group(3).find(' ') >= 0:\r
-            self.error("The email address cannot contain a space: " +\r
-                       mo.group(3))\r
-\r
     def check_signed_off_by(self):\r
         sob='Signed-off-by'\r
         if self.msg.find(sob) < 0:\r