2 # This file is used to provide method for process AsBuilt INF file. It will consumed by InfParser
4 # Copyright (c) 2011, 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
23 import Logger
.Log
as Logger
24 from Logger
import StringTable
as ST
25 from Logger
import ToolError
27 from Library
.String
import GetSplitValueList
28 from Library
.Misc
import GetHelpStringByRemoveHashKey
29 from Library
.Misc
import ValidFile
30 from Library
.Misc
import ProcessLineExtender
31 from Library
.ParserValidate
import IsValidPath
32 from Library
.Parsing
import MacroParser
33 from Parser
.InfParserMisc
import InfExpandMacro
35 from Library
import DataType
as DT
39 # Get the information from Library Instance INF file.
41 # @param string. A string start with # and followed by INF file path
42 # @param WorkSpace. The WorkSpace directory used to combined with INF file path.
44 # @return GUID, Version
45 def GetLibInstanceInfo(String
, WorkSpace
, LineNo
):
50 OrignalString
= String
51 String
= String
.strip()
55 # Remove "#" characters at the beginning
57 String
= GetHelpStringByRemoveHashKey(String
)
58 String
= String
.strip()
60 FileLinesList
= GetFileLineContent(String
, WorkSpace
, LineNo
, OrignalString
)
63 ReFindFileGuidPattern
= re
.compile("^\s*FILE_GUID\s*=.*$")
64 ReFindVerStringPattern
= re
.compile("^\s*VERSION_STRING\s*=.*$")
66 FileLinesList
= ProcessLineExtender(FileLinesList
)
68 for Line
in FileLinesList
:
69 if ReFindFileGuidPattern
.match(Line
):
71 if ReFindVerStringPattern
.match(Line
):
75 FileGuidString
= GetSplitValueList(FileGuidString
, '=', 1)[1]
77 VerString
= GetSplitValueList(VerString
, '=', 1)[1]
79 return FileGuidString
, VerString
83 # Get the package information from INF file.
85 # @param string. A string start with # and followed by INF file path
86 # @param WorkSpace. The WorkSpace directory used to combined with INF file path.
88 # @return GUID, Version
89 def GetPackageListInfo(FileNameString
, WorkSpace
, LineNo
):
91 DefineSectionMacros
= {}
92 PackageSectionMacros
= {}
94 FileLinesList
= GetFileLineContent(FileNameString
, WorkSpace
, LineNo
, '')
96 RePackageHeader
= re
.compile('^\s*\[Packages.*\].*$')
97 ReDefineHeader
= re
.compile('^\s*\[Defines].*$')
99 PackageHederFlag
= False
100 DefineHeaderFlag
= False
102 for Line
in FileLinesList
:
106 if Line
.startswith('['):
107 PackageHederFlag
= False
108 DefineHeaderFlag
= False
110 if Line
.startswith("#"):
117 # Found [Packages] section
119 if RePackageHeader
.match(Line
):
120 PackageHederFlag
= True
124 # Found [Define] section
126 if ReDefineHeader
.match(Line
):
127 DefineHeaderFlag
= True
134 Name
, Value
= MacroParser((Line
, LineNo
),
136 DT
.MODEL_META_DATA_HEADER
,
140 DefineSectionMacros
[Name
] = Value
148 Name
, Value
= MacroParser((Line
, LineNo
),
150 DT
.MODEL_META_DATA_PACKAGE
,
153 PackageSectionMacros
[Name
] = Value
157 # Replace with Local section Macro and [Defines] section Macro.
159 Line
= InfExpandMacro(Line
, (FileNameString
, Line
, LineNo
), DefineSectionMacros
, PackageSectionMacros
, True)
161 Line
= GetSplitValueList(Line
, "#", 1)[0]
162 Line
= GetSplitValueList(Line
, "|", 1)[0]
163 PackageInfoList
.append(Line
)
165 return PackageInfoList
167 def GetFileLineContent(FileName
, WorkSpace
, LineNo
, OriginalString
):
173 # Validate file name exist.
175 FullFileName
= os
.path
.normpath(os
.path
.realpath(os
.path
.join(WorkSpace
, FileName
)))
176 if not (ValidFile(FullFileName
)):
177 Logger
.Error("InfParser",
178 ToolError
.FORMAT_INVALID
,
179 ST
.ERR_FILELIST_EXIST
%(FileName),
180 File
=GlobalData
.gINF_MODULE_NAME
,
182 ExtraData
=OriginalString
)
185 # Validate file exist/format.
187 if IsValidPath(FileName
, WorkSpace
):
188 IsValidFileFlag
= True
190 Logger
.Error("InfParser",
191 ToolError
.FORMAT_INVALID
,
192 ST
.ERR_INF_PARSER_FILE_NOT_EXIST_OR_NAME_INVALID
%(FileName),
193 File
=GlobalData
.gINF_MODULE_NAME
,
195 ExtraData
=OriginalString
)
202 FullFileName
= FullFileName
.replace('\\', '/')
203 Inputfile
= open(FullFileName
, "rb", 0)
205 FileLinesList
= Inputfile
.readlines()
206 except BaseException
:
207 Logger
.Error("InfParser", ToolError
.FILE_READ_FAILURE
, ST
.ERR_FILE_OPEN_FAILURE
, File
=FullFileName
)
210 except BaseException
:
211 Logger
.Error("InfParser",
212 ToolError
.FILE_READ_FAILURE
,
213 ST
.ERR_FILE_OPEN_FAILURE
,
216 FileLinesList
= ProcessLineExtender(FileLinesList
)