X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FCommon%2FFdfParserLite.py;h=dd822505d3d3dfaa0d7c570a562a922adb020fbc;hp=df287414db6f6c847669001ff47eae27a79218d6;hb=5bcf1d5671061bbcc3ee752fdf9b9d7bcce84665;hpb=2eb370ffdb179a865a65a3bca40d284958e42424 diff --git a/BaseTools/Source/Python/Common/FdfParserLite.py b/BaseTools/Source/Python/Common/FdfParserLite.py index df287414db..dd822505d3 100644 --- a/BaseTools/Source/Python/Common/FdfParserLite.py +++ b/BaseTools/Source/Python/Common/FdfParserLite.py @@ -22,6 +22,8 @@ import CommonDataClass.FdfClass from Common.LongFilePathSupport import OpenLongFilePath as open from Common.MultipleWorkspace import MultipleWorkspace as mws from Common.RangeExpression import RangeExpression +from Common.GlobalData import * +import string ##define T_CHAR_SPACE ' ' ##define T_CHAR_NULL '\0' @@ -47,6 +49,9 @@ InputMacroDict = {} # All Macro values when parsing file, not replace existing Macro AllMacroList = [] +FileExtensionPattern = re.compile(r'([a-zA-Z][a-zA-Z0-9]*)') +TokenFindPattern = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)') + def GetRealFileLine (File, Line): InsertedLines = 0 @@ -179,21 +184,6 @@ class FdfParser(object): self.__WipeOffArea = [] - ## __IsWhiteSpace() method - # - # Whether char at current FileBufferPos is whitespace - # - # @param self The object pointer - # @param Char The char to test - # @retval True The char is a kind of white space - # @retval False The char is NOT a kind of white space - # - def __IsWhiteSpace(self, Char): - if Char in (T_CHAR_NULL, T_CHAR_CR, T_CHAR_SPACE, T_CHAR_TAB, T_CHAR_LF): - return True - else: - return False - ## __SkipWhiteSpace() method # # Skip white spaces from current char, return number of chars skipped @@ -353,7 +343,7 @@ class FdfParser(object): if Profile.FileName == File and Profile.MacroName == Name and Profile.DefinedAtLine <= Line: Value = Profile.MacroValue - if Value != None: + if Value is not None: Str = Str.replace('$(' + Name + ')', Value) MacroEnd = MacroStart + len(Value) @@ -676,8 +666,8 @@ class FdfParser(object): FileLineTuple = GetRealFileLine(self.FileName, Line) if Name in InputMacroDict: MacroValue = InputMacroDict[Name] - if Op == None: - if Value == 'Bool' and MacroValue == None or MacroValue.upper() == 'FALSE': + if Op is None: + if Value == 'Bool' and MacroValue is None or MacroValue.upper() == 'FALSE': return False return True elif Op == '!=': @@ -691,7 +681,7 @@ class FdfParser(object): else: return False else: - if (self.__IsHex(Value) or Value.isdigit()) and (self.__IsHex(MacroValue) or (MacroValue != None and MacroValue.isdigit())): + if (self.__IsHex(Value) or Value.isdigit()) and (self.__IsHex(MacroValue) or (MacroValue is not None and MacroValue.isdigit())): InputVal = long(Value, 0) MacroVal = long(MacroValue, 0) if Op == '>': @@ -721,8 +711,8 @@ class FdfParser(object): for Profile in AllMacroList: if Profile.FileName == FileLineTuple[0] and Profile.MacroName == Name and Profile.DefinedAtLine <= FileLineTuple[1]: - if Op == None: - if Value == 'Bool' and Profile.MacroValue == None or Profile.MacroValue.upper() == 'FALSE': + if Op is None: + if Value == 'Bool' and Profile.MacroValue is None or Profile.MacroValue.upper() == 'FALSE': return False return True elif Op == '!=': @@ -736,7 +726,7 @@ class FdfParser(object): else: return False else: - if (self.__IsHex(Value) or Value.isdigit()) and (self.__IsHex(Profile.MacroValue) or (Profile.MacroValue != None and Profile.MacroValue.isdigit())): + if (self.__IsHex(Value) or Value.isdigit()) and (self.__IsHex(Profile.MacroValue) or (Profile.MacroValue is not None and Profile.MacroValue.isdigit())): InputVal = long(Value, 0) MacroVal = long(Profile.MacroValue, 0) if Op == '>': @@ -932,7 +922,7 @@ class FdfParser(object): if not self.__GetNextToken(): return False - if RangeExpression.RegGuidPattern.match(self.__Token) != None: + if gGuidPattern.match(self.__Token) is not None: return True else: self.__UndoToken() @@ -971,32 +961,13 @@ class FdfParser(object): self.__GetOneChar() - ## __HexDigit() method - # - # Whether char input is a Hex data bit - # - # @param self The object pointer - # @param TempChar The char to test - # @retval True The char is a Hex data bit - # @retval False The char is NOT a Hex data bit - # - def __HexDigit(self, TempChar): - if (TempChar >= 'a' and TempChar <= 'f') or (TempChar >= 'A' and TempChar <= 'F') \ - or (TempChar >= '0' and TempChar <= '9'): - return True - else: - return False - def __IsHex(self, HexStr): if not HexStr.upper().startswith("0X"): return False if len(self.__Token) <= 2: return False - charList = [c for c in HexStr[2 : ] if not self.__HexDigit( c)] - if len(charList) == 0: - return True - else: - return False + return True if all(x in string.hexdigits for x in HexStr[2:]) else False + ## __GetNextHexNumber() method # # Get next HEX data before a seperator @@ -1451,7 +1422,7 @@ class FdfParser(object): pass for Item in Obj.BlockSizeList: - if Item[0] == None or Item[1] == None: + if Item[0] is None or Item[1] is None: raise Warning("expected block statement for Fd Section", self.FileName, self.CurrentLineNumber) return True @@ -2091,8 +2062,7 @@ class FdfParser(object): if self.__GetNextToken(): - p = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)') - if p.match(self.__Token): + if TokenFindPattern.match(self.__Token): FfsInfObj.KeyStringList.append(self.__Token) if not self.__IsToken(","): return @@ -2101,7 +2071,7 @@ class FdfParser(object): return while self.__GetNextToken(): - if not p.match(self.__Token): + if not TokenFindPattern.match(self.__Token): raise Warning("expected KeyString \"Target_Tag_Arch\" At Line ", self.FileName, self.CurrentLineNumber) FfsInfObj.KeyStringList.append(self.__Token) @@ -2160,13 +2130,12 @@ class FdfParser(object): # # Check whether reloc strip flag can be set for a file type. # - # @param self The object pointer # @param FileType The file type to check with # @retval True This type could have relocation strip flag # @retval False No way to have it # - - def __FileCouldHaveRelocFlag (self, FileType): + @staticmethod + def __FileCouldHaveRelocFlag (FileType): if FileType in ('SEC', 'PEI_CORE', 'PEIM', 'PEI_DXE_COMBO'): return True else: @@ -2176,13 +2145,12 @@ class FdfParser(object): # # Check whether reloc strip flag can be set for a section type. # - # @param self The object pointer # @param SectionType The section type to check with # @retval True This type could have relocation strip flag # @retval False No way to have it # - - def __SectionCouldHaveRelocFlag (self, SectionType): + @staticmethod + def __SectionCouldHaveRelocFlag (SectionType): if SectionType in ('TE', 'PE32'): return True else: @@ -2249,12 +2217,11 @@ class FdfParser(object): def __GetFileOpts(self, FfsFileObj): if self.__GetNextToken(): - Pattern = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)') - if Pattern.match(self.__Token): + if TokenFindPattern.match(self.__Token): FfsFileObj.KeyStringList.append(self.__Token) if self.__IsToken(","): while self.__GetNextToken(): - if not Pattern.match(self.__Token): + if not TokenFindPattern.match(self.__Token): raise Warning("expected KeyString \"Target_Tag_Arch\" At Line ", self.FileName, self.CurrentLineNumber) FfsFileObj.KeyStringList.append(self.__Token) @@ -2420,7 +2387,7 @@ class FdfParser(object): FvImageSectionObj = CommonDataClass.FdfClass.FvImageSectionClassObject() FvImageSectionObj.Alignment = AlignValue - if FvObj != None: + if FvObj is not None: FvImageSectionObj.Fv = FvObj FvImageSectionObj.FvName = None else: @@ -2842,8 +2809,7 @@ class FdfParser(object): Ext = "" if self.__GetNextToken(): - Pattern = re.compile(r'([a-zA-Z][a-zA-Z0-9]*)') - if Pattern.match(self.__Token): + if FileExtensionPattern.match(self.__Token): Ext = self.__Token return '.' + Ext else: @@ -2900,12 +2866,11 @@ class FdfParser(object): KeyStringList = [] if self.__GetNextToken(): - Pattern = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)') - if Pattern.match(self.__Token): + if TokenFindPattern.match(self.__Token): KeyStringList.append(self.__Token) if self.__IsToken(","): while self.__GetNextToken(): - if not Pattern.match(self.__Token): + if not TokenFindPattern.match(self.__Token): raise Warning("expected KeyString \"Target_Tag_Arch\" At Line ", self.FileName, self.CurrentLineNumber) KeyStringList.append(self.__Token) @@ -2940,7 +2905,7 @@ class FdfParser(object): Rule.CheckSum = CheckSum Rule.Fixed = Fixed Rule.KeyStringList = KeyStringList - if KeepReloc != None: + if KeepReloc is not None: Rule.KeepReloc = KeepReloc while True: @@ -2967,7 +2932,7 @@ class FdfParser(object): Rule.Fixed = Fixed Rule.FileExtension = Ext Rule.KeyStringList = KeyStringList - if KeepReloc != None: + if KeepReloc is not None: Rule.KeepReloc = KeepReloc return Rule @@ -3010,7 +2975,7 @@ class FdfParser(object): Rule.Fixed = Fixed Rule.FileName = self.__Token Rule.KeyStringList = KeyStringList - if KeepReloc != None: + if KeepReloc is not None: Rule.KeepReloc = KeepReloc return Rule @@ -3147,7 +3112,7 @@ class FdfParser(object): EfiSectionObj.KeepReloc = False else: EfiSectionObj.KeepReloc = True - if Obj.KeepReloc != None and Obj.KeepReloc != EfiSectionObj.KeepReloc: + if Obj.KeepReloc is not None and Obj.KeepReloc != EfiSectionObj.KeepReloc: raise Warning("Section type %s has reloc strip flag conflict with Rule At Line %d" % (EfiSectionObj.SectionType, self.CurrentLineNumber), self.FileName, self.CurrentLineNumber) else: raise Warning("Section type %s could not have reloc strip flag At Line %d" % (EfiSectionObj.SectionType, self.CurrentLineNumber), self.FileName, self.CurrentLineNumber) @@ -3185,12 +3150,12 @@ class FdfParser(object): # # Get whether a section could be optional # - # @param self The object pointer # @param SectionType The section type to check # @retval True section could be optional # @retval False section never optional # - def __RuleSectionCouldBeOptional(self, SectionType): + @staticmethod + def __RuleSectionCouldBeOptional(SectionType): if SectionType in ("DXE_DEPEX", "UI", "VERSION", "PEI_DEPEX", "RAW", "SMM_DEPEX"): return True else: @@ -3200,12 +3165,12 @@ class FdfParser(object): # # Get whether a section could have build number information # - # @param self The object pointer # @param SectionType The section type to check # @retval True section could have build number information # @retval False section never have build number information # - def __RuleSectionCouldHaveBuildNum(self, SectionType): + @staticmethod + def __RuleSectionCouldHaveBuildNum(SectionType): if SectionType in ("VERSION"): return True else: @@ -3215,12 +3180,12 @@ class FdfParser(object): # # Get whether a section could have string # - # @param self The object pointer # @param SectionType The section type to check # @retval True section could have string # @retval False section never have string # - def __RuleSectionCouldHaveString(self, SectionType): + @staticmethod + def __RuleSectionCouldHaveString(SectionType): if SectionType in ("UI", "VERSION"): return True else: @@ -3455,7 +3420,7 @@ class FdfParser(object): raise Warning("expected Component type At Line ", self.FileName, self.CurrentLineNumber) if self.__Token not in ("FIT", "PAL_B", "PAL_A", "OEM"): if not self.__Token.startswith("0x") or len(self.__Token) < 3 or len(self.__Token) > 4 or \ - not self.__HexDigit(self.__Token[2]) or not self.__HexDigit(self.__Token[-1]): + not self.__Token[2] in string.hexdigits or not self.__Token[-1] in string.hexdigits: raise Warning("Unknown location type At line ", self.FileName, self.CurrentLineNumber) CompStatementObj.CompType = self.__Token @@ -3469,7 +3434,7 @@ class FdfParser(object): raise Warning("expected Component version At Line ", self.FileName, self.CurrentLineNumber) Pattern = re.compile('-$|[0-9]{0,1}[0-9]{1}\.[0-9]{0,1}[0-9]{1}') - if Pattern.match(self.__Token) == None: + if Pattern.match(self.__Token) is None: raise Warning("Unknown version format At line ", self.FileName, self.CurrentLineNumber) CompStatementObj.CompVer = self.__Token @@ -3542,7 +3507,7 @@ class FdfParser(object): for elementRegion in FdObj.RegionList: if elementRegion.RegionType == 'FV': for elementRegionData in elementRegion.RegionDataList: - if elementRegionData != None and elementRegionData.upper() not in FvList: + if elementRegionData is not None and elementRegionData.upper() not in FvList: FvList.append(elementRegionData.upper()) return FvList @@ -3559,9 +3524,9 @@ class FdfParser(object): for FfsObj in FvObj.FfsList: if isinstance(FfsObj, FfsFileStatement.FileStatement): - if FfsObj.FvName != None and FfsObj.FvName.upper() not in RefFvList: + if FfsObj.FvName is not None and FfsObj.FvName.upper() not in RefFvList: RefFvList.append(FfsObj.FvName.upper()) - elif FfsObj.FdName != None and FfsObj.FdName.upper() not in RefFdList: + elif FfsObj.FdName is not None and FfsObj.FdName.upper() not in RefFdList: RefFdList.append(FfsObj.FdName.upper()) else: self.__GetReferencedFdFvTupleFromSection(FfsObj, RefFdList, RefFvList) @@ -3582,9 +3547,9 @@ class FdfParser(object): while SectionStack != []: SectionObj = SectionStack.pop() if isinstance(SectionObj, FvImageSection.FvImageSection): - if SectionObj.FvName != None and SectionObj.FvName.upper() not in FvList: + if SectionObj.FvName is not None and SectionObj.FvName.upper() not in FvList: FvList.append(SectionObj.FvName.upper()) - if SectionObj.Fv != None and SectionObj.Fv.UiFvName != None and SectionObj.Fv.UiFvName.upper() not in FvList: + if SectionObj.Fv is not None and SectionObj.Fv.UiFvName is not None and SectionObj.Fv.UiFvName.upper() not in FvList: FvList.append(SectionObj.Fv.UiFvName.upper()) self.__GetReferencedFdFvTuple(SectionObj.Fv, FdList, FvList)