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