def GetFileLineContent(FileName, WorkSpace, LineNo, OriginalString): if not LineNo: LineNo = -1 # # Validate file name exist. # FullFileName = os.path.normpath( os.path.realpath(os.path.join(WorkSpace, FileName))) if not (ValidFile(FullFileName)): Logger.Error("InfParser", ToolError.FORMAT_INVALID, ST.ERR_FILELIST_EXIST % (FileName), File=GlobalData.gINF_MODULE_NAME, Line=LineNo, ExtraData=OriginalString) # # Validate file exist/format. # if not IsValidPath(FileName, WorkSpace): Logger.Error("InfParser", ToolError.FORMAT_INVALID, ST.ERR_INF_PARSER_FILE_NOT_EXIST_OR_NAME_INVALID % (FileName), File=GlobalData.gINF_MODULE_NAME, Line=LineNo, ExtraData=OriginalString) FileLinesList = [] try: FullFileName = FullFileName.replace('\\', '/') Inputfile = open(FullFileName, "rb", 0) try: FileLinesList = Inputfile.readlines() except BaseException: Logger.Error("InfParser", ToolError.FILE_READ_FAILURE, ST.ERR_FILE_OPEN_FAILURE, File=FullFileName) finally: Inputfile.close() except BaseException: Logger.Error("InfParser", ToolError.FILE_READ_FAILURE, ST.ERR_FILE_OPEN_FAILURE, File=FullFileName) FileLinesList = ProcessLineExtender(FileLinesList) return FileLinesList
def GetGuidVerFormLibInstance(Guid, Version, WorkSpace, CurrentInfFileName): for InfFile in GetInfsFromWorkSpace(WorkSpace): try: if InfFile.strip().upper() == CurrentInfFileName.strip().upper(): continue InfFile = InfFile.replace('\\', '/') if InfFile not in GlobalData.gLIBINSTANCEDICT: InfFileObj = open(InfFile, "rb", 0) GlobalData.gLIBINSTANCEDICT[InfFile] = InfFileObj else: InfFileObj = GlobalData.gLIBINSTANCEDICT[InfFile] except BaseException: Logger.Error("InfParser", ToolError.FILE_READ_FAILURE, ST.ERR_FILE_OPEN_FAILURE, File=InfFile) try: FileLinesList = InfFileObj.readlines() FileLinesList = ProcessLineExtender(FileLinesList) ReFindFileGuidPattern = re.compile("^\s*FILE_GUID\s*=.*$") ReFindVerStringPattern = re.compile("^\s*VERSION_STRING\s*=.*$") for Line in FileLinesList: if ReFindFileGuidPattern.match(Line): FileGuidString = Line if ReFindVerStringPattern.match(Line): VerString = Line if FileGuidString: FileGuidString = GetSplitValueList(FileGuidString, '=', 1)[1] if VerString: VerString = GetSplitValueList(VerString, '=', 1)[1] if FileGuidString.strip().upper() == Guid.upper() and \ VerString.strip().upper() == Version.upper(): return Guid, Version except BaseException: Logger.Error("InfParser", ToolError.FILE_READ_FAILURE, ST.ERR_FILE_OPEN_FAILURE, File=InfFile) finally: InfFileObj.close() return '', ''
def GetFileLineContent(FileName, WorkSpace, LineNo, OriginalString): if not LineNo: LineNo = -1 # # Validate file name exist. # FullFileName = os.path.normpath( os.path.realpath(os.path.join(WorkSpace, FileName))) if not (ValidFile(FullFileName)): return [] # # Validate file exist/format. # if not IsValidPath(FileName, WorkSpace): return [] FileLinesList = [] try: FullFileName = FullFileName.replace('\\', '/') Inputfile = open(FullFileName, "rb", 0) try: FileLinesList = Inputfile.readlines() except BaseException: Logger.Error("InfParser", ToolError.FILE_READ_FAILURE, ST.ERR_FILE_OPEN_FAILURE, File=FullFileName) finally: Inputfile.close() except BaseException: Logger.Error("InfParser", ToolError.FILE_READ_FAILURE, ST.ERR_FILE_OPEN_FAILURE, File=FullFileName) FileLinesList = ProcessLineExtender(FileLinesList) return FileLinesList
def ParseInfFile(self, Filename): Filename = NormPath(Filename) (Path, Name) = os.path.split(Filename) self.FullPath = Filename self.RelaPath = Path self.FileName = Name GlobalData.gINF_MODULE_DIR = Path GlobalData.gINF_MODULE_NAME = self.FullPath GlobalData.gIS_BINARY_INF = False # # Initialize common data # LineNo = 0 CurrentSection = DT.MODEL_UNKNOWN SectionLines = [] # # Flags # HeaderCommentStart = False HeaderCommentEnd = False HeaderStarLineNo = -1 BinaryHeaderCommentStart = False BinaryHeaderCommentEnd = False BinaryHeaderStarLineNo = -1 # # While Section ends. parse whole section contents. # NewSectionStartFlag = False FirstSectionStartFlag = False # # Parse file content # CommentBlock = [] # # Variables for Event/Hob/BootMode # self.EventList = [] self.HobList = [] self.BootModeList = [] SectionType = '' FileLinesList = OpenInfFile(Filename) # # One INF file can only has one [Defines] section. # DefineSectionParsedFlag = False # # Convert special characters in lines to space character. # FileLinesList = ConvertSpecialChar(FileLinesList) # # Process Line Extender # FileLinesList = ProcessLineExtender(FileLinesList) # # Process EdkI INF style comment if found # OrigLines = [Line for Line in FileLinesList] FileLinesList, EdkCommentStartPos = ProcessEdkComment(FileLinesList) # # Judge whether the INF file is Binary INF or not # if IsBinaryInf(FileLinesList): GlobalData.gIS_BINARY_INF = True InfSectionCommonDefObj = None for Line in FileLinesList: LineNo = LineNo + 1 Line = Line.strip() if (LineNo < len(FileLinesList) - 1): NextLine = FileLinesList[LineNo].strip() # # blank line # if (Line == '' or not Line) and LineNo == len(FileLinesList): LastSectionFalg = True # # check whether file header comment section started # if Line.startswith(DT.TAB_SPECIAL_COMMENT) and \ (Line.find(DT.TAB_HEADER_COMMENT) > -1) and \ not HeaderCommentStart and not HeaderCommentEnd: CurrentSection = DT.MODEL_META_DATA_FILE_HEADER # # Append the first line to section lines. # HeaderStarLineNo = LineNo SectionLines.append((Line, LineNo)) HeaderCommentStart = True continue # # Collect Header content. # if (Line.startswith(DT.TAB_COMMENT_SPLIT) and CurrentSection == DT.MODEL_META_DATA_FILE_HEADER) and\ HeaderCommentStart and not Line.startswith(DT.TAB_SPECIAL_COMMENT) and not\ HeaderCommentEnd and NextLine != '': SectionLines.append((Line, LineNo)) continue # # Header content end # if (Line.startswith(DT.TAB_SPECIAL_COMMENT) or not Line.strip().startswith("#")) and HeaderCommentStart \ and not HeaderCommentEnd: HeaderCommentEnd = True BinaryHeaderCommentStart = False BinaryHeaderCommentEnd = False HeaderCommentStart = False if Line.find(DT.TAB_BINARY_HEADER_COMMENT) > -1: self.InfHeaderParser(SectionLines, self.InfHeader, self.FileName) SectionLines = [] else: SectionLines.append((Line, LineNo)) # # Call Header comment parser. # self.InfHeaderParser(SectionLines, self.InfHeader, self.FileName) SectionLines = [] continue # # check whether binary header comment section started # if Line.startswith(DT.TAB_SPECIAL_COMMENT) and \ (Line.find(DT.TAB_BINARY_HEADER_COMMENT) > -1) and \ not BinaryHeaderCommentStart: SectionLines = [] CurrentSection = DT.MODEL_META_DATA_FILE_HEADER # # Append the first line to section lines. # BinaryHeaderStarLineNo = LineNo SectionLines.append((Line, LineNo)) BinaryHeaderCommentStart = True HeaderCommentEnd = True continue # # check whether there are more than one binary header exist # if Line.startswith(DT.TAB_SPECIAL_COMMENT) and BinaryHeaderCommentStart and \ not BinaryHeaderCommentEnd and (Line.find(DT.TAB_BINARY_HEADER_COMMENT) > -1): Logger.Error('Parser', FORMAT_INVALID, ST.ERR_MULTIPLE_BINARYHEADER_EXIST, File=Filename) # # Collect Binary Header content. # if (Line.startswith(DT.TAB_COMMENT_SPLIT) and CurrentSection == DT.MODEL_META_DATA_FILE_HEADER) and\ BinaryHeaderCommentStart and not Line.startswith(DT.TAB_SPECIAL_COMMENT) and not\ BinaryHeaderCommentEnd and NextLine != '': SectionLines.append((Line, LineNo)) continue # # Binary Header content end # if (Line.startswith(DT.TAB_SPECIAL_COMMENT) or not Line.strip().startswith(DT.TAB_COMMENT_SPLIT)) and \ BinaryHeaderCommentStart and not BinaryHeaderCommentEnd: SectionLines.append((Line, LineNo)) BinaryHeaderCommentStart = False # # Call Binary Header comment parser. # self.InfHeaderParser(SectionLines, self.InfBinaryHeader, self.FileName, True) SectionLines = [] BinaryHeaderCommentEnd = True continue # # Find a new section tab # Or at the last line of INF file, # need to process the last section. # LastSectionFalg = False if LineNo == len(FileLinesList): LastSectionFalg = True if Line.startswith(DT.TAB_COMMENT_SPLIT) and not Line.startswith( DT.TAB_SPECIAL_COMMENT): SectionLines.append((Line, LineNo)) if not LastSectionFalg: continue # # Encountered a section. start with '[' and end with ']' # if (Line.startswith(DT.TAB_SECTION_START) and \ Line.find(DT.TAB_SECTION_END) > -1) or LastSectionFalg: HeaderCommentEnd = True BinaryHeaderCommentEnd = True if not LastSectionFalg: # # check to prevent '#' inside section header # HeaderContent = Line[1:Line.find(DT.TAB_SECTION_END)] if HeaderContent.find(DT.TAB_COMMENT_SPLIT) != -1: Logger.Error( "InfParser", FORMAT_INVALID, ST.ERR_INF_PARSER_DEFINE_SECTION_HEADER_INVALID, File=self.FullPath, Line=LineNo, ExtraData=Line) # # Keep last time section header content for section parser # usage. # self.LastSectionHeaderContent = deepcopy( self.SectionHeaderContent) # # TailComments in section define. # TailComments = '' CommentIndex = Line.find(DT.TAB_COMMENT_SPLIT) if CommentIndex > -1: TailComments = Line[CommentIndex:] Line = Line[:CommentIndex] InfSectionCommonDefObj = InfSectionCommonDef() if TailComments != '': InfSectionCommonDefObj.SetTailComments(TailComments) if CommentBlock != '': InfSectionCommonDefObj.SetHeaderComments(CommentBlock) CommentBlock = [] # # Call section parser before section header parer to avoid encounter EDKI INF file # if CurrentSection == DT.MODEL_META_DATA_DEFINE: DefineSectionParsedFlag = self._CallSectionParsers( CurrentSection, DefineSectionParsedFlag, SectionLines, InfSectionCommonDefObj, LineNo) # # Compare the new section name with current # self.SectionHeaderParser(Line, self.FileName, LineNo) self._CheckSectionHeaders(Line, LineNo) SectionType = _ConvertSecNameToType( self.SectionHeaderContent[0][0]) if not FirstSectionStartFlag: CurrentSection = SectionType FirstSectionStartFlag = True else: NewSectionStartFlag = True else: SectionLines.append((Line, LineNo)) continue if LastSectionFalg: SectionLines, CurrentSection = self._ProcessLastSection( SectionLines, Line, LineNo, CurrentSection) # # End of section content collect. # Parser the section content collected previously. # if NewSectionStartFlag or LastSectionFalg: if CurrentSection != DT.MODEL_META_DATA_DEFINE or \ (LastSectionFalg and CurrentSection == DT.MODEL_META_DATA_DEFINE): DefineSectionParsedFlag = self._CallSectionParsers( CurrentSection, DefineSectionParsedFlag, SectionLines, InfSectionCommonDefObj, LineNo) CurrentSection = SectionType # # Clear section lines # SectionLines = [] if HeaderStarLineNo == -1: Logger.Error("InfParser", FORMAT_INVALID, ST.ERR_NO_SOURCE_HEADER, File=self.FullPath) if BinaryHeaderStarLineNo > -1 and HeaderStarLineNo > -1 and HeaderStarLineNo > BinaryHeaderStarLineNo: Logger.Error("InfParser", FORMAT_INVALID, ST.ERR_BINARY_HEADER_ORDER, File=self.FullPath) # # EDKII INF should not have EDKI style comment # if EdkCommentStartPos != -1: Logger.Error("InfParser", FORMAT_INVALID, ST.ERR_INF_PARSER_EDKI_COMMENT_IN_EDKII, File=self.FullPath, Line=EdkCommentStartPos + 1, ExtraData=OrigLines[EdkCommentStartPos]) # # extract [Event] [Hob] [BootMode] sections # self._ExtractEventHobBootMod(FileLinesList)