]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AutoGen/InfSectionParser.py
BaseTools: use set instead of list for a variable to be used with in
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / InfSectionParser.py
1 ## @file
2 # Parser a Inf file and Get specify section data.
3 #
4 # Copyright (c) 2007 - 2012, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13
14 ## Import Modules
15 #
16
17 import Common.EdkLogger as EdkLogger
18 from Common.BuildToolError import *
19 from Common.DataType import *
20
21
22 class InfSectionParser():
23 def __init__(self, FilePath):
24 self._FilePath = FilePath
25 self._FileSectionDataList = []
26 self._ParserInf()
27
28 def _ParserInf(self):
29 Filename = self._FilePath
30 FileLinesList = []
31 UserExtFind = False
32 FindEnd = True
33 FileLastLine = False
34 SectionLine = ''
35 SectionData = []
36
37 try:
38 FileLinesList = open(Filename, "r", 0).readlines()
39 except BaseException:
40 EdkLogger.error("build", AUTOGEN_ERROR, 'File %s is opened failed.' % Filename)
41
42 for Index in range(0, len(FileLinesList)):
43 line = str(FileLinesList[Index]).strip()
44 if Index + 1 == len(FileLinesList):
45 FileLastLine = True
46 NextLine = ''
47 else:
48 NextLine = str(FileLinesList[Index + 1]).strip()
49 if UserExtFind and FindEnd == False:
50 if line:
51 SectionData.append(line)
52 if line.lower().startswith(TAB_SECTION_START) and line.lower().endswith(TAB_SECTION_END):
53 SectionLine = line
54 UserExtFind = True
55 FindEnd = False
56
57 if (NextLine != '' and NextLine[0] == TAB_SECTION_START and \
58 NextLine[-1] == TAB_SECTION_END) or FileLastLine:
59 UserExtFind = False
60 FindEnd = True
61 self._FileSectionDataList.append({SectionLine: SectionData[:]})
62 SectionData = []
63 SectionLine = ''
64
65 # Get user extension TianoCore data
66 #
67 # @return: a list include some dictionary that key is section and value is a list contain all data.
68 def GetUserExtensionTianoCore(self):
69 UserExtensionTianoCore = []
70 if not self._FileSectionDataList:
71 return UserExtensionTianoCore
72 for SectionDataDict in self._FileSectionDataList:
73 for key in SectionDataDict.keys():
74 if key.lower().startswith("[userextensions") and key.lower().find('.tianocore.') > -1:
75 SectionLine = key.lstrip(TAB_SECTION_START).rstrip(TAB_SECTION_END)
76 SubSectionList = [SectionLine]
77 if str(SectionLine).find(TAB_COMMA_SPLIT) > -1:
78 SubSectionList = str(SectionLine).split(TAB_COMMA_SPLIT)
79 for SubSection in SubSectionList:
80 if SubSection.lower().find('.tianocore.') > -1:
81 UserExtensionTianoCore.append({SubSection: SectionDataDict[key]})
82 return UserExtensionTianoCore
83
84 # Get depex expresion
85 #
86 # @return: a list include some dictionary that key is section and value is a list contain all data.
87 def GetDepexExpresionList(self):
88 DepexExpresionList = []
89 if not self._FileSectionDataList:
90 return DepexExpresionList
91 for SectionDataDict in self._FileSectionDataList:
92 for key in SectionDataDict.keys():
93 if key.lower() == "[depex]" or key.lower().startswith("[depex."):
94 SectionLine = key.lstrip(TAB_SECTION_START).rstrip(TAB_SECTION_END)
95 SubSectionList = [SectionLine]
96 if str(SectionLine).find(TAB_COMMA_SPLIT) > -1:
97 SubSectionList = str(SectionLine).split(TAB_COMMA_SPLIT)
98 for SubSection in SubSectionList:
99 SectionList = SubSection.split(TAB_SPLIT)
100 SubKey = ()
101 if len(SectionList) == 1:
102 SubKey = (TAB_ARCH_COMMON, TAB_ARCH_COMMON)
103 elif len(SectionList) == 2:
104 SubKey = (SectionList[1], TAB_ARCH_COMMON)
105 elif len(SectionList) == 3:
106 SubKey = (SectionList[1], SectionList[2])
107 else:
108 EdkLogger.error("build", AUTOGEN_ERROR, 'Section %s is invalid.' % key)
109 DepexExpresionList.append({SubKey: SectionDataDict[key]})
110 return DepexExpresionList
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125