]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Workspace/MetaFileParser.py
BaseTools: Fix old python2 idioms
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / MetaFileParser.py
index b1e715d5d4a9975944c7aef94c89dab6419060d4..8c860d594b4fe725ade3480b34ea7922be185090 100644 (file)
@@ -15,6 +15,7 @@
 ##\r
 # Import Modules\r
 #\r
+from __future__ import print_function\r
 import Common.LongFilePathOs as os\r
 import re\r
 import time\r
@@ -77,10 +78,10 @@ def ParseMacro(Parser):
             #\r
             # First judge whether this DEFINE is in conditional directive statements or not.\r
             #\r
-            if type(self) == DscParser and self._InDirective > -1:\r
+            if isinstance(self, DscParser) and self._InDirective > -1:\r
                 pass\r
             else:\r
-                if type(self) == DecParser:\r
+                if isinstance(self, DecParser):\r
                     if MODEL_META_DATA_HEADER in self._SectionType:\r
                         self._FileLocalMacros[Name] = Value\r
                     else:\r
@@ -91,7 +92,7 @@ def ParseMacro(Parser):
                     self._ConstructSectionMacroDict(Name, Value)\r
 \r
         # EDK_GLOBAL defined macros\r
-        elif type(self) != DscParser:\r
+        elif not isinstance(self, DscParser):\r
             EdkLogger.error('Parser', FORMAT_INVALID, "EDK_GLOBAL can only be used in .dsc file",\r
                             ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
         elif self._SectionType != MODEL_META_DATA_HEADER:\r
@@ -232,7 +233,7 @@ class MetaFileParser(object):
     #   DataInfo = [data_type, scope1(arch), scope2(platform/moduletype)]\r
     #\r
     def __getitem__(self, DataInfo):\r
-        if type(DataInfo) != type(()):\r
+        if not isinstance(DataInfo, type(())):\r
             DataInfo = (DataInfo,)\r
 \r
         # Parse the file first, if necessary\r
@@ -274,7 +275,7 @@ class MetaFileParser(object):
         TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT)\r
         self._ValueList[0:len(TokenList)] = TokenList\r
         # Don't do macro replacement for dsc file at this point\r
-        if type(self) != DscParser:\r
+        if not isinstance(self, DscParser):\r
             Macros = self._Macros\r
             self._ValueList = [ReplaceMacro(Value, Macros) for Value in self._ValueList]\r
 \r
@@ -301,7 +302,7 @@ class MetaFileParser(object):
         for Item in GetSplitValueList(self._CurrentLine[1:-1], TAB_COMMA_SPLIT):\r
             if Item == '':\r
                 continue\r
-            ItemList = GetSplitValueList(Item, TAB_SPLIT,3)\r
+            ItemList = GetSplitValueList(Item, TAB_SPLIT, 3)\r
             # different section should not mix in one section\r
             if self._SectionName != '' and self._SectionName != ItemList[0].upper():\r
                 EdkLogger.error('Parser', FORMAT_INVALID, "Different section names in the same section",\r
@@ -381,7 +382,7 @@ class MetaFileParser(object):
                 EdkLogger.error('Parser', FORMAT_INVALID, "Invalid version number",\r
                                 ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
 \r
-        if type(self) == InfParser and self._Version < 0x00010005:\r
+        if isinstance(self, InfParser) and self._Version < 0x00010005:\r
             # EDK module allows using defines as macros\r
             self._FileLocalMacros[Name] = Value\r
         self._Defines[Name] = Value\r
@@ -397,7 +398,7 @@ class MetaFileParser(object):
             self._ValueList[1] = TokenList2[1]              # keys\r
         else:\r
             self._ValueList[1] = TokenList[0]\r
-        if len(TokenList) == 2 and type(self) != DscParser: # value\r
+        if len(TokenList) == 2 and not isinstance(self, DscParser): # value\r
             self._ValueList[2] = ReplaceMacro(TokenList[1], self._Macros)\r
 \r
         if self._ValueList[1].count('_') != 4:\r
@@ -419,13 +420,13 @@ class MetaFileParser(object):
 \r
     ## Construct section Macro dict \r
     def _ConstructSectionMacroDict(self, Name, Value):\r
-        ScopeKey = [(Scope[0], Scope[1],Scope[2]) for Scope in self._Scope]\r
+        ScopeKey = [(Scope[0], Scope[1], Scope[2]) for Scope in self._Scope]\r
         ScopeKey = tuple(ScopeKey)\r
         #\r
         # DecParser SectionType is a list, will contain more than one item only in Pcd Section\r
         # As Pcd section macro usage is not alllowed, so here it is safe\r
         #\r
-        if type(self) == DecParser:\r
+        if isinstance(self, DecParser):\r
             SectionDictKey = self._SectionType[0], ScopeKey\r
         else:\r
             SectionDictKey = self._SectionType, ScopeKey\r
@@ -442,7 +443,7 @@ class MetaFileParser(object):
         SpeSpeMacroDict = {}\r
 \r
         ActiveSectionType = self._SectionType\r
-        if type(self) == DecParser:\r
+        if isinstance(self, DecParser):\r
             ActiveSectionType = self._SectionType[0]\r
 \r
         for (SectionType, Scope) in self._SectionsMacroDict:\r
@@ -450,15 +451,15 @@ class MetaFileParser(object):
                 continue\r
 \r
             for ActiveScope in self._Scope:\r
-                Scope0, Scope1 ,Scope2= ActiveScope[0], ActiveScope[1],ActiveScope[2]\r
-                if(Scope0, Scope1,Scope2) not in Scope:\r
+                Scope0, Scope1, Scope2= ActiveScope[0], ActiveScope[1], ActiveScope[2]\r
+                if(Scope0, Scope1, Scope2) not in Scope:\r
                     break\r
             else:\r
                 SpeSpeMacroDict.update(self._SectionsMacroDict[(SectionType, Scope)])\r
 \r
             for ActiveScope in self._Scope:\r
-                Scope0, Scope1,Scope2 = ActiveScope[0], ActiveScope[1],ActiveScope[2]\r
-                if(Scope0, Scope1,Scope2) not in Scope and (Scope0, TAB_COMMON, TAB_COMMON) not in Scope and (TAB_COMMON, Scope1, TAB_COMMON) not in Scope:\r
+                Scope0, Scope1, Scope2 = ActiveScope[0], ActiveScope[1], ActiveScope[2]\r
+                if(Scope0, Scope1, Scope2) not in Scope and (Scope0, TAB_COMMON, TAB_COMMON) not in Scope and (TAB_COMMON, Scope1, TAB_COMMON) not in Scope:\r
                     break\r
             else:\r
                 ComSpeMacroDict.update(self._SectionsMacroDict[(SectionType, Scope)])\r
@@ -635,7 +636,7 @@ class InfParser(MetaFileParser):
             # Model, Value1, Value2, Value3, Arch, Platform, BelongsToItem=-1,\r
             # LineBegin=-1, ColumnBegin=-1, LineEnd=-1, ColumnEnd=-1, Enabled=-1\r
             #\r
-            for Arch, Platform,_ in self._Scope:\r
+            for Arch, Platform, _ in self._Scope:\r
                 LastItem = self._Store(self._SectionType,\r
                             self._ValueList[0],\r
                             self._ValueList[1],\r
@@ -838,6 +839,7 @@ class DscParser(MetaFileParser):
         TAB_ELSE.upper()                            :   MODEL_META_DATA_CONDITIONAL_STATEMENT_ELSE,\r
         TAB_END_IF.upper()                          :   MODEL_META_DATA_CONDITIONAL_STATEMENT_ENDIF,\r
         TAB_USER_EXTENSIONS.upper()                 :   MODEL_META_DATA_USER_EXTENSION,\r
+        TAB_ERROR.upper()                           :   MODEL_META_DATA_CONDITIONAL_STATEMENT_ERROR,\r
     }\r
 \r
     # Valid names in define section\r
@@ -945,7 +947,7 @@ class DscParser(MetaFileParser):
                 self._DirectiveParser()\r
                 continue\r
             if Line[0] == TAB_OPTION_START and not self._InSubsection:\r
-                EdkLogger.error("Parser", FILE_READ_FAILURE, "Missing the '{' before %s in Line %s" % (Line, Index+1),ExtraData=self.MetaFile)\r
+                EdkLogger.error("Parser", FILE_READ_FAILURE, "Missing the '{' before %s in Line %s" % (Line, Index+1), ExtraData=self.MetaFile)\r
 \r
             if self._InSubsection:\r
                 SectionType = self._SubsectionType\r
@@ -1028,6 +1030,8 @@ class DscParser(MetaFileParser):
         Scope = [[TAB_COMMON, TAB_COMMON, TAB_COMMON]]\r
         if ItemType == MODEL_META_DATA_INCLUDE:\r
             Scope = self._Scope\r
+        elif ItemType == MODEL_META_DATA_CONDITIONAL_STATEMENT_ERROR:\r
+            Scope = self._Scope\r
         if ItemType == MODEL_META_DATA_CONDITIONAL_STATEMENT_ENDIF:\r
             # Remove all directives between !if and !endif, including themselves\r
             while self._DirectiveStack:\r
@@ -1041,7 +1045,7 @@ class DscParser(MetaFileParser):
                 EdkLogger.error("Parser", FORMAT_INVALID, "Redundant '!endif'",\r
                                 File=self.MetaFile, Line=self._LineIndex + 1,\r
                                 ExtraData=self._CurrentLine)\r
-        elif ItemType != MODEL_META_DATA_INCLUDE:\r
+        elif ItemType not in {MODEL_META_DATA_INCLUDE, MODEL_META_DATA_CONDITIONAL_STATEMENT_ERROR}:\r
             # Break if there's a !else is followed by a !elseif\r
             if ItemType == MODEL_META_DATA_CONDITIONAL_STATEMENT_ELSEIF and \\r
                self._DirectiveStack and \\r
@@ -1100,7 +1104,7 @@ class DscParser(MetaFileParser):
     @ParseMacro\r
     def _SkuIdParser(self):\r
         TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT)\r
-        if len(TokenList) not in (2,3):\r
+        if len(TokenList) not in (2, 3):\r
             EdkLogger.error('Parser', FORMAT_INVALID, "Correct format is '<Number>|<UiName>[|<UiName>]'",\r
                             ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
         self._ValueList[0:len(TokenList)] = TokenList\r
@@ -1160,7 +1164,7 @@ class DscParser(MetaFileParser):
 \r
         # Validate the datum type of Dynamic Defaul PCD and DynamicEx Default PCD\r
         ValueList = GetSplitValueList(self._ValueList[2])\r
-        if len(ValueList) > 1 and ValueList[1] in [TAB_UINT8 , TAB_UINT16, TAB_UINT32 , TAB_UINT64] \\r
+        if len(ValueList) > 1 and ValueList[1] in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64] \\r
                               and self._ItemType in [MODEL_PCD_DYNAMIC_DEFAULT, MODEL_PCD_DYNAMIC_EX_DEFAULT]:\r
             EdkLogger.error('Parser', FORMAT_INVALID, "The datum type '%s' of PCD is wrong" % ValueList[1],\r
                             ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
@@ -1168,7 +1172,7 @@ class DscParser(MetaFileParser):
         # Validate the VariableName of DynamicHii and DynamicExHii for PCD Entry must not be an empty string\r
         if self._ItemType in [MODEL_PCD_DYNAMIC_HII, MODEL_PCD_DYNAMIC_EX_HII]:\r
             DscPcdValueList = GetSplitValueList(TokenList[1], TAB_VALUE_SPLIT, 1)\r
-            if len(DscPcdValueList[0].replace('L','').replace('"','').strip()) == 0:\r
+            if len(DscPcdValueList[0].replace('L', '').replace('"', '').strip()) == 0:\r
                 EdkLogger.error('Parser', FORMAT_INVALID, "The VariableName field in the HII format PCD entry must not be an empty string",\r
                             ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
 \r
@@ -1248,7 +1252,7 @@ class DscParser(MetaFileParser):
             Macros.update(self._Symbols)\r
         if GlobalData.BuildOptionPcd:\r
             for Item in GlobalData.BuildOptionPcd:\r
-                if type(Item) is tuple:\r
+                if isinstance(Item, tuple):\r
                     continue\r
                 PcdName, TmpValue = Item.split("=")\r
                 TmpValue = BuildOptionValue(TmpValue, self._GuidDict)\r
@@ -1287,6 +1291,7 @@ class DscParser(MetaFileParser):
             MODEL_META_DATA_BUILD_OPTION                    :   self.__ProcessBuildOption,\r
             MODEL_UNKNOWN                                   :   self._Skip,\r
             MODEL_META_DATA_USER_EXTENSION                  :   self._SkipUserExtension,\r
+            MODEL_META_DATA_CONDITIONAL_STATEMENT_ERROR     :   self._ProcessError,\r
         }\r
 \r
         self._Table = MetaFileStorage(self._RawTable.Cur, self.MetaFile, MODEL_FILE_DSC, True)\r
@@ -1304,7 +1309,7 @@ class DscParser(MetaFileParser):
         self._ContentIndex = 0\r
         self._InSubsection = False\r
         while self._ContentIndex < len(self._Content) :\r
-            Id, self._ItemType, V1, V2, V3, S1, S2, S3,Owner, self._From, \\r
+            Id, self._ItemType, V1, V2, V3, S1, S2, S3, Owner, self._From, \\r
                 LineStart, ColStart, LineEnd, ColEnd, Enabled = self._Content[self._ContentIndex]\r
 \r
             if self._From < 0:\r
@@ -1322,8 +1327,8 @@ class DscParser(MetaFileParser):
                     break\r
                 Record = self._Content[self._ContentIndex]\r
                 if LineStart == Record[10] and LineEnd == Record[12]:\r
-                    if [Record[5], Record[6],Record[7]] not in self._Scope:\r
-                        self._Scope.append([Record[5], Record[6],Record[7]])\r
+                    if [Record[5], Record[6], Record[7]] not in self._Scope:\r
+                        self._Scope.append([Record[5], Record[6], Record[7]])\r
                     self._ContentIndex += 1\r
                 else:\r
                     break\r
@@ -1337,7 +1342,7 @@ class DscParser(MetaFileParser):
                 self._InSubsection = False\r
             try:\r
                 Processer[self._ItemType]()\r
-            except EvaluationException, Excpt:\r
+            except EvaluationException as Excpt:\r
                 # \r
                 # Only catch expression evaluation error here. We need to report\r
                 # the precise number of line on which the error occurred\r
@@ -1359,7 +1364,7 @@ class DscParser(MetaFileParser):
                     EdkLogger.error('Parser', FORMAT_INVALID, "Invalid expression: %s" % str(Excpt),\r
                                     File=self._FileWithError, ExtraData=' '.join(self._ValueList),\r
                                     Line=self._LineIndex + 1)\r
-            except MacroException, Excpt:\r
+            except MacroException as Excpt:\r
                 EdkLogger.error('Parser', FORMAT_INVALID, str(Excpt),\r
                                 File=self._FileWithError, ExtraData=' '.join(self._ValueList),\r
                                 Line=self._LineIndex + 1)\r
@@ -1390,6 +1395,10 @@ class DscParser(MetaFileParser):
         GlobalData.gPlatformDefines.update(self._FileLocalMacros)\r
         self._PostProcessed = True\r
         self._Content = None\r
+    def _ProcessError(self):\r
+        if not self._Enabled:\r
+            return\r
+        EdkLogger.error('Parser', ERROR_STATEMENT, self._ValueList[1], File=self.MetaFile, Line=self._LineIndex + 1)\r
 \r
     def __ProcessSectionHeader(self):\r
         self._SectionName = self._ValueList[0]\r
@@ -1412,7 +1421,7 @@ class DscParser(MetaFileParser):
                         MODEL_PCD_DYNAMIC_VPD, MODEL_PCD_DYNAMIC_EX_DEFAULT, MODEL_PCD_DYNAMIC_EX_HII,\r
                         MODEL_PCD_DYNAMIC_EX_VPD):\r
             Records = self._RawTable.Query(PcdType, BelongsToItem= -1.0)\r
-            for TokenSpaceGuid, PcdName, Value, Dummy2, Dummy3, Dummy4,ID, Line in Records:\r
+            for TokenSpaceGuid, PcdName, Value, Dummy2, Dummy3, Dummy4, ID, Line in Records:\r
                 Name = TokenSpaceGuid + '.' + PcdName\r
                 if Name not in GlobalData.gPlatformOtherPcds:\r
                     PcdLine = Line\r
@@ -1457,10 +1466,10 @@ class DscParser(MetaFileParser):
             Macros.update(GlobalData.gGlobalDefines)\r
             try:\r
                 Result = ValueExpression(self._ValueList[1], Macros)()\r
-            except SymbolNotFound, Exc:\r
+            except SymbolNotFound as Exc:\r
                 EdkLogger.debug(EdkLogger.DEBUG_5, str(Exc), self._ValueList[1])\r
                 Result = False\r
-            except WrnExpression, Excpt:\r
+            except WrnExpression as Excpt:\r
                 # \r
                 # Catch expression evaluation warning here. We need to report\r
                 # the precise number of line and return the evaluation result\r
@@ -1551,23 +1560,6 @@ class DscParser(MetaFileParser):
 \r
             self.IncludedFiles.add (IncludedFile1)\r
 \r
-            # todo: rework the nested include checking logic\r
-            # Current nested include checking rely on dsc file order inside build.db.\r
-            # It is not reliable and will lead to build fail in some case.\r
-            #\r
-            # When project A and B include a common dsc file C.\r
-            # Build project A. It give dsc file A = ID 1 in build.db, and C ID = 2.\r
-            # Build project B. It give dsc file B ID = 3, and C ID still = 2.\r
-            # Then, we build project B fail, unless we clean build.db.\r
-            # In oldder BaseTools, the project B ID will still be 1,\r
-            # that's why it work before.\r
-\r
-            # Does not allow lower level included file to include upper level included file\r
-            #if Parser._From != Owner and int(Owner) > int (Parser._From):\r
-            #   EdkLogger.error('parser', FILE_ALREADY_EXIST, File=self._FileWithError,\r
-            #       Line=self._LineIndex + 1, ExtraData="{0} is already included at a higher level.".format(IncludedFile1))\r
-\r
-\r
             # set the parser status with current status\r
             Parser._SectionName = self._SectionName\r
             if self._InSubsection:\r
@@ -1581,7 +1573,11 @@ class DscParser(MetaFileParser):
 \r
             # update current status with sub-parser's status\r
             self._SectionName = Parser._SectionName\r
-            self._SectionType = Parser._SectionType\r
+            if not self._InSubsection:\r
+                self._SectionType = Parser._SectionType\r
+            self._SubsectionType = Parser._SubsectionType\r
+            self._InSubsection = Parser._InSubsection\r
+\r
             self._Scope = Parser._Scope\r
             self._Enabled = Parser._Enabled\r
 \r
@@ -1619,7 +1615,7 @@ class DscParser(MetaFileParser):
         if PcdValue and "." not in self._ValueList[0]:\r
             try:\r
                 ValList[Index] = ValueExpression(PcdValue, self._Macros)(True)\r
-            except WrnExpression, Value:\r
+            except WrnExpression as Value:\r
                 ValList[Index] = Value.result\r
             except:\r
                 pass\r
@@ -1635,7 +1631,7 @@ class DscParser(MetaFileParser):
         try:\r
             self._ValueList[2] = '|'.join(ValList)\r
         except Exception:\r
-            print ValList\r
+            print(ValList)\r
 \r
     def __ProcessComponent(self):\r
         self._ValueList[0] = ReplaceMacro(self._ValueList[0], self._Macros)\r
@@ -1804,7 +1800,7 @@ class DecParser(MetaFileParser):
         if self._DefinesCount > 1:\r
             EdkLogger.error('Parser', FORMAT_INVALID, 'Multiple [Defines] section is exist.', self.MetaFile )\r
         if self._DefinesCount == 0:\r
-            EdkLogger.error('Parser', FORMAT_INVALID, 'No [Defines] section exist.',self.MetaFile)\r
+            EdkLogger.error('Parser', FORMAT_INVALID, 'No [Defines] section exist.', self.MetaFile)\r
         self._Done()\r
 \r
 \r
@@ -1948,7 +1944,7 @@ class DecParser(MetaFileParser):
                     self._CurrentStructurePcdName = ""\r
                 else:\r
                     if self._CurrentStructurePcdName != TAB_SPLIT.join(PcdNames[:2]):\r
-                        EdkLogger.error('Parser', FORMAT_INVALID, "Pcd Name does not match: %s and %s " % (self._CurrentStructurePcdName , TAB_SPLIT.join(PcdNames[:2])),\r
+                        EdkLogger.error('Parser', FORMAT_INVALID, "Pcd Name does not match: %s and %s " % (self._CurrentStructurePcdName, TAB_SPLIT.join(PcdNames[:2])),\r
                                 File=self.MetaFile, Line=self._LineIndex + 1)\r
                     self._ValueList[1] = TAB_SPLIT.join(PcdNames[2:])\r
                     self._ValueList[2] = PcdTockens[1]\r
@@ -2024,7 +2020,7 @@ class DecParser(MetaFileParser):
                 try:\r
                     self._GuidDict.update(self._AllPcdDict)\r
                     ValueList[0] = ValueExpressionEx(ValueList[0], ValueList[1], self._GuidDict)(True)\r
-                except BadExpression, Value:\r
+                except BadExpression as Value:\r
                     EdkLogger.error('Parser', FORMAT_INVALID, Value, ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
             # check format of default value against the datum type\r
             IsValid, Cause = CheckPcdDatum(ValueList[1], ValueList[0])\r