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