]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/UPT/Library/Parsing.py
BaseTools: Use absolute import in UPT
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Library / Parsing.py
index db0fec3a73cc04eab10f3b52d5dddd6577a2c235..81729d6cdbf7b3a2c43e6bfaf6072117e128296d 100644 (file)
@@ -1,12 +1,12 @@
 ## @file\r
-# This file is used to define common parsing related functions used in parsing \r
+# This file is used to define common parsing related functions used in parsing\r
 # INF/DEC/DSC process\r
 #\r
-# Copyright (c) 2011 - 2014, 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
 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
@@ -16,6 +16,7 @@
 '''\r
 Parsing\r
 '''\r
+from __future__ import absolute_import\r
 \r
 ##\r
 # Import Modules\r
@@ -23,12 +24,12 @@ Parsing
 import os.path\r
 import re\r
 \r
-from Library.String import RaiseParserError\r
-from Library.String import GetSplitValueList\r
-from Library.String import CheckFileType\r
-from Library.String import CheckFileExist\r
-from Library.String import CleanString\r
-from Library.String import NormPath\r
+from Library.StringUtils import RaiseParserError\r
+from Library.StringUtils import GetSplitValueList\r
+from Library.StringUtils import CheckFileType\r
+from Library.StringUtils import CheckFileExist\r
+from Library.StringUtils import CleanString\r
+from Library.StringUtils import NormPath\r
 \r
 from Logger.ToolError import FILE_NOT_FOUND\r
 from Logger.ToolError import FatalError\r
@@ -42,7 +43,7 @@ from Logger import StringTable as ST
 import Logger.Log as Logger\r
 \r
 from Parser.DecParser import Dec\r
-import GlobalData\r
+from . import GlobalData\r
 \r
 gPKG_INFO_DICT = {}\r
 \r
@@ -74,7 +75,7 @@ def GetBuildOption(String, File, LineNo= -1):
 # Get Library of Dsc as <LibraryClassKeyWord>|<LibraryInstance>\r
 #\r
 # @param Item:           String as <LibraryClassKeyWord>|<LibraryInstance>\r
-# @param ContainerFile:  The file which describes the library class, used for \r
+# @param ContainerFile:  The file which describes the library class, used for\r
 #                        error report\r
 #\r
 def GetLibraryClass(Item, ContainerFile, WorkspaceDir, LineNo= -1):\r
@@ -99,7 +100,7 @@ def GetLibraryClass(Item, ContainerFile, WorkspaceDir, LineNo= -1):
 # [|<TokenSpaceGuidCName>.<PcdCName>]\r
 #\r
 # @param Item:           String as <LibraryClassKeyWord>|<LibraryInstance>\r
-# @param ContainerFile:  The file which describes the library class, used for \r
+# @param ContainerFile:  The file which describes the library class, used for\r
 #                        error report\r
 #\r
 def GetLibraryClassOfInf(Item, ContainerFile, WorkspaceDir, LineNo= -1):\r
@@ -134,7 +135,7 @@ def GetLibraryClassOfInf(Item, ContainerFile, WorkspaceDir, LineNo= -1):
 #\r
 def CheckPcdTokenInfo(TokenInfoString, Section, File, LineNo= -1):\r
     Format = '<TokenSpaceGuidCName>.<PcdCName>'\r
-    if TokenInfoString != '' and TokenInfoString != None:\r
+    if TokenInfoString != '' and TokenInfoString is not None:\r
         TokenInfoList = GetSplitValueList(TokenInfoString, DataType.TAB_SPLIT)\r
         if len(TokenInfoList) == 2:\r
             return True\r
@@ -148,7 +149,7 @@ def CheckPcdTokenInfo(TokenInfoString, Section, File, LineNo= -1):
 #\r
 # @param Item:           String as <PcdTokenSpaceGuidCName>.<TokenCName>|\r
 #                        <Value>[|<Type>|<MaximumDatumSize>]\r
-# @param ContainerFile:  The file which describes the pcd, used for error \r
+# @param ContainerFile:  The file which describes the pcd, used for error\r
 #                        report\r
 \r
 #\r
@@ -176,7 +177,7 @@ def GetPcd(Item, Type, ContainerFile, LineNo= -1):
 #\r
 # @param Item:           String as <PcdTokenSpaceGuidCName>\r
 #                        .<TokenCName>|TRUE/FALSE\r
-# @param ContainerFile:  The file which describes the pcd, used for error \r
+# @param ContainerFile:  The file which describes the pcd, used for error\r
 #                        report\r
 #\r
 def GetFeatureFlagPcd(Item, Type, ContainerFile, LineNo= -1):\r
@@ -200,7 +201,7 @@ def GetFeatureFlagPcd(Item, Type, ContainerFile, LineNo= -1):
 #\r
 # @param Item:           String as <PcdTokenSpaceGuidCName>.<TokenCName>|\r
 #                        TRUE/FALSE\r
-# @param ContainerFile:  The file which describes the pcd, used for error \r
+# @param ContainerFile:  The file which describes the pcd, used for error\r
 #                        report\r
 #\r
 def GetDynamicDefaultPcd(Item, Type, ContainerFile, LineNo= -1):\r
@@ -226,7 +227,7 @@ def GetDynamicDefaultPcd(Item, Type, ContainerFile, LineNo= -1):
 #\r
 # @param Item:           String as <PcdTokenSpaceGuidCName>.<TokenCName>|\r
 #                        TRUE/FALSE\r
-# @param ContainerFile:  The file which describes the pcd, used for error \r
+# @param ContainerFile:  The file which describes the pcd, used for error\r
 #                        report\r
 #\r
 def GetDynamicHiiPcd(Item, Type, ContainerFile, LineNo= -1):\r
@@ -253,7 +254,7 @@ def GetDynamicHiiPcd(Item, Type, ContainerFile, LineNo= -1):
 #\r
 # @param Item:           String as <PcdTokenSpaceGuidCName>.<TokenCName>\r
 #                        |TRUE/FALSE\r
-# @param ContainerFile:  The file which describes the pcd, used for error \r
+# @param ContainerFile:  The file which describes the pcd, used for error\r
 #                        report\r
 #\r
 def GetDynamicVpdPcd(Item, Type, ContainerFile, LineNo= -1):\r
@@ -273,7 +274,7 @@ def GetDynamicVpdPcd(Item, Type, ContainerFile, LineNo= -1):
 ## GetComponent\r
 #\r
 # Parse block of the components defined in dsc file\r
-# Set KeyValues as [ ['component name', [lib1, lib2, lib3], \r
+# Set KeyValues as [ ['component name', [lib1, lib2, lib3],\r
 # [bo1, bo2, bo3], [pcd1, pcd2, pcd3]], ...]\r
 #\r
 # @param Lines:             The content to be parsed\r
@@ -408,7 +409,7 @@ def GetExec(String):
 ## GetComponents\r
 #\r
 # Parse block of the components defined in dsc file\r
-# Set KeyValues as [ ['component name', [lib1, lib2, lib3], [bo1, bo2, bo3], \r
+# Set KeyValues as [ ['component name', [lib1, lib2, lib3], [bo1, bo2, bo3],\r
 # [pcd1, pcd2, pcd3]], ...]\r
 #\r
 # @param Lines:             The content to be parsed\r
@@ -433,7 +434,7 @@ def GetComponents(Lines, KeyValues, CommentCharacter):
     LineList = Lines.split('\n')\r
     for Line in LineList:\r
         Line = CleanString(Line, CommentCharacter)\r
-        if Line == None or Line == '':\r
+        if Line is None or Line == '':\r
             continue\r
 \r
         if FindBlock == False:\r
@@ -531,7 +532,7 @@ def GetComponents(Lines, KeyValues, CommentCharacter):
 #\r
 # @param Item:           String as <Filename>[|<Family>[|<TagName>[|<ToolCode>\r
 #                        [|<PcdFeatureFlag>]]]]\r
-# @param ContainerFile:  The file which describes the library class, used \r
+# @param ContainerFile:  The file which describes the library class, used\r
 #                        for error report\r
 #\r
 def GetSource(Item, ContainerFile, FileRelativePath, LineNo= -1):\r
@@ -556,7 +557,7 @@ def GetSource(Item, ContainerFile, FileRelativePath, LineNo= -1):
 #\r
 # @param Item:           String as <Filename>[|<Family>[|<TagName>\r
 #                        [|<ToolCode>[|<PcdFeatureFlag>]]]]\r
-# @param ContainerFile:  The file which describes the library class, \r
+# @param ContainerFile:  The file which describes the library class,\r
 #                        used for error report\r
 #\r
 def GetBinary(Item, ContainerFile, LineNo= -1):\r
@@ -580,7 +581,7 @@ def GetBinary(Item, ContainerFile, LineNo= -1):
 #\r
 # @param Item:           String as <GuidCName>[|<PcdFeatureFlag>]\r
 # @param Type:           Type of parsing string\r
-# @param ContainerFile:  The file which describes the library class, \r
+# @param ContainerFile:  The file which describes the library class,\r
 #                        used for error report\r
 #\r
 def GetGuidsProtocolsPpisOfInf(Item):\r
@@ -594,7 +595,7 @@ def GetGuidsProtocolsPpisOfInf(Item):
 #\r
 # @param Item:           String as <GuidCName>=<GuidValue>\r
 # @param Type:           Type of parsing string\r
-# @param ContainerFile:  The file which describes the library class, \r
+# @param ContainerFile:  The file which describes the library class,\r
 # used for error report\r
 #\r
 def GetGuidsProtocolsPpisOfDec(Item, Type, ContainerFile, LineNo= -1):\r
@@ -625,7 +626,7 @@ def GetGuidsProtocolsPpisOfDec(Item, Type, ContainerFile, LineNo= -1):
 #\r
 # @param Item:           String as <PackagePath>[|<PcdFeatureFlag>]\r
 # @param Type:           Type of parsing string\r
-# @param ContainerFile:  The file which describes the library class, \r
+# @param ContainerFile:  The file which describes the library class,\r
 #                        used for error report\r
 #\r
 def GetPackage(Item, ContainerFile, FileRelativePath, LineNo= -1):\r
@@ -827,21 +828,23 @@ def GetPkgInfoFromDec(Path):
 def GetWorkspacePackage():\r
     DecFileList = []\r
     WorkspaceDir = GlobalData.gWORKSPACE\r
-    for Root, Dirs, Files in os.walk(WorkspaceDir):\r
-        if 'CVS' in Dirs:\r
-            Dirs.remove('CVS')\r
-        if '.svn' in Dirs:\r
-            Dirs.remove('.svn')\r
-        for Dir in Dirs:\r
-            if Dir.startswith('.'):\r
-                Dirs.remove(Dir)\r
-        for FileSp in Files:\r
-            if FileSp.startswith('.'):\r
-                continue\r
-            Ext = os.path.splitext(FileSp)[1]\r
-            if Ext.lower() in ['.dec']:\r
-                DecFileList.append\\r
-                (os.path.normpath(os.path.join(Root, FileSp)))\r
+    PackageDir = GlobalData.gPACKAGE_PATH\r
+    for PkgRoot in [WorkspaceDir] + PackageDir:\r
+        for Root, Dirs, Files in os.walk(PkgRoot):\r
+            if 'CVS' in Dirs:\r
+                Dirs.remove('CVS')\r
+            if '.svn' in Dirs:\r
+                Dirs.remove('.svn')\r
+            for Dir in Dirs:\r
+                if Dir.startswith('.'):\r
+                    Dirs.remove(Dir)\r
+            for FileSp in Files:\r
+                if FileSp.startswith('.'):\r
+                    continue\r
+                Ext = os.path.splitext(FileSp)[1]\r
+                if Ext.lower() in ['.dec']:\r
+                    DecFileList.append\\r
+                    (os.path.normpath(os.path.join(Root, FileSp)))\r
     #\r
     # abstract package guid, version info from DecFile List\r
     #\r
@@ -919,7 +922,7 @@ def MacroParser(Line, FileName, SectionType, FileLocalMacros):
         FileLocalMacros[Name] = Value\r
 \r
     ReIsValidMacroName = re.compile(r"^[A-Z][A-Z0-9_]*$", re.DOTALL)\r
-    if ReIsValidMacroName.match(Name) == None:\r
+    if ReIsValidMacroName.match(Name) is None:\r
         Logger.Error('Parser',\r
                      FORMAT_INVALID,\r
                      ST.ERR_MACRONAME_INVALID % (Name),\r
@@ -934,11 +937,11 @@ def MacroParser(Line, FileName, SectionType, FileLocalMacros):
     # <Value>           ::=  {<NumVal>} {<Boolean>} {<AsciiString>} {<GUID>}\r
     #                        {<CString>} {<UnicodeString>} {<CArray>}\r
     #\r
-    # The definition of <NumVal>, <PATH>, <Boolean>, <GUID>, <CString>, \r
+    # The definition of <NumVal>, <PATH>, <Boolean>, <GUID>, <CString>,\r
     # <UnicodeString>, <CArray> are subset of <AsciiString>.\r
     #\r
     ReIsValidMacroValue = re.compile(r"^[\x20-\x7e]*$", re.DOTALL)\r
-    if ReIsValidMacroValue.match(Value) == None:\r
+    if ReIsValidMacroValue.match(Value) is None:\r
         Logger.Error('Parser',\r
                      FORMAT_INVALID,\r
                      ST.ERR_MACROVALUE_INVALID % (Value),\r
@@ -948,15 +951,15 @@ def MacroParser(Line, FileName, SectionType, FileLocalMacros):
 \r
     return Name, Value\r
 \r
-## GenSection \r
+## GenSection\r
 #\r
 # generate section contents\r
 #\r
-# @param  SectionName:  indicate the name of the section, details refer to \r
+# @param  SectionName:  indicate the name of the section, details refer to\r
 #                       INF, DEC specs\r
-# @param  SectionDict:  section statement dict, key is SectionAttrs(arch, \r
-#                       moduletype or platform may exist as needed) list \r
-#                       seperated by space, \r
+# @param  SectionDict:  section statement dict, key is SectionAttrs(arch,\r
+#                       moduletype or platform may exist as needed) list\r
+#                       seperated by space,\r
 #                       value is statement\r
 #\r
 def GenSection(SectionName, SectionDict, SplitArch=True, NeedBlankLine=False):\r
@@ -977,13 +980,13 @@ def GenSection(SectionName, SectionDict, SplitArch=True, NeedBlankLine=False):
         else:\r
             Section = '[' + SectionName + ']'\r
         Content += '\n' + Section + '\n'\r
-        if StatementList != None:\r
+        if StatementList is not None:\r
             for Statement in StatementList:\r
                 LineList = Statement.split('\n')\r
                 NewStatement = ""\r
                 for Line in LineList:\r
                     # ignore blank comment\r
-                    if not Line.replace("#", '').strip() and SectionName != 'Defines':\r
+                    if not Line.replace("#", '').strip() and SectionName not in ('Defines', 'Hob', 'Event', 'BootMode'):\r
                         continue\r
                     # add two space before non-comments line except the comments in Defines section\r
                     if Line.strip().startswith('#') and SectionName == 'Defines':\r
@@ -1002,10 +1005,10 @@ def GenSection(SectionName, SectionDict, SplitArch=True, NeedBlankLine=False):
     return Content\r
 \r
 ## ConvertArchForInstall\r
-# if Arch.upper() is in "IA32", "X64", "IPF", and "EBC", it must be upper case.  "common" must be lower case.  \r
+# if Arch.upper() is in "IA32", "X64", "IPF", and "EBC", it must be upper case.  "common" must be lower case.\r
 # Anything else, the case must be preserved\r
 #\r
-# @param Arch: the arch string that need to be converted, it should be stripped before pass in \r
+# @param Arch: the arch string that need to be converted, it should be stripped before pass in\r
 # @return: the arch string that get converted\r
 #\r
 def ConvertArchForInstall(Arch):\r