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