]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AutoGen/InfSectionParser.py
BaseTools: Replace StandardError with Expression
[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 - 2018, 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 FileLinesList = []
30 UserExtFind = False
31 FindEnd = True
32 FileLastLine = False
33 SectionLine = ''
34 SectionData = []
35
36 try:
37 FileLinesList = open(self._FilePath, "r", 0).readlines()
38 except BaseException:
39 EdkLogger.error("build", AUTOGEN_ERROR, 'File %s is opened failed.' % self._FilePath)
40
41 for Index in range(0, len(FileLinesList)):
42 line = str(FileLinesList[Index]).strip()
43 if Index + 1 == len(FileLinesList):
44 FileLastLine = True
45 NextLine = ''
46 else:
47 NextLine = str(FileLinesList[Index + 1]).strip()
48 if UserExtFind and FindEnd == False:
49 if line:
50 SectionData.append(line)
51 if line.startswith(TAB_SECTION_START) and line.endswith(TAB_SECTION_END):
52 SectionLine = line
53 UserExtFind = True
54 FindEnd = False
55
56 if (NextLine != '' and NextLine[0] == TAB_SECTION_START and \
57 NextLine[-1] == TAB_SECTION_END) or FileLastLine:
58 UserExtFind = False
59 FindEnd = True
60 self._FileSectionDataList.append({SectionLine: SectionData[:]})
61 del SectionData[:]
62 SectionLine = ''
63
64 # Get user extension TianoCore data
65 #
66 # @return: a list include some dictionary that key is section and value is a list contain all data.
67 def GetUserExtensionTianoCore(self):
68 UserExtensionTianoCore = []
69 if not self._FileSectionDataList:
70 return UserExtensionTianoCore
71 for SectionDataDict in self._FileSectionDataList:
72 for key in SectionDataDict:
73 if key.lower().startswith("[userextensions") and key.lower().find('.tianocore.') > -1:
74 SectionLine = key.lstrip(TAB_SECTION_START).rstrip(TAB_SECTION_END)
75 SubSectionList = [SectionLine]
76 if str(SectionLine).find(TAB_COMMA_SPLIT) > -1:
77 SubSectionList = str(SectionLine).split(TAB_COMMA_SPLIT)
78 for SubSection in SubSectionList:
79 if SubSection.lower().find('.tianocore.') > -1:
80 UserExtensionTianoCore.append({SubSection: SectionDataDict[key]})
81 return UserExtensionTianoCore
82
83 # Get depex expresion
84 #
85 # @return: a list include some dictionary that key is section and value is a list contain all data.
86 def GetDepexExpresionList(self):
87 DepexExpresionList = []
88 if not self._FileSectionDataList:
89 return DepexExpresionList
90 for SectionDataDict in self._FileSectionDataList:
91 for key in SectionDataDict:
92 if key.lower() == "[depex]" or key.lower().startswith("[depex."):
93 SectionLine = key.lstrip(TAB_SECTION_START).rstrip(TAB_SECTION_END)
94 SubSectionList = [SectionLine]
95 if str(SectionLine).find(TAB_COMMA_SPLIT) > -1:
96 SubSectionList = str(SectionLine).split(TAB_COMMA_SPLIT)
97 for SubSection in SubSectionList:
98 SectionList = SubSection.split(TAB_SPLIT)
99 SubKey = ()
100 if len(SectionList) == 1:
101 SubKey = (TAB_ARCH_COMMON, TAB_ARCH_COMMON)
102 elif len(SectionList) == 2:
103 SubKey = (SectionList[1], TAB_ARCH_COMMON)
104 elif len(SectionList) == 3:
105 SubKey = (SectionList[1], SectionList[2])
106 else:
107 EdkLogger.error("build", AUTOGEN_ERROR, 'Section %s is invalid.' % key)
108 DepexExpresionList.append({SubKey: SectionDataDict[key]})
109 return DepexExpresionList
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124