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