X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FEcc%2FMetaFileWorkspace%2FMetaFileParser.py;h=283789fd1ddd708341c25c68d1b7a08ce8bfd776;hb=938cf4c33a0695ef1011b07e455a7ec2f87b5ad3;hp=4d61cd1cea917db996e81f8f65890337ed4d2965;hpb=656d2539be34ea0ce356857ffd4f9decdf0476b2;p=mirror_edk2.git diff --git a/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py b/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py index 4d61cd1cea..283789fd1d 100644 --- a/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py +++ b/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py @@ -14,6 +14,7 @@ ## # Import Modules # +from __future__ import absolute_import import Common.LongFilePathOs as os import re import time @@ -21,17 +22,17 @@ import copy import Common.EdkLogger as EdkLogger import Common.GlobalData as GlobalData -import EccGlobalData -import EccToolError +import Ecc.EccGlobalData as EccGlobalData +import Ecc.EccToolError as EccToolError from CommonDataClass.DataClass import * from Common.DataType import * -from Common.String import * +from Common.StringUtils import * from Common.Misc import GuidStructureStringToGuidString, CheckPcdDatum, PathClass, AnalyzePcdData from Common.Expression import * from CommonDataClass.Exceptions import * -from MetaFileTable import MetaFileStorage +from Ecc.MetaFileWorkspace.MetaFileTable import MetaFileStorage from GenFds.FdfParser import FdfParser from Common.LongFilePathSupport import OpenLongFilePath as open from Common.LongFilePathSupport import CodecOpenLongFilePath @@ -39,7 +40,7 @@ from Common.LongFilePathSupport import CodecOpenLongFilePath ## A decorator used to parse macro definition def ParseMacro(Parser): def MacroParser(self): - Match = gMacroDefPattern.match(self._CurrentLine) + Match = GlobalData.gMacroDefPattern.match(self._CurrentLine) if not Match: # Not 'DEFINE/EDK_GLOBAL' statement, call decorated method Parser(self) @@ -60,7 +61,7 @@ def ParseMacro(Parser): EdkLogger.error('Parser', FORMAT_INVALID, "%s can only be defined via environment variable" % Name, ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1) # Only upper case letters, digit and '_' are allowed - if not gMacroNamePattern.match(Name): + if not GlobalData.gMacroNamePattern.match(Name): EdkLogger.error('Parser', FORMAT_INVALID, "The macro name must be in the pattern [A-Z][A-Z0-9_]*", ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1) @@ -68,7 +69,7 @@ def ParseMacro(Parser): self._ItemType = MODEL_META_DATA_DEFINE # DEFINE defined macros if Type == TAB_DSC_DEFINES_DEFINE: - if type(self) == DecParser: + if isinstance(self, DecParser): if MODEL_META_DATA_HEADER in self._SectionType: self._FileLocalMacros[Name] = Value else: @@ -83,7 +84,7 @@ def ParseMacro(Parser): SectionLocalMacros = self._SectionsMacroDict[SectionDictKey] SectionLocalMacros[Name] = Value # EDK_GLOBAL defined macros - elif type(self) != DscParser: + elif not isinstance(self, DscParser): EdkLogger.error('Parser', FORMAT_INVALID, "EDK_GLOBAL can only be used in .dsc file", ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1) elif self._SectionType != MODEL_META_DATA_HEADER: @@ -92,7 +93,7 @@ def ParseMacro(Parser): elif (Name in self._FileLocalMacros) and (self._FileLocalMacros[Name] != Value): EdkLogger.error('Parser', FORMAT_INVALID, "EDK_GLOBAL defined a macro with the same name and different value as one defined by 'DEFINE'", ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1) - + self._ValueList = [Type, Name, Value] return MacroParser @@ -215,7 +216,7 @@ class MetaFileParser(object): # DataInfo = [data_type, scope1(arch), scope2(platform/moduletype)] # def __getitem__(self, DataInfo): - if type(DataInfo) != type(()): + if not isinstance(DataInfo, type(())): DataInfo = (DataInfo,) # Parse the file first, if necessary @@ -257,7 +258,7 @@ class MetaFileParser(object): TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT) self._ValueList[0:len(TokenList)] = TokenList # Don't do macro replacement for dsc file at this point - if type(self) != DscParser: + if not isinstance(self, DscParser): Macros = self._Macros self._ValueList = [ReplaceMacro(Value, Macros) for Value in self._ValueList] @@ -334,7 +335,7 @@ class MetaFileParser(object): self._ValueList = [ReplaceMacro(Value, self._Macros) for Value in self._ValueList] Name, Value = self._ValueList[1], self._ValueList[2] - # Sometimes, we need to make differences between EDK and EDK2 modules + # Sometimes, we need to make differences between EDK and EDK2 modules if Name == 'INF_VERSION': try: self._Version = int(Value, 0) @@ -354,8 +355,8 @@ class MetaFileParser(object): UniFile = os.path.join(os.path.dirname(self.MetaFile), Value) if os.path.exists(UniFile): self._UniObj = UniParser(UniFile, IsExtraUni=False, IsModuleUni=False) - - if type(self) == InfParser and self._Version < 0x00010005: + + if isinstance(self, InfParser) and self._Version < 0x00010005: # EDK module allows using defines as macros self._FileLocalMacros[Name] = Value self._Defines[Name] = Value @@ -370,7 +371,7 @@ class MetaFileParser(object): self._ValueList[1] = TokenList2[1] # keys else: self._ValueList[1] = TokenList[0] - if len(TokenList) == 2 and type(self) != DscParser: # value + if len(TokenList) == 2 and not isinstance(self, DscParser): # value self._ValueList[2] = ReplaceMacro(TokenList[1], self._Macros) if self._ValueList[1].count('_') != 4: @@ -390,7 +391,7 @@ class MetaFileParser(object): return Macros - ## Get section Macros that are applicable to current line, which may come from other sections + ## Get section Macros that are applicable to current line, which may come from other sections ## that share the same name while scope is wider def _GetApplicableSectionMacro(self): Macros = {} @@ -473,7 +474,7 @@ class InfParser(MetaFileParser): self.FileID = FileID else: self.FileID = self.TblFile.InsertFile(Filename, MODEL_FILE_INF) - + # parse the file line by line IsFindBlockComment = False @@ -561,7 +562,7 @@ class InfParser(MetaFileParser): NmakeLine = '' # section content - self._ValueList = ['','',''] + self._ValueList = ['', '', ''] # parse current line, result will be put in self._ValueList self._SectionParser[self._SectionType](self) if self._ValueList is None or self._ItemType == MODEL_META_DATA_DEFINE: @@ -591,7 +592,7 @@ class InfParser(MetaFileParser): ) Usage = '' if IsFindBlockComment: - EdkLogger.error("Parser", FORMAT_INVALID, "Open block comments (starting with /*) are expected to end with */", + EdkLogger.error("Parser", FORMAT_INVALID, "Open block comments (starting with /*) are expected to end with */", File=self.MetaFile) self._Done() @@ -818,7 +819,7 @@ class DscParser(MetaFileParser): # the owner item # self._IdMapping = {-1:-1} - + self.TblFile = EccGlobalData.gDb.TblFile self.FileID = -1 @@ -838,8 +839,8 @@ class DscParser(MetaFileParser): self.FileID = FileID else: self.FileID = self.TblFile.InsertFile(Filename, MODEL_FILE_DSC) - - + + for Index in range(0, len(Content)): Line = CleanString(Content[Index]) # skip empty line @@ -850,7 +851,7 @@ class DscParser(MetaFileParser): self._LineIndex = Index if self._InSubsection and self._Owner[-1] == -1: self._Owner.append(self._LastItem) - + # section header if Line[0] == TAB_SECTION_START and Line[-1] == TAB_SECTION_END: self._SectionType = MODEL_META_DATA_SECTION_HEADER @@ -920,7 +921,7 @@ class DscParser(MetaFileParser): ## Directive statement parser def _DirectiveParser(self): - self._ValueList = ['','',''] + self._ValueList = ['', '', ''] TokenList = GetSplitValueList(self._CurrentLine, ' ', 1) self._ValueList[0:len(TokenList)] = TokenList @@ -960,7 +961,7 @@ class DscParser(MetaFileParser): elif self._From > 0: EdkLogger.error('Parser', FORMAT_INVALID, "No '!include' allowed in included file", - ExtraData=self._CurrentLine, File=self.MetaFile, + ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1) # @@ -1110,7 +1111,7 @@ class DscParser(MetaFileParser): ## Override parent's method since we'll do all macro replacements in parser def _GetMacros(self): - Macros = dict( [('ARCH','IA32'), ('FAMILY','MSFT'),('TOOL_CHAIN_TAG','VS2008x86'),('TARGET','DEBUG')]) + Macros = dict( [('ARCH', 'IA32'), ('FAMILY', TAB_COMPILER_MSFT), ('TOOL_CHAIN_TAG', 'VS2008x86'), ('TARGET', 'DEBUG')]) Macros.update(self._FileLocalMacros) Macros.update(self._GetApplicableSectionMacro()) Macros.update(GlobalData.gEdkGlobal) @@ -1154,7 +1155,7 @@ class DscParser(MetaFileParser): MODEL_META_DATA_USER_EXTENSION : self._Skip, MODEL_META_DATA_CONDITIONAL_STATEMENT_ERROR : self._Skip, } - + self._RawTable = self._Table self._Table = MetaFileStorage(self._RawTable.Cur, self.MetaFile, MODEL_FILE_DSC, True) self._DirectiveStack = [] @@ -1183,8 +1184,8 @@ class DscParser(MetaFileParser): try: Processer[self._ItemType]() - except EvaluationException, Excpt: - # + except EvaluationException as Excpt: + # # Only catch expression evaluation error here. We need to report # the precise number of line on which the error occurred # @@ -1192,13 +1193,13 @@ class DscParser(MetaFileParser): # EdkLogger.error('Parser', FORMAT_INVALID, "Invalid expression: %s" % str(Excpt), # File=self._FileWithError, ExtraData=' '.join(self._ValueList), # Line=self._LineIndex+1) - except MacroException, Excpt: + except MacroException as Excpt: EdkLogger.error('Parser', FORMAT_INVALID, str(Excpt), - File=self._FileWithError, ExtraData=' '.join(self._ValueList), + File=self._FileWithError, ExtraData=' '.join(self._ValueList), Line=self._LineIndex+1) if self._ValueList is None: - continue + continue NewOwner = self._IdMapping.get(Owner, -1) self._Enabled = int((not self._DirectiveEvalStack) or (False not in self._DirectiveEvalStack)) @@ -1221,11 +1222,11 @@ class DscParser(MetaFileParser): self._IdMapping[Id] = self._LastItem RecordList = self._Table.GetAll() - + self._RawTable.Drop() self._Table.Drop() for Record in RecordList: - EccGlobalData.gDb.TblDsc.Insert(Record[1],Record[2],Record[3],Record[4],Record[5],Record[6],Record[7],Record[8],Record[9],Record[10],Record[11],Record[12],Record[13],Record[14]) + EccGlobalData.gDb.TblDsc.Insert(Record[1], Record[2], Record[3], Record[4], Record[5], Record[6], Record[7], Record[8], Record[9], Record[10], Record[11], Record[12], Record[13], Record[14]) GlobalData.gPlatformDefines.update(self._FileLocalMacros) self._PostProcessed = True self._Content = None @@ -1246,7 +1247,7 @@ class DscParser(MetaFileParser): def __RetrievePcdValue(self): Records = self._RawTable.Query(MODEL_PCD_FEATURE_FLAG, BelongsToItem=-1.0) - for TokenSpaceGuid,PcdName,Value,Dummy2,Dummy3,ID,Line in Records: + for TokenSpaceGuid, PcdName, Value, Dummy2, Dummy3, ID, Line in Records: Value, DatumType, MaxDatumSize = AnalyzePcdData(Value) # Only use PCD whose value is straitforward (no macro and PCD) if self.SymbolPattern.findall(Value): @@ -1255,20 +1256,20 @@ class DscParser(MetaFileParser): # Don't use PCD with different values. if Name in self._Symbols and self._Symbols[Name] != Value: self._Symbols.pop(Name) - continue + continue self._Symbols[Name] = Value Records = self._RawTable.Query(MODEL_PCD_FIXED_AT_BUILD, BelongsToItem=-1.0) - for TokenSpaceGuid,PcdName,Value,Dummy2,Dummy3,ID,Line in Records: + for TokenSpaceGuid, PcdName, Value, Dummy2, Dummy3, ID, Line in Records: Value, DatumType, MaxDatumSize = AnalyzePcdData(Value) # Only use PCD whose value is straitforward (no macro and PCD) if self.SymbolPattern.findall(Value): - continue + continue Name = TokenSpaceGuid+'.'+PcdName # Don't use PCD with different values. if Name in self._Symbols and self._Symbols[Name] != Value: self._Symbols.pop(Name) - continue + continue self._Symbols[Name] = Value def __ProcessDefine(self): @@ -1288,13 +1289,13 @@ class DscParser(MetaFileParser): SectionLocalMacros[Name] = Value elif self._ItemType == MODEL_META_DATA_GLOBAL_DEFINE: GlobalData.gEdkGlobal[Name] = Value - + # # Keyword in [Defines] section can be used as Macros # if (self._ItemType == MODEL_META_DATA_HEADER) and (self._SectionType == MODEL_META_DATA_HEADER): self._FileLocalMacros[Name] = Value - + self._ValueList = [Type, Name, Value] def __ProcessDirective(self): @@ -1305,19 +1306,19 @@ class DscParser(MetaFileParser): Macros.update(GlobalData.gGlobalDefines) try: Result = ValueExpression(self._ValueList[1], Macros)() - except SymbolNotFound, Exc: + except SymbolNotFound as Exc: EdkLogger.debug(EdkLogger.DEBUG_5, str(Exc), self._ValueList[1]) Result = False - except WrnExpression, Excpt: - # + except WrnExpression as Excpt: + # # Catch expression evaluation warning here. We need to report # the precise number of line and return the evaluation result # EdkLogger.warn('Parser', "Suspicious expression: %s" % str(Excpt), - File=self._FileWithError, ExtraData=' '.join(self._ValueList), + File=self._FileWithError, ExtraData=' '.join(self._ValueList), Line=self._LineIndex+1) Result = Excpt.result - except BadExpression, Exc: + except BadExpression as Exc: EdkLogger.debug(EdkLogger.DEBUG_5, str(Exc), self._ValueList[1]) Result = False @@ -1365,14 +1366,14 @@ class DscParser(MetaFileParser): # elif "ECP_SOURCE" in GlobalData.gCommandLineDefines.keys(): __IncludeMacros['ECP_SOURCE'] = GlobalData.gCommandLineDefines['ECP_SOURCE'] - + __IncludeMacros['EFI_SOURCE'] = GlobalData.gGlobalDefines['EFI_SOURCE'] __IncludeMacros['EDK_SOURCE'] = GlobalData.gGlobalDefines['EDK_SOURCE'] # - # Allow using MACROs comes from [Defines] section to keep compatible. + # Allow using MACROs comes from [Defines] section to keep compatible. # __IncludeMacros.update(self._Macros) - + IncludedFile = NormPath(ReplaceMacro(self._ValueList[1], __IncludeMacros, RaiseError=True)) # # First search the include file under the same directory as DSC file @@ -1386,14 +1387,14 @@ class DscParser(MetaFileParser): IncludedFile1 = PathClass(IncludedFile, GlobalData.gWorkspace) ErrorCode, ErrorInfo2 = IncludedFile1.Validate() if ErrorCode != 0: - EdkLogger.error('parser', ErrorCode, File=self._FileWithError, + EdkLogger.error('parser', ErrorCode, File=self._FileWithError, Line=self._LineIndex+1, ExtraData=ErrorInfo1 + "\n"+ ErrorInfo2) self._FileWithError = IncludedFile1 IncludedFileTable = MetaFileStorage(self._Table.Cur, IncludedFile1, MODEL_FILE_DSC, True) Owner = self._Content[self._ContentIndex-1][0] - Parser = DscParser(IncludedFile1, self._FileType, IncludedFileTable, + Parser = DscParser(IncludedFile1, self._FileType, IncludedFileTable, Owner=Owner, From=Owner) # set the parser status with current status @@ -1417,7 +1418,7 @@ class DscParser(MetaFileParser): self._Content.pop(self._ContentIndex-1) self._ValueList = None self._ContentIndex -= 1 - + def __ProcessSkuId(self): self._ValueList = [ReplaceMacro(Value, self._Macros, RaiseError=True) for Value in self._ValueList] @@ -1434,22 +1435,22 @@ class DscParser(MetaFileParser): # PCD value can be an expression # if len(ValueList) > 1 and ValueList[1] == TAB_VOID: - PcdValue = ValueList[0] + PcdValue = ValueList[0] try: ValueList[0] = ValueExpression(PcdValue, self._Macros)(True) - except WrnExpression, Value: - ValueList[0] = Value.result + except WrnExpression as Value: + ValueList[0] = Value.result else: PcdValue = ValueList[-1] try: ValueList[-1] = ValueExpression(PcdValue, self._Macros)(True) - except WrnExpression, Value: + except WrnExpression as Value: ValueList[-1] = Value.result - + if ValueList[-1] == 'True': ValueList[-1] = '1' if ValueList[-1] == 'False': - ValueList[-1] = '0' + ValueList[-1] = '0' self._ValueList[2] = '|'.join(ValueList) @@ -1548,7 +1549,7 @@ class DecParser(MetaFileParser): self.FileID = FileID else: self.FileID = self.TblFile.InsertFile(Filename, MODEL_FILE_DEC) - + for Index in range(0, len(Content)): Line, Comment = CleanString2(Content[Index]) self._CurrentLine = Line @@ -1571,7 +1572,7 @@ class DecParser(MetaFileParser): continue # section content - self._ValueList = ['','',''] + self._ValueList = ['', '', ''] self._SectionParser[self._SectionType[0]](self) if self._ValueList is None or self._ItemType == MODEL_META_DATA_DEFINE: self._ItemType = -1 @@ -1717,7 +1718,7 @@ class DecParser(MetaFileParser): GuidValue = GuidValue.lstrip(' {') HexList.append('0x' + str(GuidValue[2:])) Index += 1 - self._ValueList[1] = "{ %s, %s, %s, { %s, %s, %s, %s, %s, %s, %s, %s }}" % (HexList[0], HexList[1], HexList[2],HexList[3],HexList[4],HexList[5],HexList[6],HexList[7],HexList[8],HexList[9],HexList[10]) + self._ValueList[1] = "{ %s, %s, %s, { %s, %s, %s, %s, %s, %s, %s, %s }}" % (HexList[0], HexList[1], HexList[2], HexList[3], HexList[4], HexList[5], HexList[6], HexList[7], HexList[8], HexList[9], HexList[10]) else: EdkLogger.error('Parser', FORMAT_INVALID, "Invalid GUID value format", ExtraData=self._CurrentLine + \ @@ -1750,19 +1751,19 @@ class DecParser(MetaFileParser): " (.|||)", File=self.MetaFile, Line=self._LineIndex+1) - + ValueRe = re.compile(r'^\s*L?\".*\|.*\"') PtrValue = ValueRe.findall(TokenList[1]) - - # Has VOID* type string, may contain "|" character in the string. + + # Has VOID* type string, may contain "|" character in the string. if len(PtrValue) != 0: ptrValueList = re.sub(ValueRe, '', TokenList[1]) ValueList = GetSplitValueList(ptrValueList) ValueList[0] = PtrValue[0] else: ValueList = GetSplitValueList(TokenList[1]) - - + + # check if there's enough datum information given if len(ValueList) != 3: EdkLogger.error('Parser', FORMAT_INVALID, "Invalid PCD Datum information given", @@ -1792,7 +1793,7 @@ class DecParser(MetaFileParser): if not IsValid: EdkLogger.error('Parser', FORMAT_INVALID, Cause, ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1) - + if EccGlobalData.gConfig.UniCheckPCDInfo == '1' or EccGlobalData.gConfig.UniCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1': # check Description, Prompt information PatternDesc = re.compile('##\s*([\x21-\x7E\s]*)', re.S) @@ -1903,7 +1904,7 @@ class DecParser(MetaFileParser): ## Fdf # # This class defined the structure used in Fdf object -# +# # @param Filename: Input value for Ffilename of Fdf file, default is None # @param WorkspaceDir: Input value for current workspace directory, default is None # @@ -1911,7 +1912,7 @@ class Fdf(object): def __init__(self, Filename = None, IsToDatabase = False, WorkspaceDir = None, Database = None): self.WorkspaceDir = WorkspaceDir self.IsToDatabase = IsToDatabase - + self.Cur = Database.Cur self.TblFile = Database.TblFile self.TblFdf = Database.TblFdf @@ -1938,15 +1939,15 @@ class Fdf(object): self.FileList[Filename] = FileID return self.FileList[Filename] - - + + ## Load Fdf file # # Load the file if it exists # # @param Filename: Input value for filename of Fdf file # - def LoadFdfFile(self, Filename): + def LoadFdfFile(self, Filename): FileList = [] # # Parse Fdf file @@ -1991,7 +1992,7 @@ class UniParser(object): self.FileIn = None self.Missing = [] self.__read() - + def __read(self): try: self.FileIn = CodecOpenLongFilePath(self.FilePath, Mode='rb', Encoding='utf_8').read() @@ -2001,7 +2002,7 @@ class UniParser(object): self.FileIn = CodecOpenLongFilePath(self.FilePath, Mode='rb', Encoding='utf_16_le').read() except IOError: self.FileIn = "" - + def Start(self): if self.IsModuleUni: if self.IsExtraUni: @@ -2021,7 +2022,7 @@ class UniParser(object): self.PrintLog('STR_PACKAGE_ABSTRACT', PackageAbstract) PackageDescription = self.CheckKeyValid('STR_PACKAGE_DESCRIPTION') self.PrintLog('STR_PACKAGE_DESCRIPTION', PackageDescription) - + def CheckKeyValid(self, Key, Contents=None): if not Contents: Contents = self.FileIn @@ -2029,7 +2030,7 @@ class UniParser(object): if KeyPattern.search(Contents): return True return False - + def CheckPcdInfo(self, PcdCName): PromptKey = 'STR_%s_PROMPT' % PcdCName.replace('.', '_') PcdPrompt = self.CheckKeyValid(PromptKey) @@ -2037,7 +2038,7 @@ class UniParser(object): HelpKey = 'STR_%s_HELP' % PcdCName.replace('.', '_') PcdHelp = self.CheckKeyValid(HelpKey) self.PrintLog(HelpKey, PcdHelp) - + def PrintLog(self, Key, Value): if not Value and Key not in self.Missing: Msg = '%s is missing in the %s file.' % (Key, self.FileName)