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