public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [Patch 0/3] .pytool/Plugin/EccCheck: Remove git reset and optimize
@ 2021-11-23  5:44 Michael D Kinney
  2021-11-23  5:44 ` [Patch 1/3] .pytool/Plugin/EccCheck: Remove RevertCode() Michael D Kinney
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Michael D Kinney @ 2021-11-23  5:44 UTC (permalink / raw)
  To: devel; +Cc: Sean Brogan, Bret Barkelew, Liming Gao, Michael Kubacki

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2986

* Use temp directory for all operations to prevent any
  changed to git state.
* Remove git reset operation that could corrupt staged
  and local changes.
* Improve performance by removing redundant directory scans
* Improve performance and reduce log file sizes by using
  --output option of git diff to a temp file instead of
  using stdout.

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>

Michael D Kinney (3):
  .pytool/Plugin/EccCheck: Remove RevertCode()
  .pytool/Plugin/EccCheck: Remove temp directory on exception
  .pytool/Plugin/EccCheck: Add performance optimizations

 .pytool/Plugin/EccCheck/EccCheck.py | 245 +++++++++++++++++++---------
 1 file changed, 172 insertions(+), 73 deletions(-)

-- 
2.32.0.windows.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Patch 1/3] .pytool/Plugin/EccCheck: Remove RevertCode()
  2021-11-23  5:44 [Patch 0/3] .pytool/Plugin/EccCheck: Remove git reset and optimize Michael D Kinney
@ 2021-11-23  5:44 ` Michael D Kinney
  2021-11-23  5:44 ` [Patch 2/3] .pytool/Plugin/EccCheck: Remove temp directory on exception Michael D Kinney
  2021-11-23  5:44 ` [Patch 3/3] .pytool/Plugin/EccCheck: Add performance optimizations Michael D Kinney
  2 siblings, 0 replies; 4+ messages in thread
From: Michael D Kinney @ 2021-11-23  5:44 UTC (permalink / raw)
  To: devel; +Cc: Sean Brogan, Bret Barkelew, Liming Gao, Michael Kubacki

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>
---
 .pytool/Plugin/EccCheck/EccCheck.py | 96 +++++++++++++++++------------
 1 file changed, 56 insertions(+), 40 deletions(-)

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [Patch 2/3] .pytool/Plugin/EccCheck: Remove temp directory on exception
  2021-11-23  5:44 [Patch 0/3] .pytool/Plugin/EccCheck: Remove git reset and optimize Michael D Kinney
  2021-11-23  5:44 ` [Patch 1/3] .pytool/Plugin/EccCheck: Remove RevertCode() Michael D Kinney
@ 2021-11-23  5:44 ` Michael D Kinney
  2021-11-23  5:44 ` [Patch 3/3] .pytool/Plugin/EccCheck: Add performance optimizations Michael D Kinney
  2 siblings, 0 replies; 4+ messages in thread
From: Michael D Kinney @ 2021-11-23  5:44 UTC (permalink / raw)
  To: devel; +Cc: Sean Brogan, Bret Barkelew, Liming Gao, Michael Kubacki

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2986

Add try/except to RunBuildPlugin() to remove temporary
directory if a KeyboardInterrupt exception or an unexpected
exception is detected.

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>
---
 .pytool/Plugin/EccCheck/EccCheck.py | 84 +++++++++++++++++------------
 1 file changed, 50 insertions(+), 34 deletions(-)

diff --git a/.pytool/Plugin/EccCheck/EccCheck.py b/.pytool/Plugin/EccCheck/EccCheck.py
index 82a3f14d6d5c..25583f15bf54 100644
--- a/.pytool/Plugin/EccCheck/EccCheck.py
+++ b/.pytool/Plugin/EccCheck/EccCheck.py
@@ -72,48 +72,64 @@ class EccCheck(ICiBuildPlugin):
 
         # Create temp directory
         temp_path = os.path.join(workspace_path, 'Build', 'ecctemp')
-        # Delete temp directory
-        if os.path.exists(temp_path):
-            shutil.rmtree(temp_path)
-        # Copy package being scanned to temp_path
-        shutil.copytree (
-          os.path.join(workspace_path, packagename),
-          os.path.join(temp_path, packagename),
-          symlinks=True
-          )
-        # Copy exception.xml to temp_path
-        shutil.copyfile (
-          os.path.join(basetools_path, "Source", "Python", "Ecc", "exception.xml"),
-          os.path.join(temp_path, "exception.xml")
-          )
+        try:
+            # Delete temp directory
+            if os.path.exists(temp_path):
+                shutil.rmtree(temp_path)
+            # Copy package being scanned to temp_path
+            shutil.copytree (
+              os.path.join(workspace_path, packagename),
+              os.path.join(temp_path, packagename),
+              symlinks=True
+              )
+            # Copy exception.xml to temp_path
+            shutil.copyfile (
+              os.path.join(basetools_path, "Source", "Python", "Ecc", "exception.xml"),
+              os.path.join(temp_path, "exception.xml")
+              )
 
-        self.ApplyConfig(pkgconfig, temp_path, packagename)
-        modify_dir_list = self.GetModifyDir(packagename)
-        patch = self.GetDiff(packagename)
-        ecc_diff_range = self.GetDiffRange(patch, packagename, temp_path)
-        #
-        # Set workingdir to Build output directory because Ecc generates temp files
-        # Can not set workingdir to temp_path because that can be on a different
-        # drive letter for some CI platforms and RunCmd() does not work correctly
-        # if working dir is on a different drive letter.
-        #
-        self.GenerateEccReport(modify_dir_list, ecc_diff_range, temp_path, basetools_path, os.path.join (workspace_path, 'Build'))
-        ecc_log = os.path.join(temp_path, "Ecc.log")
-        if self.ECC_PASS:
+            self.ApplyConfig(pkgconfig, temp_path, packagename)
+            modify_dir_list = self.GetModifyDir(packagename)
+            patch = self.GetDiff(packagename)
+            ecc_diff_range = self.GetDiffRange(patch, packagename, temp_path)
+            #
+            # Set workingdir to Build output directory because Ecc generates temp files
+            # Can not set workingdir to temp_path because that can be on a different
+            # drive letter for some CI platforms and RunCmd() does not work correctly
+            # if working dir is on a different drive letter.
+            #
+            self.GenerateEccReport(modify_dir_list, ecc_diff_range, temp_path, basetools_path, os.path.join (workspace_path, 'Build'))
+            ecc_log = os.path.join(temp_path, "Ecc.log")
+            if self.ECC_PASS:
+                # Delete temp directory
+                if os.path.exists(temp_path):
+                    shutil.rmtree(temp_path)
+                tc.SetSuccess()
+                return 0
+            else:
+                with open(ecc_log, encoding='utf8') as output:
+                    ecc_output = output.readlines()
+                    for line in ecc_output:
+                        logging.error(line.strip())
+                # Delete temp directory
+                if os.path.exists(temp_path):
+                    shutil.rmtree(temp_path)
+                tc.SetFailed("EccCheck failed for {0}".format(packagename), "CHECK FAILED")
+                return 1
+        except KeyboardInterrupt:
+            # If EccCheck is interrupted by keybard interrupt, then return failure
             # Delete temp directory
             if os.path.exists(temp_path):
                 shutil.rmtree(temp_path)
-            tc.SetSuccess()
-            return 0
+            tc.SetFailed("EccCheck interrupted for {0}".format(packagename), "CHECK FAILED")
+            return 1
         else:
-            with open(ecc_log, encoding='utf8') as output:
-                ecc_output = output.readlines()
-                for line in ecc_output:
-                    logging.error(line.strip())
+            # If EccCheck fails for any other exception type, raise the exception
             # Delete temp directory
             if os.path.exists(temp_path):
                 shutil.rmtree(temp_path)
-            tc.SetFailed("EccCheck failed for {0}".format(packagename), "CHECK FAILED")
+            tc.SetFailed("EccCheck exception for {0}".format(packagename), "CHECK FAILED")
+            raise
             return 1
 
     def GetDiff(self, pkg: str) -> List[str]:
-- 
2.32.0.windows.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [Patch 3/3] .pytool/Plugin/EccCheck: Add performance optimizations
  2021-11-23  5:44 [Patch 0/3] .pytool/Plugin/EccCheck: Remove git reset and optimize Michael D Kinney
  2021-11-23  5:44 ` [Patch 1/3] .pytool/Plugin/EccCheck: Remove RevertCode() Michael D Kinney
  2021-11-23  5:44 ` [Patch 2/3] .pytool/Plugin/EccCheck: Remove temp directory on exception Michael D Kinney
@ 2021-11-23  5:44 ` Michael D Kinney
  2 siblings, 0 replies; 4+ messages in thread
From: Michael D Kinney @ 2021-11-23  5:44 UTC (permalink / raw)
  To: devel; +Cc: Sean Brogan, Bret Barkelew, Liming Gao, Michael Kubacki

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2986

Improve the performance of EccCheck by using a temp file
instead of stdout to capture the results of the git diff
commands. If a large patch set is passed into EccCheck,
using stdout could be slow and also added the large diff
content to the build log that is redundant information.

A second performance improvement is to filter the
modified directories to remove duplicate directories.
Complex libraries and modules that have subdirectories
with sources would be scanned twice if there were source
changes in both the main directory and subdirectories.
Filter out the subdirectories from the modified directory
list when this case is detected.

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>
---
 .pytool/Plugin/EccCheck/EccCheck.py | 121 +++++++++++++++++++++-------
 1 file changed, 94 insertions(+), 27 deletions(-)

diff --git a/.pytool/Plugin/EccCheck/EccCheck.py b/.pytool/Plugin/EccCheck/EccCheck.py
index 25583f15bf54..908655dab667 100644
--- a/.pytool/Plugin/EccCheck/EccCheck.py
+++ b/.pytool/Plugin/EccCheck/EccCheck.py
@@ -30,7 +30,6 @@ class EccCheck(ICiBuildPlugin):
     },
     """
 
-    ReModifyFile = re.compile(r'[B-Q,S-Z]+[\d]*\t(.*)')
     FindModifyFile = re.compile(r'\+\+\+ b\/(.*)')
     LineScopePattern = (r'@@ -\d*\,*\d* \+\d*\,*\d* @@.*')
     LineNumRange = re.compile(r'@@ -\d*\,*\d* \+(\d*)\,*(\d*) @@.*')
@@ -87,10 +86,12 @@ class EccCheck(ICiBuildPlugin):
               os.path.join(basetools_path, "Source", "Python", "Ecc", "exception.xml"),
               os.path.join(temp_path, "exception.xml")
               )
+            # Output file to use for git diff operations
+            temp_diff_output = os.path.join (temp_path, 'diff.txt')
 
             self.ApplyConfig(pkgconfig, temp_path, packagename)
-            modify_dir_list = self.GetModifyDir(packagename)
-            patch = self.GetDiff(packagename)
+            modify_dir_list = self.GetModifyDir(packagename, temp_diff_output)
+            patch = self.GetDiff(packagename, temp_diff_output)
             ecc_diff_range = self.GetDiffRange(patch, packagename, temp_path)
             #
             # Set workingdir to Build output directory because Ecc generates temp files
@@ -132,37 +133,103 @@ class EccCheck(ICiBuildPlugin):
             raise
             return 1
 
-    def GetDiff(self, pkg: str) -> List[str]:
-        return_buffer = StringIO()
-        params = "diff --unified=0 origin/master HEAD"
-        RunCmd("git", params, outstream=return_buffer)
-        p = return_buffer.getvalue().strip()
-        patch = p.split("\n")
-        return_buffer.close()
-
+    def GetDiff(self, pkg: str, temp_diff_output: str) -> List[str]:
+        patch = []
+        #
+        # Generate unified diff between origin/master and HEAD.
+        #
+        params = "diff --output={} --unified=0 origin/master HEAD".format(temp_diff_output)
+        RunCmd("git", params)
+        with open(temp_diff_output) as file:
+            patch = file.read().strip().split('\n')
         return patch
 
-    def GetModifyDir(self, pkg: str) -> List[str]:
-        return_buffer = StringIO()
-        params = "diff --name-status" + ' HEAD' + ' origin/master'
-        RunCmd("git", params, outstream=return_buffer)
-        p1 = return_buffer.getvalue().strip()
-        dir_list = p1.split("\n")
-        return_buffer.close()
+    def GetModifyDir(self, pkg: str, temp_diff_output: str) -> List[str]:
+        #
+        # Generate diff between origin/master and HEAD using --diff-filter to
+        # exclude deleted and renamed files that do not need to be scanned by
+        # ECC.  Also use --name-status to only generate the names of the files
+        # with differences.  The output format of this git diff command is a
+        # list of files with the change status and the filename.  The filename
+        # is always at the end of the line.  Examples:
+        #
+        #   M       MdeModulePkg/Application/CapsuleApp/CapsuleApp.h
+        #   M       MdeModulePkg/Application/UiApp/FrontPage.h
+        #
+        params = "diff --output={} --diff-filter=dr --name-status origin/master HEAD".format(temp_diff_output)
+        RunCmd("git", params)
+        dir_list = []
+        with open(temp_diff_output) as file:
+            dir_list = file.read().strip().split('\n')
+
         modify_dir_list = []
         for modify_dir in dir_list:
-            file_path = self.ReModifyFile.findall(modify_dir)
-            if file_path:
-                file_dir = os.path.dirname(file_path[0])
-            else:
+            #
+            # Parse file name from the end of the line
+            #
+            file_path = modify_dir.strip().split()
+            #
+            # Skip lines that do not have at least 2 elements (status and file name)
+            #
+            if len(file_path) < 2:
                 continue
-            if pkg in file_dir and file_dir != pkg:
-                modify_dir_list.append('%s' % file_dir)
-            else:
+            #
+            # Parse the directory name from the file name
+            #
+            file_dir = os.path.dirname(file_path[-1])
+            #
+            # Skip directory names that do not start with the package being scanned.
+            #
+            if file_dir.split('/')[0] != pkg:
                 continue
+            #
+            # Skip directory names that are identical to the package being scanned.
+            # The assumption here is that there are no source files at the package
+            # root.  Instead, the only expected files in the package root are
+            # EDK II meta data files (DEC, DSC, FDF).
+            #
+            if file_dir == pkg:
+                continue
+            #
+            # Skip directory names that are already in the modified dir list
+            #
+            if file_dir in modify_dir_list:
+                continue
+            #
+            # Add the candidate directory to scan to the modified dir list
+            #
+            modify_dir_list.append(file_dir)
 
-        modify_dir_list = list(set(modify_dir_list))
-        return modify_dir_list
+        #
+        # Remove duplicates from modify_dir_list
+        # Given a folder path, ECC performs a recursive scan of that folder.
+        # If a parent and child folder are both present in modify_dir_list,
+        # then ECC will perform redudanct scans of source files.  In order
+        # to prevent redundant scans, if a parent and child folder are both
+        # present, then remove all the child folders.
+        #
+        # For example, if modified_dir_list contains the following elements:
+        #   MdeModulePkg/Core/Dxe
+        #   MdeModulePkg/Core/Dxe/Hand
+        #   MdeModulePkg/Core/Dxe/Mem
+        #
+        # Then MdeModulePkg/Core/Dxe/Hand and MdeModulePkg/Core/Dxe/Mem should
+        # be removed because the files in those folders are covered by a scan
+        # of MdeModulePkg/Core/Dxe.
+        #
+        filtered_list = []
+        for dir1 in modify_dir_list:
+            Append = True
+            for dir2 in modify_dir_list:
+                if dir1 == dir2:
+                    continue
+                common = os.path.commonpath([dir1, dir2])
+                if os.path.normpath(common) == os.path.normpath(dir2):
+                    Append = False
+                    break
+            if Append and dir1 not in filtered_list:
+                filtered_list.append(dir1)
+        return filtered_list
 
     def GetDiffRange(self, patch_diff: List[str], pkg: str, temp_path: str) -> Dict[str, List[Tuple[int, int]]]:
         IsDelete = True
-- 
2.32.0.windows.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-11-23  5:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-23  5:44 [Patch 0/3] .pytool/Plugin/EccCheck: Remove git reset and optimize Michael D Kinney
2021-11-23  5:44 ` [Patch 1/3] .pytool/Plugin/EccCheck: Remove RevertCode() Michael D Kinney
2021-11-23  5:44 ` [Patch 2/3] .pytool/Plugin/EccCheck: Remove temp directory on exception Michael D Kinney
2021-11-23  5:44 ` [Patch 3/3] .pytool/Plugin/EccCheck: Add performance optimizations Michael D Kinney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox