]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/UPT/Core/DependencyRules.py
BaseTools: Clean up source files
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Core / DependencyRules.py
index 7039a7d6262b957cec8572735210c7dcd9eee44e..9c3baa1b12d8d2f9e9f1322b258a3c5593ecf86f 100644 (file)
@@ -1,11 +1,11 @@
 ## @file\r
 # This file is for installed package information database operations\r
 #\r
-# Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
 #\r
-# This program and the accompanying materials are licensed and made available \r
-# under the terms and conditions of the BSD License which accompanies this \r
-# distribution. The full text of the license may be found at \r
+# This program and the accompanying materials are licensed and made available\r
+# under the terms and conditions of the BSD License which accompanies this\r
+# distribution. The full text of the license may be found at\r
 # http://opensource.org/licenses/bsd-license.php\r
 #\r
 #\r
@@ -21,14 +21,15 @@ Dependency
 # Import Modules\r
 #\r
 from os.path import dirname\r
+import os\r
 \r
 import Logger.Log as Logger\r
 from Logger import StringTable as ST\r
 from Library.Parsing import GetWorkspacePackage\r
 from Library.Parsing import GetWorkspaceModule\r
+from Library.Parsing import GetPkgInfoFromDec\r
 from Library.Misc import GetRelativePath\r
 from Library import GlobalData\r
-from PomAdapter.InfPomAlignment import InfPomAlignment\r
 from Logger.ToolError import FatalError\r
 from Logger.ToolError import EDK1_INF_ERROR\r
 from Logger.ToolError import UNKNOWN_ERROR\r
@@ -39,16 +40,30 @@ DEPEX_CHECK_PACKAGE_NOT_FOUND, DEPEX_CHECK_DP_NOT_FOUND) = (0, 1, 2, 3)
 ## DependencyRules\r
 #\r
 # This class represents the dependency rule check mechanism\r
-# \r
+#\r
 # @param object:      Inherited from object class\r
 #\r
 class DependencyRules(object):\r
-    def __init__(self, Datab):\r
+    def __init__(self, Datab, ToBeInstalledPkgList=None):\r
         self.IpiDb = Datab\r
         self.WsPkgList = GetWorkspacePackage()\r
         self.WsModuleList = GetWorkspaceModule()\r
-        self.PkgsToBeDepend = []\r
-        \r
+\r
+        self.PkgsToBeDepend = [(PkgInfo[1], PkgInfo[2]) for PkgInfo in self.WsPkgList]\r
+\r
+        # Add package info from the DIST to be installed.\r
+        self.PkgsToBeDepend.extend(self.GenToBeInstalledPkgList(ToBeInstalledPkgList))\r
+\r
+    def GenToBeInstalledPkgList(self, ToBeInstalledPkgList):\r
+        if not ToBeInstalledPkgList:\r
+            return []\r
+        RtnList = []\r
+        for Dist in ToBeInstalledPkgList:\r
+            for Package in Dist.PackageSurfaceArea:\r
+                RtnList.append((Package[0], Package[1]))\r
+\r
+        return RtnList\r
+\r
     ## Check whether a module exists by checking the Guid+Version+Name+Path combination\r
     #\r
     # @param Guid:  Guid of a module\r
@@ -66,7 +81,7 @@ class DependencyRules(object):
             return True\r
         else:\r
             return False\r
-        \r
+\r
     ## Check whether a module depex satisfied.\r
     #\r
     # @param ModuleObj: A module object\r
@@ -86,15 +101,15 @@ class DependencyRules(object):
             #\r
             Exist = self.CheckPackageExists(Dep.GetGuid(), Dep.GetVersion())\r
             #\r
-            # check whether satisfied by current distribution \r
+            # check whether satisfied by current distribution\r
             #\r
             if not Exist:\r
-                if DpObj == None:\r
+                if DpObj is None:\r
                     Result = False\r
                     break\r
                 for GuidVerPair in DpObj.PackageSurfaceArea.keys():\r
                     if Dep.GetGuid() == GuidVerPair[0]:\r
-                        if Dep.GetVersion() == None or \\r
+                        if Dep.GetVersion() is None or \\r
                         len(Dep.GetVersion()) == 0:\r
                             Result = True\r
                             break\r
@@ -104,7 +119,7 @@ class DependencyRules(object):
                 else:\r
                     Result = False\r
                     break\r
-        \r
+\r
         if not Result:\r
             Logger.Error("CheckModuleDepex", UNKNOWN_ERROR, \\r
                          ST.ERR_DEPENDENCY_NOT_MATCH % (ModuleObj.GetName(), \\r
@@ -112,7 +127,7 @@ class DependencyRules(object):
                                                         Dep.GetGuid(), \\r
                                                         Dep.GetVersion()))\r
         return Result\r
-            \r
+\r
     ## Check whether a package exists in a package list specified by PkgsToBeDepend.\r
     #\r
     # @param Guid: Guid of a package\r
@@ -139,7 +154,7 @@ class DependencyRules(object):
 \r
         Logger.Verbose(ST.MSG_CHECK_PACKAGE_FINISH)\r
         return Found\r
-         \r
+\r
     ## Check whether a package depex satisfied.\r
     #\r
     # @param PkgObj: A package object\r
@@ -156,7 +171,7 @@ class DependencyRules(object):
             else:\r
                 return False\r
         return True\r
-        \r
+\r
     ## Check whether a DP exists.\r
     #\r
     # @param Guid: Guid of a Distribution\r
@@ -181,7 +196,6 @@ class DependencyRules(object):
     #          False else\r
     #\r
     def CheckInstallDpDepexSatisfied(self, DpObj):\r
-        self.PkgsToBeDepend = [(PkgInfo[1], PkgInfo[2]) for PkgInfo in self.WsPkgList]\r
         return self.CheckDpDepexSatisfied(DpObj)\r
 \r
     # # Check whether multiple DP depex satisfied by current workspace for Install\r
@@ -191,7 +205,6 @@ class DependencyRules(object):
     #          False else\r
     #\r
     def CheckTestInstallPdDepexSatisfied(self, DpObjList):\r
-        self.PkgsToBeDepend = [(PkgInfo[1], PkgInfo[2]) for PkgInfo in self.WsPkgList]\r
         for DpObj in DpObjList:\r
             if self.CheckDpDepexSatisfied(DpObj):\r
                 for PkgKey in DpObj.PackageSurfaceArea.keys():\r
@@ -203,7 +216,7 @@ class DependencyRules(object):
         return True, DpObj\r
 \r
 \r
-    ## Check whether a DP depex satisfied by current workspace \r
+    ## Check whether a DP depex satisfied by current workspace\r
     #  (excluding the original distribution's packages to be replaced) for Replace\r
     #\r
     # @param DpObj:  A distribution object\r
@@ -230,17 +243,17 @@ class DependencyRules(object):
                 continue\r
             else:\r
                 return False\r
-            \r
+\r
         for ModKey in DpObj.ModuleSurfaceArea.keys():\r
             ModObj = DpObj.ModuleSurfaceArea[ModKey]\r
             if self.CheckModuleDepexSatisfied(ModObj, DpObj):\r
                 continue\r
             else:\r
                 return False\r
-        \r
+\r
         return True\r
-    \r
-    ## Check whether a DP could be removed from current workspace. \r
+\r
+    ## Check whether a DP could be removed from current workspace.\r
     #\r
     # @param DpGuid:  File's guid\r
     # @param DpVersion: File's version\r
@@ -254,7 +267,7 @@ class DependencyRules(object):
         #\r
         # remove modules that included in current DP\r
         # List of item (FilePath)\r
-        DpModuleList = self.IpiDb.GetDpModuleList(DpGuid, DpVersion) \r
+        DpModuleList = self.IpiDb.GetDpModuleList(DpGuid, DpVersion)\r
         for Module in DpModuleList:\r
             if Module in WsModuleList:\r
                 WsModuleList.remove(Module)\r
@@ -264,7 +277,7 @@ class DependencyRules(object):
         #\r
         # get packages in current Dp and find the install path\r
         # List of item (PkgGuid, PkgVersion, InstallPath)\r
-        DpPackageList = self.IpiDb.GetPackageListFromDp(DpGuid, DpVersion) \r
+        DpPackageList = self.IpiDb.GetPackageListFromDp(DpGuid, DpVersion)\r
         DpPackagePathList = []\r
         WorkSP = GlobalData.gWORKSPACE\r
         for (PkgName, PkgGuid, PkgVersion, DecFile) in self.WsPkgList:\r
@@ -272,23 +285,23 @@ class DependencyRules(object):
                 pass\r
             DecPath = dirname(DecFile)\r
             if DecPath.find(WorkSP) > -1:\r
-                InstallPath = GetRelativePath(DecPath,WorkSP)\r
-                DecFileRelaPath = GetRelativePath(DecFile,WorkSP)\r
+                InstallPath = GetRelativePath(DecPath, WorkSP)\r
+                DecFileRelaPath = GetRelativePath(DecFile, WorkSP)\r
             else:\r
                 InstallPath = DecPath\r
                 DecFileRelaPath = DecFile\r
-                \r
+\r
             if (PkgGuid, PkgVersion, InstallPath) in DpPackageList:\r
                 DpPackagePathList.append(DecFileRelaPath)\r
                 DpPackageList.remove((PkgGuid, PkgVersion, InstallPath))\r
-        \r
+\r
         #\r
         # the left items in DpPackageList are the packages that installed but not found anymore\r
         #\r
         for (PkgGuid, PkgVersion, InstallPath) in DpPackageList:\r
             Logger.Warn("UPT",\r
                         ST.WARN_INSTALLED_PACKAGE_NOT_FOUND%(PkgGuid, PkgVersion, InstallPath))\r
-        \r
+\r
         #\r
         # check modules to see if has dependency on package of current DP\r
         #\r
@@ -307,7 +320,7 @@ class DependencyRules(object):
     # @param NewDpPkgList: a list of package information (Guid, Version) in new Dp\r
     # @retval Replaceable: True if distribution could be replaced, False Else\r
     # @retval DependModuleList: the list of modules that make distribution can not be replaced\r
-    # \r
+    #\r
     def CheckDpDepexForReplace(self, OrigDpGuid, OrigDpVersion, NewDpPkgList):\r
         Replaceable = True\r
         DependModuleList = []\r
@@ -315,19 +328,19 @@ class DependencyRules(object):
         #\r
         # remove modules that included in current DP\r
         # List of item (FilePath)\r
-        DpModuleList = self.IpiDb.GetDpModuleList(OrigDpGuid, OrigDpVersion) \r
+        DpModuleList = self.IpiDb.GetDpModuleList(OrigDpGuid, OrigDpVersion)\r
         for Module in DpModuleList:\r
             if Module in WsModuleList:\r
                 WsModuleList.remove(Module)\r
             else:\r
                 Logger.Warn("UPT\n",\r
                             ST.ERR_MODULE_NOT_INSTALLED % Module)\r
-        \r
+\r
         OtherPkgList = NewDpPkgList\r
         #\r
         # get packages in current Dp and find the install path\r
         # List of item (PkgGuid, PkgVersion, InstallPath)\r
-        DpPackageList = self.IpiDb.GetPackageListFromDp(OrigDpGuid, OrigDpVersion) \r
+        DpPackageList = self.IpiDb.GetPackageListFromDp(OrigDpGuid, OrigDpVersion)\r
         DpPackagePathList = []\r
         WorkSP = GlobalData.gWORKSPACE\r
         for (PkgName, PkgGuid, PkgVersion, DecFile) in self.WsPkgList:\r
@@ -335,12 +348,12 @@ class DependencyRules(object):
                 pass\r
             DecPath = dirname(DecFile)\r
             if DecPath.find(WorkSP) > -1:\r
-                InstallPath = GetRelativePath(DecPath,WorkSP)\r
-                DecFileRelaPath = GetRelativePath(DecFile,WorkSP)\r
+                InstallPath = GetRelativePath(DecPath, WorkSP)\r
+                DecFileRelaPath = GetRelativePath(DecFile, WorkSP)\r
             else:\r
                 InstallPath = DecPath\r
                 DecFileRelaPath = DecFile\r
-                \r
+\r
             if (PkgGuid, PkgVersion, InstallPath) in DpPackageList:\r
                 DpPackagePathList.append(DecFileRelaPath)\r
                 DpPackageList.remove((PkgGuid, PkgVersion, InstallPath))\r
@@ -353,7 +366,7 @@ class DependencyRules(object):
         for (PkgGuid, PkgVersion, InstallPath) in DpPackageList:\r
             Logger.Warn("UPT",\r
                         ST.WARN_INSTALLED_PACKAGE_NOT_FOUND%(PkgGuid, PkgVersion, InstallPath))\r
-        \r
+\r
         #\r
         # check modules to see if it can be satisfied by package not belong to removed DP\r
         #\r
@@ -363,8 +376,8 @@ class DependencyRules(object):
                 DependModuleList.append(Module)\r
         return (Replaceable, DependModuleList)\r
 \r
-    \r
-## check whether module depends on packages in DpPackagePathList, return True \r
+\r
+## check whether module depends on packages in DpPackagePathList, return True\r
 # if found, False else\r
 #\r
 # @param Path: a module path\r
@@ -373,18 +386,15 @@ class DependencyRules(object):
 #           True:  module doesn't depend on package in DpPackagePathList\r
 #\r
 def VerifyRemoveModuleDep(Path, DpPackagePathList):\r
-    WorkSP = GlobalData.gWORKSPACE\r
-    \r
     try:\r
-        PomAli = InfPomAlignment(Path, WorkSP, Skip=True)\r
-\r
-        for Item in PomAli.GetPackageDependencyList():\r
-            if Item.GetPackageFilePath() in DpPackagePathList:\r
-                Logger.Info(ST.MSG_MODULE_DEPEND_ON % (Path, Item.GetPackageFilePath()))\r
+        for Item in GetPackagePath(Path):\r
+            if Item in DpPackagePathList:\r
+                DecPath = os.path.normpath(os.path.join(GlobalData.gWORKSPACE, Item))\r
+                Logger.Info(ST.MSG_MODULE_DEPEND_ON % (Path, DecPath))\r
                 return False\r
         else:\r
             return True\r
-    except FatalError, ErrCode:\r
+    except FatalError as ErrCode:\r
         if ErrCode.message == EDK1_INF_ERROR:\r
             Logger.Warn("UPT",\r
                         ST.WRN_EDK1_INF_FOUND%Path)\r
@@ -392,36 +402,54 @@ def VerifyRemoveModuleDep(Path, DpPackagePathList):
         else:\r
             return True\r
 \r
+# # GetPackagePath\r
+#\r
+# Get Dependency package path from an Inf file path\r
+#\r
+def GetPackagePath(InfPath):\r
+    PackagePath = []\r
+    if os.path.exists(InfPath):\r
+        FindSection = False\r
+        for Line in open(InfPath).readlines():\r
+            Line = Line.strip()\r
+            if not Line:\r
+                continue\r
+            if Line.startswith('#'):\r
+                continue\r
+            if Line.startswith('[Packages') and Line.endswith(']'):\r
+                FindSection = True\r
+                continue\r
+            if Line.startswith('[') and Line.endswith(']') and FindSection:\r
+                break\r
+            if FindSection:\r
+                PackagePath.append(os.path.normpath(Line))\r
+\r
+    return PackagePath\r
+\r
 ## check whether module depends on packages in DpPackagePathList and can not be satisfied by OtherPkgList\r
 #\r
 # @param Path: a module path\r
 # @param DpPackagePathList:  a list of Package Paths\r
 # @param OtherPkgList:       a list of Package Information (Guid, Version)\r
 # @retval:  False: module depends on package in DpPackagePathList and can not be satisfied by OtherPkgList\r
-#           True:  either module doesn't depend on DpPackagePathList or module depends on DpPackagePathList \r
+#           True:  either module doesn't depend on DpPackagePathList or module depends on DpPackagePathList\r
 #                 but can be satisfied by OtherPkgList\r
 #\r
 def VerifyReplaceModuleDep(Path, DpPackagePathList, OtherPkgList):\r
-    WorkSP = GlobalData.gWORKSPACE\r
-    \r
     try:\r
-        PomAli = InfPomAlignment(Path, WorkSP, Skip=True)\r
-\r
-        for Item in PomAli.GetPackageDependencyList():\r
-            if Item.GetPackageFilePath() in DpPackagePathList:\r
-                Guid, Version = Item.GetGuid(), Item.GetVersion()\r
+        for Item in GetPackagePath(Path):\r
+            if Item in DpPackagePathList:\r
+                DecPath = os.path.normpath(os.path.join(GlobalData.gWORKSPACE, Item))\r
+                Name, Guid, Version = GetPkgInfoFromDec(DecPath)\r
                 if (Guid, Version) not in OtherPkgList:\r
-                    Logger.Info(ST.MSG_MODULE_DEPEND_ON % (Path, Item.GetPackageFilePath()))\r
+                    Logger.Info(ST.MSG_MODULE_DEPEND_ON % (Path, DecPath))\r
                     return False\r
         else:\r
             return True\r
-    except FatalError, ErrCode:\r
+    except FatalError as ErrCode:\r
         if ErrCode.message == EDK1_INF_ERROR:\r
             Logger.Warn("UPT",\r
                         ST.WRN_EDK1_INF_FOUND%Path)\r
             return True\r
         else:\r
             return True\r
-   \r
-\r
-\r