]>
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
24 from BuildToolError
import *
26 gHexVerPatt
= re
.compile('0x[a-f0-9]{4}[a-f0-9]{4}$',re
.IGNORECASE
)
27 gHumanReadableVerPatt
= re
.compile(r
'([1-9][0-9]*|0)\.[0-9]{1,2}$')
31 # Get a value list from a string with multiple values splited with SplitTag
32 # The default SplitTag is DataType.TAB_VALUE_SPLIT
33 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
35 # @param String: The input string to be splitted
36 # @param SplitTag: The split key, default is DataType.TAB_VALUE_SPLIT
37 # @param MaxSplit: The max number of split values, default is -1
39 # @retval list() A list for splitted string
41 def GetSplitValueList(String
, SplitTag
= DataType
.TAB_VALUE_SPLIT
, MaxSplit
= -1):
42 return map(lambda l
: l
.strip(), String
.split(SplitTag
, MaxSplit
))
46 # Find a key's all arches in dict, add the new arch to the list
47 # If not exist any arch, set the arch directly
49 # @param Dict: The input value for Dict
50 # @param Key: The input value for Key
51 # @param Arch: The Arch to be added or merged
53 def MergeArches(Dict
, Key
, Arch
):
54 if Key
in Dict
.keys():
55 Dict
[Key
].append(Arch
)
57 Dict
[Key
] = Arch
.split()
61 # Parse a string with format "DEFINE <VarName> = <PATH>"
62 # Generate a map Defines[VarName] = PATH
63 # Return False if invalid format
65 # @param String: String with DEFINE statement
66 # @param Arch: Supportted Arch
67 # @param Defines: DEFINE statement to be parsed
69 # @retval 0 DEFINE statement found, and valid
70 # @retval 1 DEFINE statement found, but not valid
71 # @retval -1 DEFINE statement not found
73 def GenDefines(String
, Arch
, Defines
):
74 if String
.find(DataType
.TAB_DEFINE
+ ' ') > -1:
75 List
= String
.replace(DataType
.TAB_DEFINE
+ ' ', '').split(DataType
.TAB_EQUAL_SPLIT
)
77 Defines
[(CleanString(List
[0]), Arch
)] = CleanString(List
[1])
86 # Parse a string with format "!include <Filename>"
87 # Return the file path
88 # Return False if invalid format or NOT FOUND
90 # @param String: String with INCLUDE statement
91 # @param IncludeFiles: INCLUDE statement to be parsed
92 # @param Arch: Supportted Arch
97 def GenInclude(String
, IncludeFiles
, Arch
):
98 if String
.upper().find(DataType
.TAB_INCLUDE
.upper() + ' ') > -1:
99 IncludeFile
= CleanString(String
[String
.upper().find(DataType
.TAB_INCLUDE
.upper() + ' ') + len(DataType
.TAB_INCLUDE
+ ' ') : ])
100 MergeArches(IncludeFiles
, IncludeFile
, Arch
)
105 ## GetLibraryClassesWithModuleType
107 # Get Library Class definition when no module type defined
109 # @param Lines: The content to be parsed
110 # @param Key: Reserved
111 # @param KeyValues: To store data after parsing
112 # @param CommentCharacter: Comment char, used to ignore comment content
114 # @retval True Get library classes successfully
116 def GetLibraryClassesWithModuleType(Lines
, Key
, KeyValues
, CommentCharacter
):
117 newKey
= SplitModuleType(Key
)
118 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
119 LineList
= Lines
.splitlines()
120 for Line
in LineList
:
121 Line
= CleanString(Line
, CommentCharacter
)
122 if Line
!= '' and Line
[0] != CommentCharacter
:
123 KeyValues
.append([CleanString(Line
, CommentCharacter
), newKey
[1]])
131 # @param Lines: The content to be parsed
132 # @param Key: Reserved
133 # @param KeyValues: To store data after parsing
134 # @param CommentCharacter: Comment char, used to ignore comment content
136 # @retval True Get Dynamic Pcds successfully
138 def GetDynamics(Lines
, Key
, KeyValues
, CommentCharacter
):
140 # Get SkuId Name List
142 SkuIdNameList
= SplitModuleType(Key
)
144 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
145 LineList
= Lines
.splitlines()
146 for Line
in LineList
:
147 Line
= CleanString(Line
, CommentCharacter
)
148 if Line
!= '' and Line
[0] != CommentCharacter
:
149 KeyValues
.append([CleanString(Line
, CommentCharacter
), SkuIdNameList
[1]])
155 # Split ModuleType out of section defien to get key
156 # [LibraryClass.Arch.ModuleType|ModuleType|ModuleType] -> [ 'LibraryClass.Arch', ['ModuleType', 'ModuleType', 'ModuleType'] ]
158 # @param Key: String to be parsed
160 # @retval ReturnValue A list for module types
162 def SplitModuleType(Key
):
163 KeyList
= Key
.split(DataType
.TAB_SPLIT
)
169 # Fill in for moduletype
173 KeyValue
= KeyList
[0]
175 KeyValue
= KeyValue
+ DataType
.TAB_SPLIT
+ KeyList
[1]
176 ReturnValue
.append(KeyValue
)
177 ReturnValue
.append(GetSplitValueList(KeyList
[2]))
181 ## Replace macro in strings list
183 # This method replace macros used in a given string list. The macros are
184 # given in a dictionary.
186 # @param StringList StringList to be processed
187 # @param MacroDefinitions The macro definitions in the form of dictionary
188 # @param SelfReplacement To decide whether replace un-defined macro to ''
190 # @retval NewList A new string list whose macros are replaced
192 def ReplaceMacros(StringList
, MacroDefinitions
={}, SelfReplacement
= False):
194 for String
in StringList
:
195 if type(String
) == type(''):
196 NewList
.append(ReplaceMacro(String
, MacroDefinitions
, SelfReplacement
))
198 NewList
.append(String
)
202 ## Replace macro in string
204 # This method replace macros used in given string. The macros are given in a
207 # @param String String to be processed
208 # @param MacroDefinitions The macro definitions in the form of dictionary
209 # @param SelfReplacement To decide whether replace un-defined macro to ''
211 # @retval string The string whose macros are replaced
213 def ReplaceMacro(String
, MacroDefinitions
={}, SelfReplacement
= False):
215 while MacroDefinitions
:
216 MacroUsed
= GlobalData
.gMacroPattern
.findall(String
)
217 # no macro found in String, stop replacing
218 if len(MacroUsed
) == 0:
221 for Macro
in MacroUsed
:
222 if Macro
not in MacroDefinitions
:
224 String
= String
.replace("$(%s)" % Macro
, '')
226 String
= String
.replace("$(%s)" % Macro
, MacroDefinitions
[Macro
])
227 # in case there's macro not defined
228 if String
== LastString
:
236 # Create a normal path
237 # And replace DFEINE in the path
239 # @param Path: The input value for Path to be converted
240 # @param Defines: A set for DEFINE statement
242 # @retval Path Formatted path
244 def NormPath(Path
, Defines
= {}):
245 IsRelativePath
= False
248 IsRelativePath
= True
250 # Replace with Define
253 Path
= ReplaceMacro(Path
, Defines
)
255 # To local path format
257 Path
= os
.path
.normpath(Path
)
259 if IsRelativePath
and Path
[0] != '.':
260 Path
= os
.path
.join('.', Path
)
266 # Remove comments in a string
269 # @param Line: The string to be cleaned
270 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
272 # @retval Path Formatted path
274 def CleanString(Line
, CommentCharacter
= DataType
.TAB_COMMENT_SPLIT
, AllowCppStyleComment
=False):
280 # Replace Edk's comment character
282 if AllowCppStyleComment
:
283 Line
= Line
.replace(DataType
.TAB_COMMENT_EDK_SPLIT
, CommentCharacter
)
285 # remove comments, but we should escape comment character in string
288 CommentInString
= False
289 for Index
in range(0, len(Line
)):
290 if Line
[Index
] == '"':
291 InString
= not InString
292 elif Line
[Index
] == CommentCharacter
and InString
:
293 CommentInString
= True
294 elif Line
[Index
] == CommentCharacter
and not InString
:
295 Line
= Line
[0: Index
]
299 Line
= Line
.replace('"', '')
300 ChIndex
= Line
.find('#')
302 if GlobalData
.gIsWindows
:
303 if ChIndex
== 0 or Line
[ChIndex
-1] != '^':
304 Line
= Line
[0:ChIndex
] + '^' + Line
[ChIndex
:]
305 ChIndex
= Line
.find('#', ChIndex
+ 2)
307 ChIndex
= Line
.find('#', ChIndex
+ 1)
309 if ChIndex
== 0 or Line
[ChIndex
-1] != '\\':
310 Line
= Line
[0:ChIndex
] + '\\' + Line
[ChIndex
:]
311 ChIndex
= Line
.find('#', ChIndex
+ 2)
313 ChIndex
= Line
.find('#', ChIndex
+ 1)
315 # remove whitespace again
323 # Split comments in a string
326 # @param Line: The string to be cleaned
327 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
329 # @retval Path Formatted path
331 def CleanString2(Line
, CommentCharacter
= DataType
.TAB_COMMENT_SPLIT
, AllowCppStyleComment
=False):
337 # Replace Edk's comment character
339 if AllowCppStyleComment
:
340 Line
= Line
.replace(DataType
.TAB_COMMENT_EDK_SPLIT
, CommentCharacter
)
342 # separate comments and statements
344 LineParts
= Line
.split(CommentCharacter
, 1);
346 # remove whitespace again
348 Line
= LineParts
[0].strip();
349 if len(LineParts
) > 1:
350 Comment
= LineParts
[1].strip()
351 # Remove prefixed and trailing comment characters
354 while Start
< End
and Comment
.startswith(CommentCharacter
, Start
, End
):
356 while End
>= 0 and Comment
.endswith(CommentCharacter
, Start
, End
):
358 Comment
= Comment
[Start
:End
]
359 Comment
= Comment
.strip()
365 ## GetMultipleValuesOfKeyFromLines
367 # Parse multiple strings to clean comment and spaces
368 # The result is saved to KeyValues
370 # @param Lines: The content to be parsed
371 # @param Key: Reserved
372 # @param KeyValues: To store data after parsing
373 # @param CommentCharacter: Comment char, used to ignore comment content
375 # @retval True Successfully executed
377 def GetMultipleValuesOfKeyFromLines(Lines
, Key
, KeyValues
, CommentCharacter
):
378 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
379 LineList
= Lines
.split('\n')
380 for Line
in LineList
:
381 Line
= CleanString(Line
, CommentCharacter
)
382 if Line
!= '' and Line
[0] != CommentCharacter
:
389 # Parse a DEFINE statement to get defined value
392 # @param String: The content to be parsed
393 # @param Key: The key of DEFINE statement
394 # @param CommentCharacter: Comment char, used to ignore comment content
396 # @retval string The defined value
398 def GetDefineValue(String
, Key
, CommentCharacter
):
399 String
= CleanString(String
)
400 return String
[String
.find(Key
+ ' ') + len(Key
+ ' ') : ]
404 # Get a Hex Version Value
406 # @param VerString: The version string to be parsed
409 # @retval: If VerString is incorrectly formatted, return "None" which will break the build.
410 # If VerString is correctly formatted, return a Hex value of the Version Number (0xmmmmnnnn)
411 # where mmmm is the major number and nnnn is the adjusted minor number.
413 def GetHexVerValue(VerString
):
414 VerString
= CleanString(VerString
)
416 if gHumanReadableVerPatt
.match(VerString
):
417 ValueList
= VerString
.split('.')
422 DeciValue
= (int(Major
) << 16) + int(Minor
);
423 return "0x%08x"%DeciValue
424 elif gHexVerPatt
.match(VerString
):
430 ## GetSingleValueOfKeyFromLines
432 # Parse multiple strings as below to get value of each definition line
435 # The result is saved to Dictionary
437 # @param Lines: The content to be parsed
438 # @param Dictionary: To store data after parsing
439 # @param CommentCharacter: Comment char, be used to ignore comment content
440 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
441 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
442 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
444 # @retval True Successfully executed
446 def GetSingleValueOfKeyFromLines(Lines
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
447 Lines
= Lines
.split('\n')
455 # Handle DEFINE and SPEC
457 if Line
.find(DataType
.TAB_INF_DEFINES_DEFINE
+ ' ') > -1:
458 if '' in DefineValues
:
459 DefineValues
.remove('')
460 DefineValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_DEFINE
, CommentCharacter
))
462 if Line
.find(DataType
.TAB_INF_DEFINES_SPEC
+ ' ') > -1:
464 SpecValues
.remove('')
465 SpecValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_SPEC
, CommentCharacter
))
471 LineList
= Line
.split(KeySplitCharacter
, 1)
472 if len(LineList
) >= 2:
473 Key
= LineList
[0].split()
474 if len(Key
) == 1 and Key
[0][0] != CommentCharacter
:
476 # Remove comments and white spaces
478 LineList
[1] = CleanString(LineList
[1], CommentCharacter
)
480 Value
= map(string
.strip
, LineList
[1].split(ValueSplitCharacter
))
482 Value
= CleanString(LineList
[1], CommentCharacter
).splitlines()
484 if Key
[0] in Dictionary
:
485 if Key
[0] not in Keys
:
486 Dictionary
[Key
[0]] = Value
489 Dictionary
[Key
[0]].extend(Value
)
491 Dictionary
[DataType
.TAB_INF_DEFINES_MACRO
][Key
[0]] = Value
[0]
493 if DefineValues
== []:
497 Dictionary
[DataType
.TAB_INF_DEFINES_DEFINE
] = DefineValues
498 Dictionary
[DataType
.TAB_INF_DEFINES_SPEC
] = SpecValues
502 ## The content to be parsed
504 # Do pre-check for a file before it is parsed
508 # @param FileName: Used for error report
509 # @param FileContent: File content to be parsed
510 # @param SupSectionTag: Used for error report
512 def PreCheck(FileName
, FileContent
, SupSectionTag
):
516 for Line
in FileContent
.splitlines():
521 Line
= CleanString(Line
)
524 # Remove commented line
526 if Line
.find(DataType
.TAB_COMMA_SPLIT
) == 0:
531 if Line
.find('$') > -1:
532 if Line
.find('$(') < 0 or Line
.find(')') < 0:
533 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
= EdkLogger
.IsRaiseError
)
538 if Line
.find('[') > -1 or Line
.find(']') > -1:
540 # Only get one '[' or one ']'
542 if not (Line
.find('[') > -1 and Line
.find(']') > -1):
543 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
= EdkLogger
.IsRaiseError
)
546 # Regenerate FileContent
548 NewFileContent
= NewFileContent
+ Line
+ '\r\n'
551 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
= EdkLogger
.IsRaiseError
)
553 return NewFileContent
557 # Check if the Filename is including ExtName
558 # Return True if it exists
559 # Raise a error message if it not exists
561 # @param CheckFilename: Name of the file to be checked
562 # @param ExtName: Ext name of the file to be checked
563 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
564 # @param SectionName: Used for error report
565 # @param Line: The line in container file which defines the file to be checked
567 # @retval True The file type is correct
569 def CheckFileType(CheckFilename
, ExtName
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
570 if CheckFilename
!= '' and CheckFilename
!= None:
571 (Root
, Ext
) = os
.path
.splitext(CheckFilename
)
572 if Ext
.upper() != ExtName
.upper():
573 ContainerFile
= open(ContainerFilename
, 'r').read()
575 LineNo
= GetLineNo(ContainerFile
, Line
)
576 ErrorMsg
= "Invalid %s. '%s' is found, but '%s' file is needed" % (SectionName
, CheckFilename
, ExtName
)
577 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, Line
=LineNo
,
578 File
=ContainerFilename
, RaiseError
= EdkLogger
.IsRaiseError
)
584 # Check if the file exists
585 # Return True if it exists
586 # Raise a error message if it not exists
588 # @param CheckFilename: Name of the file to be checked
589 # @param WorkspaceDir: Current workspace dir
590 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
591 # @param SectionName: Used for error report
592 # @param Line: The line in container file which defines the file to be checked
594 # @retval The file full path if the file exists
596 def CheckFileExist(WorkspaceDir
, CheckFilename
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
598 if CheckFilename
!= '' and CheckFilename
!= None:
599 CheckFile
= WorkspaceFile(WorkspaceDir
, CheckFilename
)
600 if not os
.path
.isfile(CheckFile
):
601 ContainerFile
= open(ContainerFilename
, 'r').read()
603 LineNo
= GetLineNo(ContainerFile
, Line
)
604 ErrorMsg
= "Can't find file '%s' defined in section '%s'" % (CheckFile
, SectionName
)
605 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
,
606 File
=ContainerFilename
, Line
= LineNo
, RaiseError
= EdkLogger
.IsRaiseError
)
612 # Find the index of a line in a file
614 # @param FileContent: Search scope
615 # @param Line: Search key
617 # @retval int Index of the line
618 # @retval -1 The line is not found
620 def GetLineNo(FileContent
, Line
, IsIgnoreComment
= True):
621 LineList
= FileContent
.splitlines()
622 for Index
in range(len(LineList
)):
623 if LineList
[Index
].find(Line
) > -1:
625 # Ignore statement in comment
628 if LineList
[Index
].strip()[0] == DataType
.TAB_COMMENT_SPLIT
:
636 # Raise a parser error
638 # @param Line: String which has error
639 # @param Section: Used for error report
640 # @param File: File which has the string
641 # @param Format: Correct format
643 def RaiseParserError(Line
, Section
, File
, Format
= '', LineNo
= -1):
645 LineNo
= GetLineNo(open(os
.path
.normpath(File
), 'r').read(), Line
)
646 ErrorMsg
= "Invalid statement '%s' is found in section '%s'" % (Line
, Section
)
648 Format
= "Correct format is " + Format
649 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, File
=File
, Line
=LineNo
, ExtraData
=Format
, RaiseError
= EdkLogger
.IsRaiseError
)
653 # Return a full path with workspace dir
655 # @param WorkspaceDir: Workspace dir
656 # @param Filename: Relative file name
658 # @retval string A full path
660 def WorkspaceFile(WorkspaceDir
, Filename
):
661 return os
.path
.join(NormPath(WorkspaceDir
), NormPath(Filename
))
665 # Revmove '"' which startswith and endswith string
667 # @param String: The string need to be splited
669 # @retval String: The string after removed '""'
671 def SplitString(String
):
672 if String
.startswith('\"'):
674 if String
.endswith('\"'):
679 ## Convert To Sql String
681 # 1. Replace "'" with "''" in each item of StringList
683 # @param StringList: A list for strings to be converted
685 def ConvertToSqlString(StringList
):
686 return map(lambda s
: s
.replace("'", "''") , StringList
)
688 ## Convert To Sql String
690 # 1. Replace "'" with "''" in the String
692 # @param String: A String to be converted
694 def ConvertToSqlString2(String
):
695 return String
.replace("'", "''")
698 # Remove comment block
700 def RemoveBlockComment(Lines
):
701 IsFindBlockComment
= False
702 IsFindBlockCode
= False
709 # Remove comment block
711 if Line
.find(DataType
.TAB_COMMENT_EDK_START
) > -1:
712 ReservedLine
= GetSplitValueList(Line
, DataType
.TAB_COMMENT_EDK_START
, 1)[0]
713 IsFindBlockComment
= True
714 if Line
.find(DataType
.TAB_COMMENT_EDK_END
) > -1:
715 Line
= ReservedLine
+ GetSplitValueList(Line
, DataType
.TAB_COMMENT_EDK_END
, 1)[1]
717 IsFindBlockComment
= False
718 if IsFindBlockComment
:
722 NewLines
.append(Line
)
726 # Get String of a List
728 def GetStringOfList(List
, Split
= ' '):
729 if type(List
) != type([]):
733 Str
= Str
+ Item
+ Split
738 # Get HelpTextList from HelpTextClassList
740 def GetHelpTextList(HelpTextClassList
):
742 if HelpTextClassList
:
743 for HelpText
in HelpTextClassList
:
744 if HelpText
.String
.endswith('\n'):
745 HelpText
.String
= HelpText
.String
[0: len(HelpText
.String
) - len('\n')]
746 List
.extend(HelpText
.String
.split('\n'))
750 def StringToArray(String
):
751 if isinstance(String
, unicode):
753 return "{0x00, 0x00}"
754 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C
) for C
in String
])
755 elif String
.startswith('L"'):
756 if String
== "L\"\"":
757 return "{0x00, 0x00}"
759 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C
) for C
in String
[2:-1]])
760 elif String
.startswith('"'):
764 return "{%s, 0x00}" % ", ".join(["0x%02x" % ord(C
) for C
in String
[1:-1]])
766 return '{%s, 0}' % ', '.join(String
.split())
768 def StringArrayLength(String
):
769 if isinstance(String
, unicode):
770 return (len(String
) + 1) * 2 + 1;
771 elif String
.startswith('L"'):
772 return (len(String
) - 3 + 1) * 2
773 elif String
.startswith('"'):
774 return (len(String
) - 2 + 1)
776 return len(String
.split()) + 1
778 def RemoveDupOption(OptionString
, Which
="/I", Against
=None):
779 OptionList
= OptionString
.split()
783 for Index
in range(len(OptionList
)):
784 Opt
= OptionList
[Index
]
785 if not Opt
.startswith(Which
):
787 if len(Opt
) > len(Which
):
788 Val
= Opt
[len(Which
):]
792 OptionList
[Index
] = ""
794 ValueList
.append(Val
)
795 return " ".join(OptionList
)
799 # This acts like the main() function for the script, unless it is 'import'ed into another
802 if __name__
== '__main__':