]> git.proxmox.com Git - mirror_edk2.git/commitdiff
.pytool/Plugin/EccCheck: Remove RevertCode()
authorMichael D Kinney <michael.d.kinney@intel.com>
Tue, 23 Nov 2021 05:13:41 +0000 (21:13 -0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 29 Nov 2021 06:38:51 +0000 (06:38 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2986

The RevertCode() method uses git reset which can remove
local changes. Instead of modifying the local files, a
copy of the package passed into the EccCheck tool is
copied to a temp directory in Build/ecctemp.  This same
temp directory is also used for exception.xml. The working
directory used by ECC is also set to this same temp
directory. The combination of these changes eliminates
operations that that modified the git state.

Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael Kubacki <michael.kubacki@microsoft.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Acked-by: Sean Brogan <sean.brogan@microsoft.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
.pytool/Plugin/EccCheck/EccCheck.py

index 2d0612269b2e24cf5d7e76337ca2aeca30303af9..c4c2af1bf6e46e27215c924fb1cdb6deebc1e2a0 100644 (file)
@@ -69,32 +69,50 @@ class EccCheck(ICiBuildPlugin):
         env.set_shell_var('WORKSPACE', workspace_path)\r
         env.set_shell_var('PACKAGES_PATH', os.pathsep.join(Edk2pathObj.PackagePathList))\r
         self.ECC_PASS = True\r
-        self.ApplyConfig(pkgconfig, workspace_path, basetools_path, packagename)\r
+\r
+        # Create temp directory\r
+        temp_path = os.path.join(workspace_path, 'Build', '.pytool', 'Plugin', 'EccCheck')\r
+        # Delete temp directory\r
+        if os.path.exists(temp_path):\r
+            shutil.rmtree(temp_path)\r
+        # Copy package being scanned to temp_path\r
+        shutil.copytree (\r
+          os.path.join(workspace_path, packagename),\r
+          os.path.join(temp_path, packagename),\r
+          symlinks=True\r
+          )\r
+        # Copy exception.xml to temp_path\r
+        shutil.copyfile (\r
+          os.path.join(basetools_path, "Source", "Python", "Ecc", "exception.xml"),\r
+          os.path.join(temp_path, "exception.xml")\r
+          )\r
+\r
+        self.ApplyConfig(pkgconfig, temp_path, packagename)\r
         modify_dir_list = self.GetModifyDir(packagename)\r
         patch = self.GetDiff(packagename)\r
-        ecc_diff_range = self.GetDiffRange(patch, packagename, workspace_path)\r
-        self.GenerateEccReport(modify_dir_list, ecc_diff_range, workspace_path, basetools_path)\r
-        ecc_log = os.path.join(workspace_path, "Ecc.log")\r
-        self.RevertCode()\r
+        ecc_diff_range = self.GetDiffRange(patch, packagename, temp_path)\r
+        #\r
+        # Use temp_path as working directory when running ECC tool\r
+        #\r
+        self.GenerateEccReport(modify_dir_list, ecc_diff_range, temp_path, basetools_path)\r
+        ecc_log = os.path.join(temp_path, "Ecc.log")\r
         if self.ECC_PASS:\r
+            # Delete temp directory\r
+            if os.path.exists(temp_path):\r
+                shutil.rmtree(temp_path)\r
             tc.SetSuccess()\r
-            self.RemoveFile(ecc_log)\r
             return 0\r
         else:\r
             with open(ecc_log, encoding='utf8') as output:\r
                 ecc_output = output.readlines()\r
                 for line in ecc_output:\r
                     logging.error(line.strip())\r
-            self.RemoveFile(ecc_log)\r
-            tc.SetFailed("EccCheck failed for {0}".format(packagename), "Ecc detected issues")\r
+            # Delete temp directory\r
+            if os.path.exists(temp_path):\r
+                shutil.rmtree(temp_path)\r
+            tc.SetFailed("EccCheck failed for {0}".format(packagename), "CHECK FAILED")\r
             return 1\r
 \r
-    def RevertCode(self) -> None:\r
-        submoudle_params = "submodule update --init"\r
-        RunCmd("git", submoudle_params)\r
-        reset_params = "reset HEAD --hard"\r
-        RunCmd("git", reset_params)\r
-\r
     def GetDiff(self, pkg: str) -> List[str]:\r
         return_buffer = StringIO()\r
         params = "diff --unified=0 origin/master HEAD"\r
@@ -105,11 +123,6 @@ class EccCheck(ICiBuildPlugin):
 \r
         return patch\r
 \r
-    def RemoveFile(self, file: str) -> None:\r
-        if os.path.exists(file):\r
-            os.remove(file)\r
-        return\r
-\r
     def GetModifyDir(self, pkg: str) -> List[str]:\r
         return_buffer = StringIO()\r
         params = "diff --name-status" + ' HEAD' + ' origin/master'\r
@@ -132,14 +145,14 @@ class EccCheck(ICiBuildPlugin):
         modify_dir_list = list(set(modify_dir_list))\r
         return modify_dir_list\r
 \r
-    def GetDiffRange(self, patch_diff: List[str], pkg: str, workingdir: str) -> Dict[str, List[Tuple[int, int]]]:\r
+    def GetDiffRange(self, patch_diff: List[str], pkg: str, temp_path: str) -> Dict[str, List[Tuple[int, int]]]:\r
         IsDelete = True\r
         StartCheck = False\r
         range_directory: Dict[str, List[Tuple[int, int]]] = {}\r
         for line in patch_diff:\r
             modify_file = self.FindModifyFile.findall(line)\r
             if modify_file and pkg in modify_file[0] and not StartCheck and os.path.isfile(modify_file[0]):\r
-                modify_file_comment_dic = self.GetCommentRange(modify_file[0], workingdir)\r
+                modify_file_comment_dic = self.GetCommentRange(modify_file[0], temp_path)\r
                 IsDelete = False\r
                 StartCheck = True\r
                 modify_file_dic = modify_file[0]\r
@@ -158,11 +171,13 @@ class EccCheck(ICiBuildPlugin):
                         range_directory[modify_file_dic].append(i)\r
         return range_directory\r
 \r
-    def GetCommentRange(self, modify_file: str, workingdir: str) -> List[Tuple[int, int]]:\r
-        modify_file_path = os.path.join(workingdir, modify_file)\r
+    def GetCommentRange(self, modify_file: str, temp_path: str) -> List[Tuple[int, int]]:\r
+        comment_range: List[Tuple[int, int]] = []\r
+        modify_file_path = os.path.join(temp_path, modify_file)\r
+        if not os.path.exists (modify_file_path):\r
+            return comment_range\r
         with open(modify_file_path) as f:\r
             line_no = 1\r
-            comment_range: List[Tuple[int, int]] = []\r
             Start = False\r
             for line in f:\r
                 if line.startswith('/**'):\r
@@ -179,35 +194,33 @@ class EccCheck(ICiBuildPlugin):
         return comment_range\r
 \r
     def GenerateEccReport(self, modify_dir_list: List[str], ecc_diff_range: Dict[str, List[Tuple[int, int]]],\r
-                           workspace_path: str, basetools_path: str) -> None:\r
+                           temp_path: str, basetools_path: str) -> None:\r
         ecc_need = False\r
         ecc_run = True\r
-        config = os.path.join(basetools_path, "Source", "Python", "Ecc", "config.ini")\r
-        exception = os.path.join(basetools_path, "Source", "Python", "Ecc", "exception.xml")\r
-        report = os.path.join(workspace_path, "Ecc.csv")\r
+        config    = os.path.normpath(os.path.join(basetools_path, "Source", "Python", "Ecc", "config.ini"))\r
+        exception = os.path.normpath(os.path.join(temp_path, "exception.xml"))\r
+        report    = os.path.normpath(os.path.join(temp_path, "Ecc.csv"))\r
         for modify_dir in modify_dir_list:\r
-            target = os.path.join(workspace_path, modify_dir)\r
+            target = os.path.normpath(os.path.join(temp_path, modify_dir))\r
             logging.info('Run ECC tool for the commit in %s' % modify_dir)\r
             ecc_need = True\r
             ecc_params = "-c {0} -e {1} -t {2} -r {3}".format(config, exception, target, report)\r
-            return_code = RunCmd("Ecc", ecc_params, workingdir=workspace_path)\r
+            return_code = RunCmd("Ecc", ecc_params, workingdir=temp_path)\r
             if return_code != 0:\r
                 ecc_run = False\r
                 break\r
             if not ecc_run:\r
                 logging.error('Fail to run ECC tool')\r
-            self.ParseEccReport(ecc_diff_range, workspace_path)\r
+            self.ParseEccReport(ecc_diff_range, temp_path)\r
 \r
         if not ecc_need:\r
             logging.info("Doesn't need run ECC check")\r
 \r
-        revert_params = "checkout -- {}".format(exception)\r
-        RunCmd("git", revert_params)\r
         return\r
 \r
-    def ParseEccReport(self, ecc_diff_range: Dict[str, List[Tuple[int, int]]], workspace_path: str) -> None:\r
-        ecc_log = os.path.join(workspace_path, "Ecc.log")\r
-        ecc_csv = os.path.join(workspace_path, "Ecc.csv")\r
+    def ParseEccReport(self, ecc_diff_range: Dict[str, List[Tuple[int, int]]], temp_path: str) -> None:\r
+        ecc_log = os.path.join(temp_path, "Ecc.log")\r
+        ecc_csv = os.path.join(temp_path, "Ecc.csv")\r
         row_lines = []\r
         ignore_error_code = self.GetIgnoreErrorCode()\r
         if os.path.exists(ecc_csv):\r
@@ -236,16 +249,16 @@ class EccCheck(ICiBuildPlugin):
             log.writelines(all_line)\r
         return\r
 \r
-    def ApplyConfig(self, pkgconfig: Dict[str, List[str]], workspace_path: str, basetools_path: str, pkg: str) -> None:\r
+    def ApplyConfig(self, pkgconfig: Dict[str, List[str]], temp_path: str, pkg: str) -> None:\r
         if "IgnoreFiles" in pkgconfig:\r
             for a in pkgconfig["IgnoreFiles"]:\r
-                a = os.path.join(workspace_path, pkg, a)\r
+                a = os.path.join(temp_path, pkg, a)\r
                 a = a.replace(os.sep, "/")\r
 \r
                 logging.info("Ignoring Files {0}".format(a))\r
                 if os.path.exists(a):\r
                     if os.path.isfile(a):\r
-                        self.RemoveFile(a)\r
+                        os.remove(a)\r
                     elif os.path.isdir(a):\r
                         shutil.rmtree(a)\r
                 else:\r
@@ -253,7 +266,7 @@ class EccCheck(ICiBuildPlugin):
 \r
         if "ExceptionList" in pkgconfig:\r
             exception_list = pkgconfig["ExceptionList"]\r
-            exception_xml = os.path.join(basetools_path, "Source", "Python", "Ecc", "exception.xml")\r
+            exception_xml = os.path.join(temp_path, "exception.xml")\r
             try:\r
                 logging.info("Appending exceptions")\r
                 self.AppendException(exception_list, exception_xml)\r