2 # This file is used to provide method for process AsBuilt INF file. It will consumed by InfParser
4 # Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials are licensed and made available
7 # under the terms and conditions of the BSD License which accompanies this
8 # distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 from Library
import GlobalData
22 import Logger
.Log
as Logger
23 from Logger
import StringTable
as ST
24 from Logger
import ToolError
26 from Library
.String
import GetSplitValueList
27 from Library
.Misc
import GetHelpStringByRemoveHashKey
28 from Library
.Misc
import ValidFile
29 from Library
.Misc
import ProcessLineExtender
30 from Library
.ParserValidate
import IsValidPath
31 from Library
.Parsing
import MacroParser
32 from Parser
.InfParserMisc
import InfExpandMacro
34 from Library
import DataType
as DT
38 # Get the information from Library Instance INF file.
40 # @param string. A string start with # and followed by INF file path
41 # @param WorkSpace. The WorkSpace directory used to combined with INF file path.
43 # @return GUID, Version
44 def GetLibInstanceInfo(String
, WorkSpace
, LineNo
, CurrentInfFileName
):
49 OrignalString
= String
50 String
= String
.strip()
54 # Remove "#" characters at the beginning
56 String
= GetHelpStringByRemoveHashKey(String
)
57 String
= String
.strip()
60 # To deal with library instance specified by GUID and version
62 RegFormatGuidPattern
= re
.compile("\s*([0-9a-fA-F]){8}-"
66 "([0-9a-fA-F]){12}\s*")
67 VersionPattern
= re
.compile('[\t\s]*\d+(\.\d+)?[\t\s]*')
68 GuidMatchedObj
= RegFormatGuidPattern
.search(String
)
70 if String
.upper().startswith('GUID') and GuidMatchedObj
and 'Version' in String
:
71 VersionStr
= String
[String
.upper().find('VERSION') + 8:]
72 VersionMatchedObj
= VersionPattern
.search(VersionStr
)
74 Guid
= GuidMatchedObj
.group().strip()
75 Version
= VersionMatchedObj
.group().strip()
79 # To deal with library instance specified by file name
81 FileLinesList
= GetFileLineContent(String
, WorkSpace
, LineNo
, OrignalString
)
84 ReFindFileGuidPattern
= re
.compile("^\s*FILE_GUID\s*=.*$")
85 ReFindVerStringPattern
= re
.compile("^\s*VERSION_STRING\s*=.*$")
87 for Line
in FileLinesList
:
88 if ReFindFileGuidPattern
.match(Line
):
90 if ReFindVerStringPattern
.match(Line
):
94 FileGuidString
= GetSplitValueList(FileGuidString
, '=', 1)[1]
96 VerString
= GetSplitValueList(VerString
, '=', 1)[1]
98 return FileGuidString
, VerString
100 ## GetPackageListInfo
102 # Get the package information from INF file.
104 # @param string. A string start with # and followed by INF file path
105 # @param WorkSpace. The WorkSpace directory used to combined with INF file path.
107 # @return GUID, Version
108 def GetPackageListInfo(FileNameString
, WorkSpace
, LineNo
):
110 DefineSectionMacros
= {}
111 PackageSectionMacros
= {}
113 FileLinesList
= GetFileLineContent(FileNameString
, WorkSpace
, LineNo
, '')
115 RePackageHeader
= re
.compile('^\s*\[Packages.*\].*$')
116 ReDefineHeader
= re
.compile('^\s*\[Defines].*$')
118 PackageHederFlag
= False
119 DefineHeaderFlag
= False
121 for Line
in FileLinesList
:
125 if Line
.startswith('['):
126 PackageHederFlag
= False
127 DefineHeaderFlag
= False
129 if Line
.startswith("#"):
136 # Found [Packages] section
138 if RePackageHeader
.match(Line
):
139 PackageHederFlag
= True
143 # Found [Define] section
145 if ReDefineHeader
.match(Line
):
146 DefineHeaderFlag
= True
153 Name
, Value
= MacroParser((Line
, LineNo
),
155 DT
.MODEL_META_DATA_HEADER
,
159 DefineSectionMacros
[Name
] = Value
167 Name
, Value
= MacroParser((Line
, LineNo
),
169 DT
.MODEL_META_DATA_PACKAGE
,
172 PackageSectionMacros
[Name
] = Value
176 # Replace with Local section Macro and [Defines] section Macro.
178 Line
= InfExpandMacro(Line
, (FileNameString
, Line
, LineNo
), DefineSectionMacros
, PackageSectionMacros
, True)
180 Line
= GetSplitValueList(Line
, "#", 1)[0]
181 Line
= GetSplitValueList(Line
, "|", 1)[0]
182 PackageInfoList
.append(Line
)
184 return PackageInfoList
186 def GetFileLineContent(FileName
, WorkSpace
, LineNo
, OriginalString
):
192 # Validate file name exist.
194 FullFileName
= os
.path
.normpath(os
.path
.realpath(os
.path
.join(WorkSpace
, FileName
)))
195 if not (ValidFile(FullFileName
)):
199 # Validate file exist/format.
201 if not IsValidPath(FileName
, WorkSpace
):
207 FullFileName
= FullFileName
.replace('\\', '/')
208 Inputfile
= open(FullFileName
, "rb", 0)
210 FileLinesList
= Inputfile
.readlines()
211 except BaseException
:
212 Logger
.Error("InfParser", ToolError
.FILE_READ_FAILURE
, ST
.ERR_FILE_OPEN_FAILURE
, File
=FullFileName
)
215 except BaseException
:
216 Logger
.Error("InfParser",
217 ToolError
.FILE_READ_FAILURE
,
218 ST
.ERR_FILE_OPEN_FAILURE
,
221 FileLinesList
= ProcessLineExtender(FileLinesList
)
226 # Get all INF files from current workspace
229 def GetInfsFromWorkSpace(WorkSpace
):
231 for top
, dirs
, files
in os
.walk(WorkSpace
):
232 dirs
= dirs
# just for pylint
234 if File
.upper().endswith(".INF"):
235 InfFiles
.append(os
.path
.join(top
, File
))
240 # Get GUID and version from library instance file
243 def GetGuidVerFormLibInstance(Guid
, Version
, WorkSpace
, CurrentInfFileName
):
244 for InfFile
in GetInfsFromWorkSpace(WorkSpace
):
246 if InfFile
.strip().upper() == CurrentInfFileName
.strip().upper():
248 InfFile
= InfFile
.replace('\\', '/')
249 if InfFile
not in GlobalData
.gLIBINSTANCEDICT
:
250 InfFileObj
= open(InfFile
, "rb", 0)
251 GlobalData
.gLIBINSTANCEDICT
[InfFile
] = InfFileObj
253 InfFileObj
= GlobalData
.gLIBINSTANCEDICT
[InfFile
]
255 except BaseException
:
256 Logger
.Error("InfParser",
257 ToolError
.FILE_READ_FAILURE
,
258 ST
.ERR_FILE_OPEN_FAILURE
,
261 FileLinesList
= InfFileObj
.readlines()
262 FileLinesList
= ProcessLineExtender(FileLinesList
)
264 ReFindFileGuidPattern
= re
.compile("^\s*FILE_GUID\s*=.*$")
265 ReFindVerStringPattern
= re
.compile("^\s*VERSION_STRING\s*=.*$")
267 for Line
in FileLinesList
:
268 if ReFindFileGuidPattern
.match(Line
):
269 FileGuidString
= Line
270 if ReFindVerStringPattern
.match(Line
):
274 FileGuidString
= GetSplitValueList(FileGuidString
, '=', 1)[1]
276 VerString
= GetSplitValueList(VerString
, '=', 1)[1]
278 if FileGuidString
.strip().upper() == Guid
.upper() and \
279 VerString
.strip().upper() == Version
.upper():
282 except BaseException
:
283 Logger
.Error("InfParser", ToolError
.FILE_READ_FAILURE
, ST
.ERR_FILE_OPEN_FAILURE
, File
=InfFile
)