BaseTools: Use absolute import in Eot
[mirror_edk2.git] / BaseTools / Source / Python / Eot / InfParserLite.py
CommitLineData
52302d4d
LG
1## @file\r
2# This file is used to parse INF file of EDK project\r
3#\r
1be2ed90 4# Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>\r
40d841f6 5# This program and the accompanying materials\r
52302d4d
LG
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##\r
15# Import Modules\r
16#\r
72443dd2 17from __future__ import print_function\r
64429fbd 18from __future__ import absolute_import\r
1be2ed90 19import Common.LongFilePathOs as os\r
52302d4d
LG
20import Common.EdkLogger as EdkLogger\r
21from Common.DataType import *\r
22from CommonDataClass.DataClass import *\r
23from Common.Identification import *\r
5a57246e 24from Common.StringUtils import *\r
64429fbd
GL
25from .Parser import *\r
26from . import Database\r
52302d4d
LG
27\r
28## EdkInfParser() class\r
29#\r
30# This class defined basic INF object which is used by inheriting\r
31#\r
32# @param object: Inherited from object class\r
33#\r
34class EdkInfParser(object):\r
35 ## The constructor\r
36 #\r
37 # @param self: The object pointer\r
38 # @param Filename: INF file name\r
39 # @param Database: Eot database\r
40 # @param SourceFileList: A list for all source file belonging this INF file\r
41 # @param SourceOverridePath: Override path for source file\r
42 # @param Edk_Source: Envirnoment variable EDK_SOURCE\r
43 # @param Efi_Source: Envirnoment variable EFI_SOURCE\r
44 #\r
45 def __init__(self, Filename = None, Database = None, SourceFileList = None, SourceOverridePath = None, Edk_Source = None, Efi_Source = None):\r
46 self.Identification = Identification()\r
47 self.Sources = []\r
48 self.Macros = {}\r
49\r
50 self.Cur = Database.Cur\r
51 self.TblFile = Database.TblFile\r
52 self.TblInf = Database.TblInf\r
53 self.FileID = -1\r
54 self.SourceOverridePath = SourceOverridePath\r
55\r
56 # Load Inf file if filename is not None\r
4231a819 57 if Filename is not None:\r
52302d4d
LG
58 self.LoadInfFile(Filename)\r
59\r
60 if SourceFileList:\r
61 for Item in SourceFileList:\r
62 self.TblInf.Insert(MODEL_EFI_SOURCE_FILE, Item, '', '', '', '', 'COMMON', -1, self.FileID, -1, -1, -1, -1, 0)\r
63\r
64\r
65 ## LoadInffile() method\r
66 #\r
67 # Load INF file and insert a record in database\r
68 #\r
69 # @param self: The object pointer\r
70 # @param Filename: Input value for filename of Inf file\r
71 #\r
72 def LoadInfFile(self, Filename = None):\r
73 # Insert a record for file\r
74 Filename = NormPath(Filename)\r
75 self.Identification.FileFullPath = Filename\r
76 (self.Identification.FileRelativePath, self.Identification.FileName) = os.path.split(Filename)\r
77\r
78 self.FileID = self.TblFile.InsertFile(Filename, MODEL_FILE_INF)\r
79\r
80 self.ParseInf(PreProcess(Filename, False), self.Identification.FileRelativePath, Filename)\r
81\r
82 ## ParserSource() method\r
83 #\r
84 # Parse Source section and insert records in database\r
85 #\r
86 # @param self: The object pointer\r
87 # @param CurrentSection: current section name\r
88 # @param SectionItemList: the item belonging current section\r
89 # @param ArchList: A list for arch for this section\r
90 # @param ThirdList: A list for third item for this section\r
91 #\r
92 def ParserSource(self, CurrentSection, SectionItemList, ArchList, ThirdList):\r
93 for Index in range(0, len(ArchList)):\r
94 Arch = ArchList[Index]\r
95 Third = ThirdList[Index]\r
96 if Arch == '':\r
97 Arch = TAB_ARCH_COMMON\r
98\r
99 for Item in SectionItemList:\r
100 if CurrentSection.upper() == 'defines'.upper():\r
101 (Name, Value) = AddToSelfMacro(self.Macros, Item[0])\r
102 self.TblInf.Insert(MODEL_META_DATA_HEADER, Name, Value, Third, '', '', Arch, -1, self.FileID, Item[1], -1, Item[1], -1, 0)\r
103\r
104 ## ParseInf() method\r
105 #\r
106 # Parse INF file and get sections information\r
107 #\r
108 # @param self: The object pointer\r
109 # @param Lines: contents of INF file\r
110 # @param FileRelativePath: relative path of the file\r
111 # @param Filename: file name of INF file\r
112 #\r
113 def ParseInf(self, Lines = [], FileRelativePath = '', Filename = ''):\r
114 IfDefList, SectionItemList, CurrentSection, ArchList, ThirdList, IncludeFiles = \\r
115 [], [], TAB_UNKNOWN, [], [], []\r
116 LineNo = 0\r
117\r
118 for Line in Lines:\r
119 LineNo = LineNo + 1\r
120 if Line == '':\r
121 continue\r
122 if Line.startswith(TAB_SECTION_START) and Line.endswith(TAB_SECTION_END):\r
123 self.ParserSource(CurrentSection, SectionItemList, ArchList, ThirdList)\r
124\r
125 # Parse the new section\r
126 SectionItemList = []\r
127 ArchList = []\r
128 ThirdList = []\r
129 # Parse section name\r
130 CurrentSection = ''\r
131 LineList = GetSplitValueList(Line[len(TAB_SECTION_START):len(Line) - len(TAB_SECTION_END)], TAB_COMMA_SPLIT)\r
132 for Item in LineList:\r
133 ItemList = GetSplitValueList(Item, TAB_SPLIT)\r
134 if CurrentSection == '':\r
135 CurrentSection = ItemList[0]\r
136 else:\r
137 if CurrentSection != ItemList[0]:\r
138 EdkLogger.error("Parser", PARSER_ERROR, "Different section names '%s' and '%s' are found in one section definition, this is not allowed." % (CurrentSection, ItemList[0]), File=Filename, Line=LineNo)\r
139 ItemList.append('')\r
140 ItemList.append('')\r
141 if len(ItemList) > 5:\r
142 RaiseParserError(Line, CurrentSection, Filename, '', LineNo)\r
143 else:\r
144 ArchList.append(ItemList[1].upper())\r
145 ThirdList.append(ItemList[2])\r
146\r
147 continue\r
148\r
149 # Add a section item\r
150 SectionItemList.append([Line, LineNo])\r
151 # End of parse\r
152\r
153 self.ParserSource(CurrentSection, SectionItemList, ArchList, ThirdList)\r
154 #End of For\r
155\r
156##\r
157#\r
158# This acts like the main() function for the script, unless it is 'import'ed into another\r
159# script.\r
160#\r
161if __name__ == '__main__':\r
162 EdkLogger.Initialize()\r
163 EdkLogger.SetLevel(EdkLogger.QUIET)\r
164\r
165 Db = Database.Database('Inf.db')\r
166 Db.InitDatabase()\r
167 P = EdkInfParser(os.path.normpath("C:\Framework\Edk\Sample\Platform\Nt32\Dxe\PlatformBds\PlatformBds.inf"), Db, '', '')\r
168 for Inf in P.Sources:\r
72443dd2 169 print(Inf)\r
52302d4d 170 for Item in P.Macros:\r
72443dd2 171 print(Item, P.Macros[Item])\r
52302d4d 172\r
72443dd2 173 Db.Close()\r