1## @file 2# Parser a Inf file and Get specify section data. 3# 4# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR> 5# SPDX-License-Identifier: BSD-2-Clause-Patent 6# 7 8## Import Modules 9# 10 11import Common.EdkLogger as EdkLogger 12from Common.BuildToolError import * 13from Common.DataType import * 14 15 16class InfSectionParser(): 17 def __init__(self, FilePath): 18 self._FilePath = FilePath 19 self._FileSectionDataList = [] 20 self._ParserInf() 21 22 def _ParserInf(self): 23 FileLinesList = [] 24 UserExtFind = False 25 FindEnd = True 26 FileLastLine = False 27 SectionLine = '' 28 SectionData = [] 29 30 try: 31 with open(self._FilePath, "r") as File: 32 FileLinesList = File.readlines() 33 except BaseException: 34 EdkLogger.error("build", AUTOGEN_ERROR, 'File %s is opened failed.' % self._FilePath) 35 36 for Index in range(0, len(FileLinesList)): 37 line = str(FileLinesList[Index]).strip() 38 if Index + 1 == len(FileLinesList): 39 FileLastLine = True 40 NextLine = '' 41 else: 42 NextLine = str(FileLinesList[Index + 1]).strip() 43 if UserExtFind and FindEnd == False: 44 if line: 45 SectionData.append(line) 46 if line.startswith(TAB_SECTION_START) and line.endswith(TAB_SECTION_END): 47 SectionLine = line 48 UserExtFind = True 49 FindEnd = False 50 51 if (NextLine != '' and NextLine[0] == TAB_SECTION_START and \ 52 NextLine[-1] == TAB_SECTION_END) or FileLastLine: 53 UserExtFind = False 54 FindEnd = True 55 self._FileSectionDataList.append({SectionLine: SectionData[:]}) 56 del SectionData[:] 57 SectionLine = '' 58 59 # Get user extension TianoCore data 60 # 61 # @return: a list include some dictionary that key is section and value is a list contain all data. 62 def GetUserExtensionTianoCore(self): 63 UserExtensionTianoCore = [] 64 if not self._FileSectionDataList: 65 return UserExtensionTianoCore 66 for SectionDataDict in self._FileSectionDataList: 67 for key in SectionDataDict: 68 if key.lower().startswith("[userextensions") and key.lower().find('.tianocore.') > -1: 69 SectionLine = key.lstrip(TAB_SECTION_START).rstrip(TAB_SECTION_END) 70 SubSectionList = [SectionLine] 71 if str(SectionLine).find(TAB_COMMA_SPLIT) > -1: 72 SubSectionList = str(SectionLine).split(TAB_COMMA_SPLIT) 73 for SubSection in SubSectionList: 74 if SubSection.lower().find('.tianocore.') > -1: 75 UserExtensionTianoCore.append({SubSection: SectionDataDict[key]}) 76 return UserExtensionTianoCore 77 78 # Get depex expression 79 # 80 # @return: a list include some dictionary that key is section and value is a list contain all data. 81 def GetDepexExpresionList(self): 82 DepexExpressionList = [] 83 if not self._FileSectionDataList: 84 return DepexExpressionList 85 for SectionDataDict in self._FileSectionDataList: 86 for key in SectionDataDict: 87 if key.lower() == "[depex]" or key.lower().startswith("[depex."): 88 SectionLine = key.lstrip(TAB_SECTION_START).rstrip(TAB_SECTION_END) 89 SubSectionList = [SectionLine] 90 if str(SectionLine).find(TAB_COMMA_SPLIT) > -1: 91 SubSectionList = str(SectionLine).split(TAB_COMMA_SPLIT) 92 for SubSection in SubSectionList: 93 SectionList = SubSection.split(TAB_SPLIT) 94 SubKey = () 95 if len(SectionList) == 1: 96 SubKey = (TAB_ARCH_COMMON, TAB_ARCH_COMMON) 97 elif len(SectionList) == 2: 98 SubKey = (SectionList[1], TAB_ARCH_COMMON) 99 elif len(SectionList) == 3: 100 SubKey = (SectionList[1], SectionList[2]) 101 else: 102 EdkLogger.error("build", AUTOGEN_ERROR, 'Section %s is invalid.' % key) 103 DepexExpressionList.append({SubKey: SectionDataDict[key]}) 104 return DepexExpressionList 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120