2 # This file is used to provide method for process AsBuilt INF file. It will consumed by InfParser
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
15 from Library
import GlobalData
16 import Logger
.Log
as Logger
17 from Logger
import StringTable
as ST
18 from Logger
import ToolError
20 from Library
.StringUtils
import GetSplitValueList
21 from Library
.Misc
import GetHelpStringByRemoveHashKey
22 from Library
.Misc
import ValidFile
23 from Library
.Misc
import ProcessLineExtender
24 from Library
.ParserValidate
import IsValidPath
25 from Library
.Parsing
import MacroParser
26 from Parser
.InfParserMisc
import InfExpandMacro
28 from Library
import DataType
as DT
32 # Get the information from Library Instance INF file.
34 # @param string. A string start with # and followed by INF file path
35 # @param WorkSpace. The WorkSpace directory used to combined with INF file path.
37 # @return GUID, Version
38 def GetLibInstanceInfo(String
, WorkSpace
, LineNo
, CurrentInfFileName
):
43 OriginalString
= String
44 String
= String
.strip()
48 # Remove "#" characters at the beginning
50 String
= GetHelpStringByRemoveHashKey(String
)
51 String
= String
.strip()
54 # To deal with library instance specified by GUID and version
56 RegFormatGuidPattern
= re
.compile("\s*([0-9a-fA-F]){8}-"
60 "([0-9a-fA-F]){12}\s*")
61 VersionPattern
= re
.compile('[\t\s]*\d+(\.\d+)?[\t\s]*')
62 GuidMatchedObj
= RegFormatGuidPattern
.search(String
)
64 if String
.upper().startswith('GUID') and GuidMatchedObj
and 'Version' in String
:
65 VersionStr
= String
[String
.upper().find('VERSION') + 8:]
66 VersionMatchedObj
= VersionPattern
.search(VersionStr
)
68 Guid
= GuidMatchedObj
.group().strip()
69 Version
= VersionMatchedObj
.group().strip()
73 # To deal with library instance specified by file name
75 FileLinesList
= GetFileLineContent(String
, WorkSpace
, LineNo
, OriginalString
)
78 ReFindFileGuidPattern
= re
.compile("^\s*FILE_GUID\s*=.*$")
79 ReFindVerStringPattern
= re
.compile("^\s*VERSION_STRING\s*=.*$")
81 for Line
in FileLinesList
:
82 if ReFindFileGuidPattern
.match(Line
):
84 if ReFindVerStringPattern
.match(Line
):
88 FileGuidString
= GetSplitValueList(FileGuidString
, '=', 1)[1]
90 VerString
= GetSplitValueList(VerString
, '=', 1)[1]
92 return FileGuidString
, VerString
96 # Get the package information from INF file.
98 # @param string. A string start with # and followed by INF file path
99 # @param WorkSpace. The WorkSpace directory used to combined with INF file path.
101 # @return GUID, Version
102 def GetPackageListInfo(FileNameString
, WorkSpace
, LineNo
):
104 DefineSectionMacros
= {}
105 PackageSectionMacros
= {}
107 FileLinesList
= GetFileLineContent(FileNameString
, WorkSpace
, LineNo
, '')
109 RePackageHeader
= re
.compile('^\s*\[Packages.*\].*$')
110 ReDefineHeader
= re
.compile('^\s*\[Defines].*$')
112 PackageHederFlag
= False
113 DefineHeaderFlag
= False
115 for Line
in FileLinesList
:
119 if Line
.startswith('['):
120 PackageHederFlag
= False
121 DefineHeaderFlag
= False
123 if Line
.startswith("#"):
130 # Found [Packages] section
132 if RePackageHeader
.match(Line
):
133 PackageHederFlag
= True
137 # Found [Define] section
139 if ReDefineHeader
.match(Line
):
140 DefineHeaderFlag
= True
147 Name
, Value
= MacroParser((Line
, LineNo
),
149 DT
.MODEL_META_DATA_HEADER
,
153 DefineSectionMacros
[Name
] = Value
161 Name
, Value
= MacroParser((Line
, LineNo
),
163 DT
.MODEL_META_DATA_PACKAGE
,
166 PackageSectionMacros
[Name
] = Value
170 # Replace with Local section Macro and [Defines] section Macro.
172 Line
= InfExpandMacro(Line
, (FileNameString
, Line
, LineNo
), DefineSectionMacros
, PackageSectionMacros
, True)
174 Line
= GetSplitValueList(Line
, "#", 1)[0]
175 Line
= GetSplitValueList(Line
, "|", 1)[0]
176 PackageInfoList
.append(Line
)
178 return PackageInfoList
180 def GetFileLineContent(FileName
, WorkSpace
, LineNo
, OriginalString
):
186 # Validate file name exist.
188 FullFileName
= os
.path
.normpath(os
.path
.realpath(os
.path
.join(WorkSpace
, FileName
)))
189 if not (ValidFile(FullFileName
)):
193 # Validate file exist/format.
195 if not IsValidPath(FileName
, WorkSpace
):
201 FullFileName
= FullFileName
.replace('\\', '/')
202 Inputfile
= open(FullFileName
, "r")
204 FileLinesList
= Inputfile
.readlines()
205 except BaseException
:
206 Logger
.Error("InfParser", ToolError
.FILE_READ_FAILURE
, ST
.ERR_FILE_OPEN_FAILURE
, File
=FullFileName
)
209 except BaseException
:
210 Logger
.Error("InfParser",
211 ToolError
.FILE_READ_FAILURE
,
212 ST
.ERR_FILE_OPEN_FAILURE
,
215 FileLinesList
= ProcessLineExtender(FileLinesList
)
220 # Get all INF files from current workspace
223 def GetInfsFromWorkSpace(WorkSpace
):
225 for top
, dirs
, files
in os
.walk(WorkSpace
):
226 dirs
= dirs
# just for pylint
228 if File
.upper().endswith(".INF"):
229 InfFiles
.append(os
.path
.join(top
, File
))
234 # Get GUID and version from library instance file
237 def GetGuidVerFormLibInstance(Guid
, Version
, WorkSpace
, CurrentInfFileName
):
238 for InfFile
in GetInfsFromWorkSpace(WorkSpace
):
240 if InfFile
.strip().upper() == CurrentInfFileName
.strip().upper():
242 InfFile
= InfFile
.replace('\\', '/')
243 if InfFile
not in GlobalData
.gLIBINSTANCEDICT
:
244 InfFileObj
= open(InfFile
, "r")
245 GlobalData
.gLIBINSTANCEDICT
[InfFile
] = InfFileObj
247 InfFileObj
= GlobalData
.gLIBINSTANCEDICT
[InfFile
]
249 except BaseException
:
250 Logger
.Error("InfParser",
251 ToolError
.FILE_READ_FAILURE
,
252 ST
.ERR_FILE_OPEN_FAILURE
,
255 FileLinesList
= InfFileObj
.readlines()
256 FileLinesList
= ProcessLineExtender(FileLinesList
)
258 ReFindFileGuidPattern
= re
.compile("^\s*FILE_GUID\s*=.*$")
259 ReFindVerStringPattern
= re
.compile("^\s*VERSION_STRING\s*=.*$")
261 for Line
in FileLinesList
:
262 if ReFindFileGuidPattern
.match(Line
):
263 FileGuidString
= Line
264 if ReFindVerStringPattern
.match(Line
):
268 FileGuidString
= GetSplitValueList(FileGuidString
, '=', 1)[1]
270 VerString
= GetSplitValueList(VerString
, '=', 1)[1]
272 if FileGuidString
.strip().upper() == Guid
.upper() and \
273 VerString
.strip().upper() == Version
.upper():
276 except BaseException
:
277 Logger
.Error("InfParser", ToolError
.FILE_READ_FAILURE
, ST
.ERR_FILE_OPEN_FAILURE
, File
=InfFile
)