]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/String.py
195fa5c6cacc0c9d82f483ace03316d04ae67ac2
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 *
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
= 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 R8's comment character
282 if AllowCppStyleComment
:
283 Line
= Line
.replace(DataType
.TAB_COMMENT_R8_SPLIT
, CommentCharacter
)
285 # remove comments, but we should escape comment character in string
288 for Index
in range(0, len(Line
)):
289 if Line
[Index
] == '"':
290 InString
= not InString
291 elif Line
[Index
] == CommentCharacter
and not InString
:
292 Line
= Line
[0: Index
]
296 # remove whitespace again
304 # Split comments in a string
307 # @param Line: The string to be cleaned
308 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
310 # @retval Path Formatted path
312 def CleanString2(Line
, CommentCharacter
= DataType
.TAB_COMMENT_SPLIT
, AllowCppStyleComment
=False):
318 # Replace R8's comment character
320 if AllowCppStyleComment
:
321 Line
= Line
.replace(DataType
.TAB_COMMENT_R8_SPLIT
, CommentCharacter
)
323 # separate comments and statements
325 LineParts
= Line
.split(CommentCharacter
, 1);
327 # remove whitespace again
329 Line
= LineParts
[0].strip();
330 if len(LineParts
) > 1:
331 Comment
= LineParts
[1].strip()
332 # Remove prefixed and trailing comment characters
335 while Start
< End
and Comment
.startswith(CommentCharacter
, Start
, End
):
337 while End
>= 0 and Comment
.endswith(CommentCharacter
, Start
, End
):
339 Comment
= Comment
[Start
:End
]
340 Comment
= Comment
.strip()
346 ## GetMultipleValuesOfKeyFromLines
348 # Parse multiple strings to clean comment and spaces
349 # The result is saved to KeyValues
351 # @param Lines: The content to be parsed
352 # @param Key: Reserved
353 # @param KeyValues: To store data after parsing
354 # @param CommentCharacter: Comment char, used to ignore comment content
356 # @retval True Successfully executed
358 def GetMultipleValuesOfKeyFromLines(Lines
, Key
, KeyValues
, CommentCharacter
):
359 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
360 LineList
= Lines
.split('\n')
361 for Line
in LineList
:
362 Line
= CleanString(Line
, CommentCharacter
)
363 if Line
!= '' and Line
[0] != CommentCharacter
:
370 # Parse a DEFINE statement to get defined value
373 # @param String: The content to be parsed
374 # @param Key: The key of DEFINE statement
375 # @param CommentCharacter: Comment char, used to ignore comment content
377 # @retval string The defined value
379 def GetDefineValue(String
, Key
, CommentCharacter
):
380 String
= CleanString(String
)
381 return String
[String
.find(Key
+ ' ') + len(Key
+ ' ') : ]
385 # Get a Hex Version Value
387 # @param VerString: The version string to be parsed
390 # @retval: If VerString is incorrectly formatted, return "None" which will break the build.
391 # If VerString is correctly formatted, return a Hex value of the Version Number (0xmmmmnnnn)
392 # where mmmm is the major number and nnnn is the adjusted minor number.
394 def GetHexVerValue(VerString
):
395 VerString
= CleanString(VerString
)
397 if gHumanReadableVerPatt
.match(VerString
):
398 ValueList
= VerString
.split('.')
403 DeciValue
= (int(Major
) << 16) + int(Minor
);
404 return "0x%08x"%DeciValue
405 elif gHexVerPatt
.match(VerString
):
411 ## GetSingleValueOfKeyFromLines
413 # Parse multiple strings as below to get value of each definition line
416 # The result is saved to Dictionary
418 # @param Lines: The content to be parsed
419 # @param Dictionary: To store data after parsing
420 # @param CommentCharacter: Comment char, be used to ignore comment content
421 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
422 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
423 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
425 # @retval True Successfully executed
427 def GetSingleValueOfKeyFromLines(Lines
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
428 Lines
= Lines
.split('\n')
436 # Handle DEFINE and SPEC
438 if Line
.find(DataType
.TAB_INF_DEFINES_DEFINE
+ ' ') > -1:
439 if '' in DefineValues
:
440 DefineValues
.remove('')
441 DefineValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_DEFINE
, CommentCharacter
))
443 if Line
.find(DataType
.TAB_INF_DEFINES_SPEC
+ ' ') > -1:
445 SpecValues
.remove('')
446 SpecValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_SPEC
, CommentCharacter
))
452 LineList
= Line
.split(KeySplitCharacter
, 1)
453 if len(LineList
) >= 2:
454 Key
= LineList
[0].split()
455 if len(Key
) == 1 and Key
[0][0] != CommentCharacter
:
457 # Remove comments and white spaces
459 LineList
[1] = CleanString(LineList
[1], CommentCharacter
)
461 Value
= map(string
.strip
, LineList
[1].split(ValueSplitCharacter
))
463 Value
= CleanString(LineList
[1], CommentCharacter
).splitlines()
465 if Key
[0] in Dictionary
:
466 if Key
[0] not in Keys
:
467 Dictionary
[Key
[0]] = Value
470 Dictionary
[Key
[0]].extend(Value
)
472 Dictionary
[DataType
.TAB_INF_DEFINES_MACRO
][Key
[0]] = Value
[0]
474 if DefineValues
== []:
478 Dictionary
[DataType
.TAB_INF_DEFINES_DEFINE
] = DefineValues
479 Dictionary
[DataType
.TAB_INF_DEFINES_SPEC
] = SpecValues
483 ## The content to be parsed
485 # Do pre-check for a file before it is parsed
489 # @param FileName: Used for error report
490 # @param FileContent: File content to be parsed
491 # @param SupSectionTag: Used for error report
493 def PreCheck(FileName
, FileContent
, SupSectionTag
):
497 for Line
in FileContent
.splitlines():
502 Line
= CleanString(Line
)
505 # Remove commented line
507 if Line
.find(DataType
.TAB_COMMA_SPLIT
) == 0:
512 if Line
.find('$') > -1:
513 if Line
.find('$(') < 0 or Line
.find(')') < 0:
514 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
= EdkLogger
.IsRaiseError
)
519 if Line
.find('[') > -1 or Line
.find(']') > -1:
521 # Only get one '[' or one ']'
523 if not (Line
.find('[') > -1 and Line
.find(']') > -1):
524 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
= EdkLogger
.IsRaiseError
)
527 # Regenerate FileContent
529 NewFileContent
= NewFileContent
+ Line
+ '\r\n'
532 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
= EdkLogger
.IsRaiseError
)
534 return NewFileContent
538 # Check if the Filename is including ExtName
539 # Return True if it exists
540 # Raise a error message if it not exists
542 # @param CheckFilename: Name of the file to be checked
543 # @param ExtName: Ext name of the file to be checked
544 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
545 # @param SectionName: Used for error report
546 # @param Line: The line in container file which defines the file to be checked
548 # @retval True The file type is correct
550 def CheckFileType(CheckFilename
, ExtName
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
551 if CheckFilename
!= '' and CheckFilename
!= None:
552 (Root
, Ext
) = os
.path
.splitext(CheckFilename
)
553 if Ext
.upper() != ExtName
.upper():
554 ContainerFile
= open(ContainerFilename
, 'r').read()
556 LineNo
= GetLineNo(ContainerFile
, Line
)
557 ErrorMsg
= "Invalid %s. '%s' is found, but '%s' file is needed" % (SectionName
, CheckFilename
, ExtName
)
558 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, Line
=LineNo
,
559 File
=ContainerFilename
, RaiseError
= EdkLogger
.IsRaiseError
)
565 # Check if the file exists
566 # Return True if it exists
567 # Raise a error message if it not exists
569 # @param CheckFilename: Name of the file to be checked
570 # @param WorkspaceDir: Current workspace dir
571 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
572 # @param SectionName: Used for error report
573 # @param Line: The line in container file which defines the file to be checked
575 # @retval The file full path if the file exists
577 def CheckFileExist(WorkspaceDir
, CheckFilename
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
579 if CheckFilename
!= '' and CheckFilename
!= None:
580 CheckFile
= WorkspaceFile(WorkspaceDir
, CheckFilename
)
581 if not os
.path
.isfile(CheckFile
):
582 ContainerFile
= open(ContainerFilename
, 'r').read()
584 LineNo
= GetLineNo(ContainerFile
, Line
)
585 ErrorMsg
= "Can't find file '%s' defined in section '%s'" % (CheckFile
, SectionName
)
586 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
,
587 File
=ContainerFilename
, Line
= LineNo
, RaiseError
= EdkLogger
.IsRaiseError
)
593 # Find the index of a line in a file
595 # @param FileContent: Search scope
596 # @param Line: Search key
598 # @retval int Index of the line
599 # @retval -1 The line is not found
601 def GetLineNo(FileContent
, Line
, IsIgnoreComment
= True):
602 LineList
= FileContent
.splitlines()
603 for Index
in range(len(LineList
)):
604 if LineList
[Index
].find(Line
) > -1:
606 # Ignore statement in comment
609 if LineList
[Index
].strip()[0] == DataType
.TAB_COMMENT_SPLIT
:
617 # Raise a parser error
619 # @param Line: String which has error
620 # @param Section: Used for error report
621 # @param File: File which has the string
622 # @param Format: Correct format
624 def RaiseParserError(Line
, Section
, File
, Format
= '', LineNo
= -1):
626 LineNo
= GetLineNo(open(os
.path
.normpath(File
), 'r').read(), Line
)
627 ErrorMsg
= "Invalid statement '%s' is found in section '%s'" % (Line
, Section
)
629 Format
= "Correct format is " + Format
630 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, File
=File
, Line
=LineNo
, ExtraData
=Format
, RaiseError
= EdkLogger
.IsRaiseError
)
634 # Return a full path with workspace dir
636 # @param WorkspaceDir: Workspace dir
637 # @param Filename: Relative file name
639 # @retval string A full path
641 def WorkspaceFile(WorkspaceDir
, Filename
):
642 return os
.path
.join(NormPath(WorkspaceDir
), NormPath(Filename
))
646 # Revmove '"' which startswith and endswith string
648 # @param String: The string need to be splited
650 # @retval String: The string after removed '""'
652 def SplitString(String
):
653 if String
.startswith('\"'):
655 if String
.endswith('\"'):
660 ## Convert To Sql String
662 # 1. Replace "'" with "''" in each item of StringList
664 # @param StringList: A list for strings to be converted
666 def ConvertToSqlString(StringList
):
667 return map(lambda s
: s
.replace("'", "''") , StringList
)
669 ## Convert To Sql String
671 # 1. Replace "'" with "''" in the String
673 # @param String: A String to be converted
675 def ConvertToSqlString2(String
):
676 return String
.replace("'", "''")
679 # Remove comment block
681 def RemoveBlockComment(Lines
):
682 IsFindBlockComment
= False
683 IsFindBlockCode
= False
690 # Remove comment block
692 if Line
.find(DataType
.TAB_COMMENT_R8_START
) > -1:
693 ReservedLine
= GetSplitValueList(Line
, DataType
.TAB_COMMENT_R8_START
, 1)[0]
694 IsFindBlockComment
= True
695 if Line
.find(DataType
.TAB_COMMENT_R8_END
) > -1:
696 Line
= ReservedLine
+ GetSplitValueList(Line
, DataType
.TAB_COMMENT_R8_END
, 1)[1]
698 IsFindBlockComment
= False
699 if IsFindBlockComment
:
703 NewLines
.append(Line
)
707 # Get String of a List
709 def GetStringOfList(List
, Split
= ' '):
710 if type(List
) != type([]):
714 Str
= Str
+ Item
+ Split
719 # Get HelpTextList from HelpTextClassList
721 def GetHelpTextList(HelpTextClassList
):
723 if HelpTextClassList
:
724 for HelpText
in HelpTextClassList
:
725 if HelpText
.String
.endswith('\n'):
726 HelpText
.String
= HelpText
.String
[0: len(HelpText
.String
) - len('\n')]
727 List
.extend(HelpText
.String
.split('\n'))
731 def StringToArray(String
):
732 if isinstance(String
, unicode):
734 return "{0x00, 0x00}"
735 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C
) for C
in String
])
736 elif String
.startswith('L"'):
737 if String
== "L\"\"":
738 return "{0x00, 0x00}"
740 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C
) for C
in String
[2:-1]])
741 elif String
.startswith('"'):
745 return "{%s, 0x00}" % ", ".join(["0x%02x" % ord(C
) for C
in String
[1:-1]])
747 return '{%s, 0}' % ', '.join(String
.split())
749 def StringArrayLength(String
):
750 if isinstance(String
, unicode):
751 return (len(String
) + 1) * 2 + 1;
752 elif String
.startswith('L"'):
753 return (len(String
) - 3 + 1) * 2
754 elif String
.startswith('"'):
755 return (len(String
) - 2 + 1)
757 return len(String
.split()) + 1
759 def RemoveDupOption(OptionString
, Which
="/I", Against
=None):
760 OptionList
= OptionString
.split()
764 for Index
in range(len(OptionList
)):
765 Opt
= OptionList
[Index
]
766 if not Opt
.startswith(Which
):
768 if len(Opt
) > len(Which
):
769 Val
= Opt
[len(Which
):]
773 OptionList
[Index
] = ""
775 ValueList
.append(Val
)
776 return " ".join(OptionList
)
780 # This acts like the main() function for the script, unless it is 'import'ed into another
783 if __name__
== '__main__':