[edk2] [PATCH] BaseTools/Ecc: Add some new checkpoints

Yonghong Zhu posted 1 patch 5 years, 9 months ago
Failed in applying to current master (apply log)
BaseTools/Source/Python/Ecc/Check.py         | 54 ++++++++++++++++++++++++++++
BaseTools/Source/Python/Ecc/Configuration.py |  4 +++
BaseTools/Source/Python/Ecc/EccToolError.py  |  8 +++--
BaseTools/Source/Python/Ecc/config.ini       |  4 +++
4 files changed, 68 insertions(+), 2 deletions(-)
[edk2] [PATCH] BaseTools/Ecc: Add some new checkpoints
Posted by Yonghong Zhu 5 years, 9 months ago
From: hchen30 <hesheng.chen@intel.com>

1. Add a checkpoint to check NO TABs.
2. Add a checkpoint to check line ending with CRLF.
3. Add a checkpoint to check no trailing spaces.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hess Chen <hesheng.chen@intel.com>
---
 BaseTools/Source/Python/Ecc/Check.py         | 54 ++++++++++++++++++++++++++++
 BaseTools/Source/Python/Ecc/Configuration.py |  4 +++
 BaseTools/Source/Python/Ecc/EccToolError.py  |  8 +++--
 BaseTools/Source/Python/Ecc/config.ini       |  4 +++
 4 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/BaseTools/Source/Python/Ecc/Check.py b/BaseTools/Source/Python/Ecc/Check.py
index 0b81013d77..6803afdfdd 100644
--- a/BaseTools/Source/Python/Ecc/Check.py
+++ b/BaseTools/Source/Python/Ecc/Check.py
@@ -188,6 +188,60 @@ class Check(object):
     def GeneralCheck(self):
         self.GeneralCheckNonAcsii()
         self.UniCheck()
+        self.GeneralCheckNoTab()
+        self.GeneralCheckLineEnding()
+        self.GeneralCheckTrailingWhiteSpaceLine()
+
+    # Check whether NO Tab is used, replaced with spaces
+    def GeneralCheckNoTab(self):
+        if EccGlobalData.gConfig.GeneralCheckNoTab == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
+            EdkLogger.quiet("Checking No TAB used in file ...")
+            SqlCommand = """select ID, FullPath, ExtName from File where ExtName in ('.dec', '.inf', '.dsc', 'c', 'h')"""
+            RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
+            for Record in RecordSet:
+                if Record[2].upper() not in EccGlobalData.gConfig.BinaryExtList:
+                    op = open(Record[1]).readlines()
+                    IndexOfLine = 0
+                    for Line in op:
+                        IndexOfLine += 1
+                        IndexOfChar = 0
+                        for Char in Line:
+                            IndexOfChar += 1
+                            if Char == '\t':
+                                OtherMsg = "File %s has TAB char at line %s column %s" % (Record[1], IndexOfLine, IndexOfChar)
+                                EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_NO_TAB, OtherMsg=OtherMsg, BelongsToTable='File', BelongsToItem=Record[0])
+
+    # Check Only use CRLF (Carriage Return Line Feed) line endings.
+    def GeneralCheckLineEnding(self):
+        if EccGlobalData.gConfig.GeneralCheckLineEnding == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
+            EdkLogger.quiet("Checking line ending in file ...")
+            SqlCommand = """select ID, FullPath, ExtName from File where ExtName in ('.dec', '.inf', '.dsc', 'c', 'h')"""
+            RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
+            for Record in RecordSet:
+                if Record[2].upper() not in EccGlobalData.gConfig.BinaryExtList:
+                    op = open(Record[1], 'rb').readlines()
+                    IndexOfLine = 0
+                    for Line in op:
+                        IndexOfLine += 1
+                        if not Line.endswith('\r\n'):
+                            OtherMsg = "File %s has invalid line ending at line %s" % (Record[1], IndexOfLine)
+                            EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_INVALID_LINE_ENDING, OtherMsg=OtherMsg, BelongsToTable='File', BelongsToItem=Record[0])
+
+    # Check if there is no trailing white space in one line.
+    def GeneralCheckTrailingWhiteSpaceLine(self):
+        if EccGlobalData.gConfig.GeneralCheckTrailingWhiteSpaceLine == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
+            EdkLogger.quiet("Checking trailing white space line in file ...")
+            SqlCommand = """select ID, FullPath, ExtName from File where ExtName in ('.dec', '.inf', '.dsc', 'c', 'h')"""
+            RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
+            for Record in RecordSet:
+                if Record[2].upper() not in EccGlobalData.gConfig.BinaryExtList:
+                    op = open(Record[1], 'rb').readlines()
+                    IndexOfLine = 0
+                    for Line in op:
+                        IndexOfLine += 1
+                        if Line.replace('\r', '').replace('\n', '').endswith(' '):
+                            OtherMsg = "File %s has trailing white spaces at line %s" % (Record[1], IndexOfLine)
+                            EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_TRAILING_WHITE_SPACE_LINE, OtherMsg=OtherMsg, BelongsToTable='File', BelongsToItem=Record[0])
 
     # Check whether file has non ACSII char
     def GeneralCheckNonAcsii(self):
diff --git a/BaseTools/Source/Python/Ecc/Configuration.py b/BaseTools/Source/Python/Ecc/Configuration.py
index 29a1220761..f58adbf736 100644
--- a/BaseTools/Source/Python/Ecc/Configuration.py
+++ b/BaseTools/Source/Python/Ecc/Configuration.py
@@ -186,6 +186,10 @@ class Configuration(object):
         self.GeneralCheckNonAcsii = 1
         # Check whether UNI file is valid
         self.GeneralCheckUni = 1
+        # Check Only use CRLF (Carriage Return Line Feed) line endings.
+        self.GeneralCheckLineEnding = 1
+        # Check if there is no trailing white space in one line.
+        self.GeneralCheckTrailingWhiteSpaceLine = 1
 
         ## Space Checking
         self.SpaceCheckAll = 1
diff --git a/BaseTools/Source/Python/Ecc/EccToolError.py b/BaseTools/Source/Python/Ecc/EccToolError.py
index 1d51da3ae1..ae0a31af8a 100644
--- a/BaseTools/Source/Python/Ecc/EccToolError.py
+++ b/BaseTools/Source/Python/Ecc/EccToolError.py
@@ -1,7 +1,7 @@
 ## @file
 # Standardized Error Hanlding infrastructures.
 #
-# Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
 # This program and the accompanying materials
 # are licensed and made available under the terms and conditions of the BSD License
 # which accompanies this distribution.  The full text of the license may be found at
@@ -22,6 +22,8 @@ ERROR_GENERAL_CHECK_FILE_EXISTENCE = 1007
 ERROR_GENERAL_CHECK_NON_ACSII = 1008
 ERROR_GENERAL_CHECK_UNI = 1009
 ERROR_GENERAL_CHECK_UNI_HELP_INFO = 1010
+ERROR_GENERAL_CHECK_INVALID_LINE_ENDING = 1011
+ERROR_GENERAL_CHECK_TRAILING_WHITE_SPACE_LINE = 1012
 
 ERROR_SPACE_CHECK_ALL = 2000
 
@@ -109,7 +111,7 @@ ERROR_SMM_COMM_PARA_CHECK_BUFFER_TYPE = 12001
 
 gEccErrorMessage = {
     ERROR_GENERAL_CHECK_ALL : "",
-    ERROR_GENERAL_CHECK_NO_TAB : "'TAB' character is not allowed in source code, please replace each 'TAB' with two spaces",
+    ERROR_GENERAL_CHECK_NO_TAB : "'TAB' character is not allowed in source code, please replace each 'TAB' with two spaces.",
     ERROR_GENERAL_CHECK_INDENTATION : "Indentation does not follow coding style",
     ERROR_GENERAL_CHECK_LINE : "The width of each line does not follow coding style",
     ERROR_GENERAL_CHECK_NO_ASM : "There should be no use of _asm in the source file",
@@ -119,6 +121,8 @@ gEccErrorMessage = {
     ERROR_GENERAL_CHECK_NON_ACSII : "File has invalid Non-ACSII char",
     ERROR_GENERAL_CHECK_UNI : "File is not a valid UTF-16 UNI file",
     ERROR_GENERAL_CHECK_UNI_HELP_INFO : "UNI file that is associated by INF or DEC file need define the prompt and help information.",
+    ERROR_GENERAL_CHECK_INVALID_LINE_ENDING : "Only CRLF (Carriage Return Line Feed) is allowed to line ending.",
+    ERROR_GENERAL_CHECK_TRAILING_WHITE_SPACE_LINE : "There should be no trailing white space in one line.",
 
     ERROR_SPACE_CHECK_ALL : "",
 
diff --git a/BaseTools/Source/Python/Ecc/config.ini b/BaseTools/Source/Python/Ecc/config.ini
index 9a431bf124..6c86da74d6 100644
--- a/BaseTools/Source/Python/Ecc/config.ini
+++ b/BaseTools/Source/Python/Ecc/config.ini
@@ -72,6 +72,10 @@ GeneralCheckFileExistence = 1
 GeneralCheckNonAcsii = 1
 # Check whether UNI file is valid
 GeneralCheckUni = 1
+# Check Only use CRLF (Carriage Return Line Feed) line endings.
+self.GeneralCheckLineEnding = 1
+# Check if there is no trailing white space in one line.
+self.GeneralCheckTrailingWhiteSpaceLine = 1
 
 #
 # Space Checking
-- 
2.14.2.windows.2

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [PATCH] BaseTools/Ecc: Add some new checkpoints
Posted by Zhu, Yonghong 5 years, 9 months ago
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com> 

Best Regards,
Zhu Yonghong


-----Original Message-----
From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Yonghong Zhu
Sent: Monday, July 23, 2018 2:03 PM
To: edk2-devel@lists.01.org
Cc: Chen, Hesheng <hesheng.chen@intel.com>
Subject: [edk2] [PATCH] BaseTools/Ecc: Add some new checkpoints

From: hchen30 <hesheng.chen@intel.com>

1. Add a checkpoint to check NO TABs.
2. Add a checkpoint to check line ending with CRLF.
3. Add a checkpoint to check no trailing spaces.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hess Chen <hesheng.chen@intel.com>
---
 BaseTools/Source/Python/Ecc/Check.py         | 54 ++++++++++++++++++++++++++++
 BaseTools/Source/Python/Ecc/Configuration.py |  4 +++  BaseTools/Source/Python/Ecc/EccToolError.py  |  8 +++--
 BaseTools/Source/Python/Ecc/config.ini       |  4 +++
 4 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/BaseTools/Source/Python/Ecc/Check.py b/BaseTools/Source/Python/Ecc/Check.py
index 0b81013d77..6803afdfdd 100644
--- a/BaseTools/Source/Python/Ecc/Check.py
+++ b/BaseTools/Source/Python/Ecc/Check.py
@@ -188,6 +188,60 @@ class Check(object):
     def GeneralCheck(self):
         self.GeneralCheckNonAcsii()
         self.UniCheck()
+        self.GeneralCheckNoTab()
+        self.GeneralCheckLineEnding()
+        self.GeneralCheckTrailingWhiteSpaceLine()
+
+    # Check whether NO Tab is used, replaced with spaces
+    def GeneralCheckNoTab(self):
+        if EccGlobalData.gConfig.GeneralCheckNoTab == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
+            EdkLogger.quiet("Checking No TAB used in file ...")
+            SqlCommand = """select ID, FullPath, ExtName from File where ExtName in ('.dec', '.inf', '.dsc', 'c', 'h')"""
+            RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
+            for Record in RecordSet:
+                if Record[2].upper() not in EccGlobalData.gConfig.BinaryExtList:
+                    op = open(Record[1]).readlines()
+                    IndexOfLine = 0
+                    for Line in op:
+                        IndexOfLine += 1
+                        IndexOfChar = 0
+                        for Char in Line:
+                            IndexOfChar += 1
+                            if Char == '\t':
+                                OtherMsg = "File %s has TAB char at line %s column %s" % (Record[1], IndexOfLine, IndexOfChar)
+                                
+ EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_NO_TAB, 
+ OtherMsg=OtherMsg, BelongsToTable='File', BelongsToItem=Record[0])
+
+    # Check Only use CRLF (Carriage Return Line Feed) line endings.
+    def GeneralCheckLineEnding(self):
+        if EccGlobalData.gConfig.GeneralCheckLineEnding == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
+            EdkLogger.quiet("Checking line ending in file ...")
+            SqlCommand = """select ID, FullPath, ExtName from File where ExtName in ('.dec', '.inf', '.dsc', 'c', 'h')"""
+            RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
+            for Record in RecordSet:
+                if Record[2].upper() not in EccGlobalData.gConfig.BinaryExtList:
+                    op = open(Record[1], 'rb').readlines()
+                    IndexOfLine = 0
+                    for Line in op:
+                        IndexOfLine += 1
+                        if not Line.endswith('\r\n'):
+                            OtherMsg = "File %s has invalid line ending at line %s" % (Record[1], IndexOfLine)
+                            
+ EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_INVALID_LINE_EN
+ DING, OtherMsg=OtherMsg, BelongsToTable='File', 
+ BelongsToItem=Record[0])
+
+    # Check if there is no trailing white space in one line.
+    def GeneralCheckTrailingWhiteSpaceLine(self):
+        if EccGlobalData.gConfig.GeneralCheckTrailingWhiteSpaceLine == '1' or EccGlobalData.gConfig.GeneralCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1':
+            EdkLogger.quiet("Checking trailing white space line in file ...")
+            SqlCommand = """select ID, FullPath, ExtName from File where ExtName in ('.dec', '.inf', '.dsc', 'c', 'h')"""
+            RecordSet = EccGlobalData.gDb.TblFile.Exec(SqlCommand)
+            for Record in RecordSet:
+                if Record[2].upper() not in EccGlobalData.gConfig.BinaryExtList:
+                    op = open(Record[1], 'rb').readlines()
+                    IndexOfLine = 0
+                    for Line in op:
+                        IndexOfLine += 1
+                        if Line.replace('\r', '').replace('\n', '').endswith(' '):
+                            OtherMsg = "File %s has trailing white spaces at line %s" % (Record[1], IndexOfLine)
+                            
+ EccGlobalData.gDb.TblReport.Insert(ERROR_GENERAL_CHECK_TRAILING_WHITE_
+ SPACE_LINE, OtherMsg=OtherMsg, BelongsToTable='File', 
+ BelongsToItem=Record[0])
 
     # Check whether file has non ACSII char
     def GeneralCheckNonAcsii(self):
diff --git a/BaseTools/Source/Python/Ecc/Configuration.py b/BaseTools/Source/Python/Ecc/Configuration.py
index 29a1220761..f58adbf736 100644
--- a/BaseTools/Source/Python/Ecc/Configuration.py
+++ b/BaseTools/Source/Python/Ecc/Configuration.py
@@ -186,6 +186,10 @@ class Configuration(object):
         self.GeneralCheckNonAcsii = 1
         # Check whether UNI file is valid
         self.GeneralCheckUni = 1
+        # Check Only use CRLF (Carriage Return Line Feed) line endings.
+        self.GeneralCheckLineEnding = 1
+        # Check if there is no trailing white space in one line.
+        self.GeneralCheckTrailingWhiteSpaceLine = 1
 
         ## Space Checking
         self.SpaceCheckAll = 1
diff --git a/BaseTools/Source/Python/Ecc/EccToolError.py b/BaseTools/Source/Python/Ecc/EccToolError.py
index 1d51da3ae1..ae0a31af8a 100644
--- a/BaseTools/Source/Python/Ecc/EccToolError.py
+++ b/BaseTools/Source/Python/Ecc/EccToolError.py
@@ -1,7 +1,7 @@
 ## @file
 # Standardized Error Hanlding infrastructures.
 #
-# Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2008 - 2018, Intel Corporation. All rights 
+reserved.<BR>
 # This program and the accompanying materials  # are licensed and made available under the terms and conditions of the BSD License  # which accompanies this distribution.  The full text of the license may be found at @@ -22,6 +22,8 @@ ERROR_GENERAL_CHECK_FILE_EXISTENCE = 1007  ERROR_GENERAL_CHECK_NON_ACSII = 1008  ERROR_GENERAL_CHECK_UNI = 1009  ERROR_GENERAL_CHECK_UNI_HELP_INFO = 1010
+ERROR_GENERAL_CHECK_INVALID_LINE_ENDING = 1011 
+ERROR_GENERAL_CHECK_TRAILING_WHITE_SPACE_LINE = 1012
 
 ERROR_SPACE_CHECK_ALL = 2000
 
@@ -109,7 +111,7 @@ ERROR_SMM_COMM_PARA_CHECK_BUFFER_TYPE = 12001
 
 gEccErrorMessage = {
     ERROR_GENERAL_CHECK_ALL : "",
-    ERROR_GENERAL_CHECK_NO_TAB : "'TAB' character is not allowed in source code, please replace each 'TAB' with two spaces",
+    ERROR_GENERAL_CHECK_NO_TAB : "'TAB' character is not allowed in 
+ source code, please replace each 'TAB' with two spaces.",
     ERROR_GENERAL_CHECK_INDENTATION : "Indentation does not follow coding style",
     ERROR_GENERAL_CHECK_LINE : "The width of each line does not follow coding style",
     ERROR_GENERAL_CHECK_NO_ASM : "There should be no use of _asm in the source file", @@ -119,6 +121,8 @@ gEccErrorMessage = {
     ERROR_GENERAL_CHECK_NON_ACSII : "File has invalid Non-ACSII char",
     ERROR_GENERAL_CHECK_UNI : "File is not a valid UTF-16 UNI file",
     ERROR_GENERAL_CHECK_UNI_HELP_INFO : "UNI file that is associated by INF or DEC file need define the prompt and help information.",
+    ERROR_GENERAL_CHECK_INVALID_LINE_ENDING : "Only CRLF (Carriage Return Line Feed) is allowed to line ending.",
+    ERROR_GENERAL_CHECK_TRAILING_WHITE_SPACE_LINE : "There should be no 
+ trailing white space in one line.",
 
     ERROR_SPACE_CHECK_ALL : "",
 
diff --git a/BaseTools/Source/Python/Ecc/config.ini b/BaseTools/Source/Python/Ecc/config.ini
index 9a431bf124..6c86da74d6 100644
--- a/BaseTools/Source/Python/Ecc/config.ini
+++ b/BaseTools/Source/Python/Ecc/config.ini
@@ -72,6 +72,10 @@ GeneralCheckFileExistence = 1  GeneralCheckNonAcsii = 1  # Check whether UNI file is valid  GeneralCheckUni = 1
+# Check Only use CRLF (Carriage Return Line Feed) line endings.
+self.GeneralCheckLineEnding = 1
+# Check if there is no trailing white space in one line.
+self.GeneralCheckTrailingWhiteSpaceLine = 1
 
 #
 # Space Checking
--
2.14.2.windows.2

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel