]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/String.py
2 # This file is used to define common string related functions used in parsing process
4 # Copyright (c) 2007 - 2008, 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
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.
21 import EdkLogger
as EdkLogger
23 from GlobalData
import *
24 from BuildToolError
import *
28 # Get a value list from a string with multiple values splited with SplitTag
29 # The default SplitTag is DataType.TAB_VALUE_SPLIT
30 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
32 # @param String: The input string to be splitted
33 # @param SplitTag: The split key, default is DataType.TAB_VALUE_SPLIT
34 # @param MaxSplit: The max number of split values, default is -1
36 # @retval list() A list for splitted string
38 def GetSplitValueList(String
, SplitTag
= DataType
.TAB_VALUE_SPLIT
, MaxSplit
= -1):
39 return map(lambda l
: l
.strip(), String
.split(SplitTag
, MaxSplit
))
43 # Find a key's all arches in dict, add the new arch to the list
44 # If not exist any arch, set the arch directly
46 # @param Dict: The input value for Dict
47 # @param Key: The input value for Key
48 # @param Arch: The Arch to be added or merged
50 def MergeArches(Dict
, Key
, Arch
):
51 if Key
in Dict
.keys():
52 Dict
[Key
].append(Arch
)
54 Dict
[Key
] = Arch
.split()
58 # Parse a string with format "DEFINE <VarName> = <PATH>"
59 # Generate a map Defines[VarName] = PATH
60 # Return False if invalid format
62 # @param String: String with DEFINE statement
63 # @param Arch: Supportted Arch
64 # @param Defines: DEFINE statement to be parsed
66 # @retval 0 DEFINE statement found, and valid
67 # @retval 1 DEFINE statement found, but not valid
68 # @retval -1 DEFINE statement not found
70 def GenDefines(String
, Arch
, Defines
):
71 if String
.find(DataType
.TAB_DEFINE
+ ' ') > -1:
72 List
= String
.replace(DataType
.TAB_DEFINE
+ ' ', '').split(DataType
.TAB_EQUAL_SPLIT
)
74 Defines
[(CleanString(List
[0]), Arch
)] = CleanString(List
[1])
83 # Parse a string with format "!include <Filename>"
84 # Return the file path
85 # Return False if invalid format or NOT FOUND
87 # @param String: String with INCLUDE statement
88 # @param IncludeFiles: INCLUDE statement to be parsed
89 # @param Arch: Supportted Arch
94 def GenInclude(String
, IncludeFiles
, Arch
):
95 if String
.upper().find(DataType
.TAB_INCLUDE
.upper() + ' ') > -1:
96 IncludeFile
= CleanString(String
[String
.upper().find(DataType
.TAB_INCLUDE
.upper() + ' ') + len(DataType
.TAB_INCLUDE
+ ' ') : ])
97 MergeArches(IncludeFiles
, IncludeFile
, Arch
)
102 ## GetLibraryClassesWithModuleType
104 # Get Library Class definition when no module type defined
106 # @param Lines: The content to be parsed
107 # @param Key: Reserved
108 # @param KeyValues: To store data after parsing
109 # @param CommentCharacter: Comment char, used to ignore comment content
111 # @retval True Get library classes successfully
113 def GetLibraryClassesWithModuleType(Lines
, Key
, KeyValues
, CommentCharacter
):
114 newKey
= SplitModuleType(Key
)
115 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
116 LineList
= Lines
.splitlines()
117 for Line
in LineList
:
118 Line
= CleanString(Line
, CommentCharacter
)
119 if Line
!= '' and Line
[0] != CommentCharacter
:
120 KeyValues
.append([CleanString(Line
, CommentCharacter
), newKey
[1]])
128 # @param Lines: The content to be parsed
129 # @param Key: Reserved
130 # @param KeyValues: To store data after parsing
131 # @param CommentCharacter: Comment char, used to ignore comment content
133 # @retval True Get Dynamic Pcds successfully
135 def GetDynamics(Lines
, Key
, KeyValues
, CommentCharacter
):
137 # Get SkuId Name List
139 SkuIdNameList
= SplitModuleType(Key
)
141 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
142 LineList
= Lines
.splitlines()
143 for Line
in LineList
:
144 Line
= CleanString(Line
, CommentCharacter
)
145 if Line
!= '' and Line
[0] != CommentCharacter
:
146 KeyValues
.append([CleanString(Line
, CommentCharacter
), SkuIdNameList
[1]])
152 # Split ModuleType out of section defien to get key
153 # [LibraryClass.Arch.ModuleType|ModuleType|ModuleType] -> [ 'LibraryClass.Arch', ['ModuleType', 'ModuleType', 'ModuleType'] ]
155 # @param Key: String to be parsed
157 # @retval ReturnValue A list for module types
159 def SplitModuleType(Key
):
160 KeyList
= Key
.split(DataType
.TAB_SPLIT
)
166 # Fill in for moduletype
170 KeyValue
= KeyList
[0]
172 KeyValue
= KeyValue
+ DataType
.TAB_SPLIT
+ KeyList
[1]
173 ReturnValue
.append(KeyValue
)
174 ReturnValue
.append(GetSplitValueList(KeyList
[2]))
178 ## Replace macro in strings list
180 # This method replace macros used in a given string list. The macros are
181 # given in a dictionary.
183 # @param StringList StringList to be processed
184 # @param MacroDefinitions The macro definitions in the form of dictionary
185 # @param SelfReplacement To decide whether replace un-defined macro to ''
187 # @retval NewList A new string list whose macros are replaced
189 def ReplaceMacros(StringList
, MacroDefinitions
={}, SelfReplacement
= False):
191 for String
in StringList
:
192 if type(String
) == type(''):
193 NewList
.append(ReplaceMacro(String
, MacroDefinitions
, SelfReplacement
))
195 NewList
.append(String
)
199 ## Replace macro in string
201 # This method replace macros used in given string. The macros are given in a
204 # @param String String to be processed
205 # @param MacroDefinitions The macro definitions in the form of dictionary
206 # @param SelfReplacement To decide whether replace un-defined macro to ''
208 # @retval string The string whose macros are replaced
210 def ReplaceMacro(String
, MacroDefinitions
={}, SelfReplacement
= False):
212 while MacroDefinitions
:
213 MacroUsed
= gMacroPattern
.findall(String
)
214 # no macro found in String, stop replacing
215 if len(MacroUsed
) == 0:
218 for Macro
in MacroUsed
:
219 if Macro
not in MacroDefinitions
:
221 String
= String
.replace("$(%s)" % Macro
, '')
223 String
= String
.replace("$(%s)" % Macro
, MacroDefinitions
[Macro
])
224 # in case there's macro not defined
225 if String
== LastString
:
233 # Create a normal path
234 # And replace DFEINE in the path
236 # @param Path: The input value for Path to be converted
237 # @param Defines: A set for DEFINE statement
239 # @retval Path Formatted path
241 def NormPath(Path
, Defines
= {}):
242 IsRelativePath
= False
245 IsRelativePath
= True
247 # Replace with Define
250 Path
= ReplaceMacro(Path
, Defines
)
252 # To local path format
254 Path
= os
.path
.normpath(Path
)
256 if IsRelativePath
and Path
[0] != '.':
257 Path
= os
.path
.join('.', Path
)
263 # Remove comments in a string
266 # @param Line: The string to be cleaned
267 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
269 # @retval Path Formatted path
271 def CleanString(Line
, CommentCharacter
= DataType
.TAB_COMMENT_SPLIT
, AllowCppStyleComment
=False):
277 # Replace R8's comment character
279 if AllowCppStyleComment
:
280 Line
= Line
.replace(DataType
.TAB_COMMENT_R8_SPLIT
, CommentCharacter
)
282 # remove comments, but we should escape comment character in string
285 for Index
in range(0, len(Line
)):
286 if Line
[Index
] == '"':
287 InString
= not InString
288 elif Line
[Index
] == CommentCharacter
and not InString
:
289 Line
= Line
[0: Index
]
293 # remove whitespace again
299 ## GetMultipleValuesOfKeyFromLines
301 # Parse multiple strings to clean comment and spaces
302 # The result is saved to KeyValues
304 # @param Lines: The content to be parsed
305 # @param Key: Reserved
306 # @param KeyValues: To store data after parsing
307 # @param CommentCharacter: Comment char, used to ignore comment content
309 # @retval True Successfully executed
311 def GetMultipleValuesOfKeyFromLines(Lines
, Key
, KeyValues
, CommentCharacter
):
312 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
313 LineList
= Lines
.split('\n')
314 for Line
in LineList
:
315 Line
= CleanString(Line
, CommentCharacter
)
316 if Line
!= '' and Line
[0] != CommentCharacter
:
323 # Parse a DEFINE statement to get defined value
326 # @param String: The content to be parsed
327 # @param Key: The key of DEFINE statement
328 # @param CommentCharacter: Comment char, used to ignore comment content
330 # @retval string The defined value
332 def GetDefineValue(String
, Key
, CommentCharacter
):
333 String
= CleanString(String
)
334 return String
[String
.find(Key
+ ' ') + len(Key
+ ' ') : ]
336 ## GetSingleValueOfKeyFromLines
338 # Parse multiple strings as below to get value of each definition line
341 # The result is saved to Dictionary
343 # @param Lines: The content to be parsed
344 # @param Dictionary: To store data after parsing
345 # @param CommentCharacter: Comment char, be used to ignore comment content
346 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
347 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
348 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
350 # @retval True Successfully executed
352 def GetSingleValueOfKeyFromLines(Lines
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
353 Lines
= Lines
.split('\n')
361 # Handle DEFINE and SPEC
363 if Line
.find(DataType
.TAB_INF_DEFINES_DEFINE
+ ' ') > -1:
364 if '' in DefineValues
:
365 DefineValues
.remove('')
366 DefineValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_DEFINE
, CommentCharacter
))
368 if Line
.find(DataType
.TAB_INF_DEFINES_SPEC
+ ' ') > -1:
370 SpecValues
.remove('')
371 SpecValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_SPEC
, CommentCharacter
))
377 LineList
= Line
.split(KeySplitCharacter
, 1)
378 if len(LineList
) >= 2:
379 Key
= LineList
[0].split()
380 if len(Key
) == 1 and Key
[0][0] != CommentCharacter
:
382 # Remove comments and white spaces
384 LineList
[1] = CleanString(LineList
[1], CommentCharacter
)
386 Value
= map(string
.strip
, LineList
[1].split(ValueSplitCharacter
))
388 Value
= CleanString(LineList
[1], CommentCharacter
).splitlines()
390 if Key
[0] in Dictionary
:
391 if Key
[0] not in Keys
:
392 Dictionary
[Key
[0]] = Value
395 Dictionary
[Key
[0]].extend(Value
)
397 Dictionary
[DataType
.TAB_INF_DEFINES_MACRO
][Key
[0]] = Value
[0]
399 if DefineValues
== []:
403 Dictionary
[DataType
.TAB_INF_DEFINES_DEFINE
] = DefineValues
404 Dictionary
[DataType
.TAB_INF_DEFINES_SPEC
] = SpecValues
408 ## The content to be parsed
410 # Do pre-check for a file before it is parsed
414 # @param FileName: Used for error report
415 # @param FileContent: File content to be parsed
416 # @param SupSectionTag: Used for error report
418 def PreCheck(FileName
, FileContent
, SupSectionTag
):
422 for Line
in FileContent
.splitlines():
427 Line
= CleanString(Line
)
430 # Remove commented line
432 if Line
.find(DataType
.TAB_COMMA_SPLIT
) == 0:
437 if Line
.find('$') > -1:
438 if Line
.find('$(') < 0 or Line
.find(')') < 0:
439 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
= EdkLogger
.IsRaiseError
)
444 if Line
.find('[') > -1 or Line
.find(']') > -1:
446 # Only get one '[' or one ']'
448 if not (Line
.find('[') > -1 and Line
.find(']') > -1):
449 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
= EdkLogger
.IsRaiseError
)
452 # Regenerate FileContent
454 NewFileContent
= NewFileContent
+ Line
+ '\r\n'
457 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
= EdkLogger
.IsRaiseError
)
459 return NewFileContent
463 # Check if the Filename is including ExtName
464 # Return True if it exists
465 # Raise a error message if it not exists
467 # @param CheckFilename: Name of the file to be checked
468 # @param ExtName: Ext name of the file to be checked
469 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
470 # @param SectionName: Used for error report
471 # @param Line: The line in container file which defines the file to be checked
473 # @retval True The file type is correct
475 def CheckFileType(CheckFilename
, ExtName
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
476 if CheckFilename
!= '' and CheckFilename
!= None:
477 (Root
, Ext
) = os
.path
.splitext(CheckFilename
)
478 if Ext
.upper() != ExtName
.upper():
479 ContainerFile
= open(ContainerFilename
, 'r').read()
481 LineNo
= GetLineNo(ContainerFile
, Line
)
482 ErrorMsg
= "Invalid %s. '%s' is found, but '%s' file is needed" % (SectionName
, CheckFilename
, ExtName
)
483 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, Line
=LineNo
,
484 File
=ContainerFilename
, RaiseError
= EdkLogger
.IsRaiseError
)
490 # Check if the file exists
491 # Return True if it exists
492 # Raise a error message if it not exists
494 # @param CheckFilename: Name of the file to be checked
495 # @param WorkspaceDir: Current workspace dir
496 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
497 # @param SectionName: Used for error report
498 # @param Line: The line in container file which defines the file to be checked
500 # @retval The file full path if the file exists
502 def CheckFileExist(WorkspaceDir
, CheckFilename
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
504 if CheckFilename
!= '' and CheckFilename
!= None:
505 CheckFile
= WorkspaceFile(WorkspaceDir
, CheckFilename
)
506 if not os
.path
.isfile(CheckFile
):
507 ContainerFile
= open(ContainerFilename
, 'r').read()
509 LineNo
= GetLineNo(ContainerFile
, Line
)
510 ErrorMsg
= "Can't find file '%s' defined in section '%s'" % (CheckFile
, SectionName
)
511 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
,
512 File
=ContainerFilename
, Line
= LineNo
, RaiseError
= EdkLogger
.IsRaiseError
)
518 # Find the index of a line in a file
520 # @param FileContent: Search scope
521 # @param Line: Search key
523 # @retval int Index of the line
524 # @retval -1 The line is not found
526 def GetLineNo(FileContent
, Line
, IsIgnoreComment
= True):
527 LineList
= FileContent
.splitlines()
528 for Index
in range(len(LineList
)):
529 if LineList
[Index
].find(Line
) > -1:
531 # Ignore statement in comment
534 if LineList
[Index
].strip()[0] == DataType
.TAB_COMMENT_SPLIT
:
542 # Raise a parser error
544 # @param Line: String which has error
545 # @param Section: Used for error report
546 # @param File: File which has the string
547 # @param Format: Correct format
549 def RaiseParserError(Line
, Section
, File
, Format
= '', LineNo
= -1):
551 LineNo
= GetLineNo(open(os
.path
.normpath(File
), 'r').read(), Line
)
552 ErrorMsg
= "Invalid statement '%s' is found in section '%s'" % (Line
, Section
)
554 Format
= "Correct format is " + Format
555 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, File
=File
, Line
=LineNo
, ExtraData
=Format
, RaiseError
= EdkLogger
.IsRaiseError
)
559 # Return a full path with workspace dir
561 # @param WorkspaceDir: Workspace dir
562 # @param Filename: Relative file name
564 # @retval string A full path
566 def WorkspaceFile(WorkspaceDir
, Filename
):
567 return os
.path
.join(NormPath(WorkspaceDir
), NormPath(Filename
))
571 # Revmove '"' which startswith and endswith string
573 # @param String: The string need to be splited
575 # @retval String: The string after removed '""'
577 def SplitString(String
):
578 if String
.startswith('\"'):
580 if String
.endswith('\"'):
585 ## Convert To Sql String
587 # 1. Replace "'" with "''" in each item of StringList
589 # @param StringList: A list for strings to be converted
591 def ConvertToSqlString(StringList
):
592 return map(lambda s
: s
.replace("'", "''") , StringList
)
594 ## Convert To Sql String
596 # 1. Replace "'" with "''" in the String
598 # @param String: A String to be converted
600 def ConvertToSqlString2(String
):
601 return String
.replace("'", "''")
604 # Remove comment block
606 def RemoveBlockComment(Lines
):
607 IsFindBlockComment
= False
608 IsFindBlockCode
= False
615 # Remove comment block
617 if Line
.find(DataType
.TAB_COMMENT_R8_START
) > -1:
618 ReservedLine
= GetSplitValueList(Line
, DataType
.TAB_COMMENT_R8_START
, 1)[0]
619 IsFindBlockComment
= True
620 if Line
.find(DataType
.TAB_COMMENT_R8_END
) > -1:
621 Line
= ReservedLine
+ GetSplitValueList(Line
, DataType
.TAB_COMMENT_R8_END
, 1)[1]
623 IsFindBlockComment
= False
624 if IsFindBlockComment
:
628 NewLines
.append(Line
)
632 # Get String of a List
634 def GetStringOfList(List
, Split
= ' '):
635 if type(List
) != type([]):
639 Str
= Str
+ Item
+ Split
644 # Get HelpTextList from HelpTextClassList
646 def GetHelpTextList(HelpTextClassList
):
648 if HelpTextClassList
:
649 for HelpText
in HelpTextClassList
:
650 if HelpText
.String
.endswith('\n'):
651 HelpText
.String
= HelpText
.String
[0: len(HelpText
.String
) - len('\n')]
652 List
.extend(HelpText
.String
.split('\n'))
656 def StringToArray(String
):
657 if isinstance(String
, unicode):
659 return "{0x00, 0x00}"
660 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C
) for C
in String
])
661 elif String
.startswith('L"'):
662 if String
== "L\"\"":
663 return "{0x00, 0x00}"
665 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C
) for C
in String
[2:-1]])
666 elif String
.startswith('"'):
670 return "{%s, 0x00}" % ", ".join(["0x%02x" % ord(C
) for C
in String
[1:-1]])
672 return '{%s, 0}' % ', '.join(String
.split())
674 def StringArrayLength(String
):
675 if isinstance(String
, unicode):
676 return (len(String
) + 1) * 2 + 1;
677 elif String
.startswith('L"'):
678 return (len(String
) - 3 + 1) * 2
679 elif String
.startswith('"'):
680 return (len(String
) - 2 + 1)
682 return len(String
.split()) + 1
684 def RemoveDupOption(OptionString
, Which
="/I", Against
=None):
685 OptionList
= OptionString
.split()
689 for Index
in range(len(OptionList
)):
690 Opt
= OptionList
[Index
]
691 if not Opt
.startswith(Which
):
693 if len(Opt
) > len(Which
):
694 Val
= Opt
[len(Which
):]
698 OptionList
[Index
] = ""
700 ValueList
.append(Val
)
701 return " ".join(OptionList
)
705 # This acts like the main() function for the script, unless it is 'import'ed into another
708 if __name__
== '__main__':