]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/StringUtils.py
2 # This file is used to define common string related functions used in parsing process
4 # Copyright (c) 2007 - 2018, 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.
17 from __future__
import absolute_import
19 from . import DataType
20 import Common
.LongFilePathOs
as os
22 from . import EdkLogger
as EdkLogger
24 from . import GlobalData
25 from .BuildToolError
import *
26 from CommonDataClass
.Exceptions
import *
27 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
28 from Common
.MultipleWorkspace
import MultipleWorkspace
as mws
30 gHexVerPatt
= re
.compile('0x[a-f0-9]{4}[a-f0-9]{4}$', re
.IGNORECASE
)
31 gHumanReadableVerPatt
= re
.compile(r
'([1-9][0-9]*|0)\.[0-9]{1,2}$')
35 # Get a value list from a string with multiple values splited with SplitTag
36 # The default SplitTag is DataType.TAB_VALUE_SPLIT
37 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
39 # @param String: The input string to be splitted
40 # @param SplitTag: The split key, default is DataType.TAB_VALUE_SPLIT
41 # @param MaxSplit: The max number of split values, default is -1
43 # @retval list() A list for splitted string
45 def GetSplitValueList(String
, SplitTag
=DataType
.TAB_VALUE_SPLIT
, MaxSplit
= -1):
49 InSingleQuoteString
= False
50 InDoubleQuoteString
= False
52 for Index
in range(0, len(String
)):
56 # Found a splitter not in a string, split it
57 if (not InSingleQuoteString
or not InDoubleQuoteString
) and InParenthesis
== 0 and Char
== SplitTag
:
58 ValueList
.append(String
[Last
:Index
].strip())
60 if MaxSplit
> 0 and len(ValueList
) >= MaxSplit
:
63 if Char
== '\\' and (InSingleQuoteString
or InDoubleQuoteString
):
65 elif Char
== '"' and not InSingleQuoteString
:
66 if not InDoubleQuoteString
:
67 InDoubleQuoteString
= True
69 InDoubleQuoteString
= False
70 elif Char
== "'" and not InDoubleQuoteString
:
71 if not InSingleQuoteString
:
72 InSingleQuoteString
= True
74 InSingleQuoteString
= False
76 InParenthesis
= InParenthesis
+ 1
78 InParenthesis
= InParenthesis
- 1
82 if Last
< len(String
):
83 ValueList
.append(String
[Last
:].strip())
84 elif Last
== len(String
):
91 # Get a value list from a string with multiple values splited with SplitString
92 # The default SplitTag is DataType.TAB_VALUE_SPLIT
93 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
95 # @param String: The input string to be splitted
96 # @param SplitStr: The split key, default is DataType.TAB_VALUE_SPLIT
97 # @param MaxSplit: The max number of split values, default is -1
99 # @retval list() A list for splitted string
101 def GetSplitList(String
, SplitStr
=DataType
.TAB_VALUE_SPLIT
, MaxSplit
= -1):
102 return map(lambda l
: l
.strip(), String
.split(SplitStr
, MaxSplit
))
106 # Find a key's all arches in dict, add the new arch to the list
107 # If not exist any arch, set the arch directly
109 # @param Dict: The input value for Dict
110 # @param Key: The input value for Key
111 # @param Arch: The Arch to be added or merged
113 def MergeArches(Dict
, Key
, Arch
):
115 Dict
[Key
].append(Arch
)
117 Dict
[Key
] = Arch
.split()
121 # Parse a string with format "DEFINE <VarName> = <PATH>"
122 # Generate a map Defines[VarName] = PATH
123 # Return False if invalid format
125 # @param String: String with DEFINE statement
126 # @param Arch: Supportted Arch
127 # @param Defines: DEFINE statement to be parsed
129 # @retval 0 DEFINE statement found, and valid
130 # @retval 1 DEFINE statement found, but not valid
131 # @retval -1 DEFINE statement not found
133 def GenDefines(String
, Arch
, Defines
):
134 if String
.find(DataType
.TAB_DEFINE
+ ' ') > -1:
135 List
= String
.replace(DataType
.TAB_DEFINE
+ ' ', '').split(DataType
.TAB_EQUAL_SPLIT
)
137 Defines
[(CleanString(List
[0]), Arch
)] = CleanString(List
[1])
146 # Parse a string with format "!include <Filename>"
147 # Return the file path
148 # Return False if invalid format or NOT FOUND
150 # @param String: String with INCLUDE statement
151 # @param IncludeFiles: INCLUDE statement to be parsed
152 # @param Arch: Supportted Arch
157 def GenInclude(String
, IncludeFiles
, Arch
):
158 if String
.upper().find(DataType
.TAB_INCLUDE
.upper() + ' ') > -1:
159 IncludeFile
= CleanString(String
[String
.upper().find(DataType
.TAB_INCLUDE
.upper() + ' ') + len(DataType
.TAB_INCLUDE
+ ' ') : ])
160 MergeArches(IncludeFiles
, IncludeFile
, Arch
)
165 ## GetLibraryClassesWithModuleType
167 # Get Library Class definition when no module type defined
169 # @param Lines: The content to be parsed
170 # @param Key: Reserved
171 # @param KeyValues: To store data after parsing
172 # @param CommentCharacter: Comment char, used to ignore comment content
174 # @retval True Get library classes successfully
176 def GetLibraryClassesWithModuleType(Lines
, Key
, KeyValues
, CommentCharacter
):
177 newKey
= SplitModuleType(Key
)
178 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
179 LineList
= Lines
.splitlines()
180 for Line
in LineList
:
181 Line
= CleanString(Line
, CommentCharacter
)
182 if Line
!= '' and Line
[0] != CommentCharacter
:
183 KeyValues
.append([CleanString(Line
, CommentCharacter
), newKey
[1]])
191 # @param Lines: The content to be parsed
192 # @param Key: Reserved
193 # @param KeyValues: To store data after parsing
194 # @param CommentCharacter: Comment char, used to ignore comment content
196 # @retval True Get Dynamic Pcds successfully
198 def GetDynamics(Lines
, Key
, KeyValues
, CommentCharacter
):
200 # Get SkuId Name List
202 SkuIdNameList
= SplitModuleType(Key
)
204 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
205 LineList
= Lines
.splitlines()
206 for Line
in LineList
:
207 Line
= CleanString(Line
, CommentCharacter
)
208 if Line
!= '' and Line
[0] != CommentCharacter
:
209 KeyValues
.append([CleanString(Line
, CommentCharacter
), SkuIdNameList
[1]])
215 # Split ModuleType out of section defien to get key
216 # [LibraryClass.Arch.ModuleType|ModuleType|ModuleType] -> [ 'LibraryClass.Arch', ['ModuleType', 'ModuleType', 'ModuleType'] ]
218 # @param Key: String to be parsed
220 # @retval ReturnValue A list for module types
222 def SplitModuleType(Key
):
223 KeyList
= Key
.split(DataType
.TAB_SPLIT
)
229 # Fill in for moduletype
233 KeyValue
= KeyList
[0]
235 KeyValue
= KeyValue
+ DataType
.TAB_SPLIT
+ KeyList
[1]
236 ReturnValue
.append(KeyValue
)
237 ReturnValue
.append(GetSplitValueList(KeyList
[2]))
241 ## Replace macro in strings list
243 # This method replace macros used in a given string list. The macros are
244 # given in a dictionary.
246 # @param StringList StringList to be processed
247 # @param MacroDefinitions The macro definitions in the form of dictionary
248 # @param SelfReplacement To decide whether replace un-defined macro to ''
250 # @retval NewList A new string list whose macros are replaced
252 def ReplaceMacros(StringList
, MacroDefinitions
={}, SelfReplacement
=False):
254 for String
in StringList
:
255 if isinstance(String
, type('')):
256 NewList
.append(ReplaceMacro(String
, MacroDefinitions
, SelfReplacement
))
258 NewList
.append(String
)
262 ## Replace macro in string
264 # This method replace macros used in given string. The macros are given in a
267 # @param String String to be processed
268 # @param MacroDefinitions The macro definitions in the form of dictionary
269 # @param SelfReplacement To decide whether replace un-defined macro to ''
271 # @retval string The string whose macros are replaced
273 def ReplaceMacro(String
, MacroDefinitions
={}, SelfReplacement
=False, RaiseError
=False):
275 while String
and MacroDefinitions
:
276 MacroUsed
= GlobalData
.gMacroRefPattern
.findall(String
)
277 # no macro found in String, stop replacing
278 if len(MacroUsed
) == 0:
281 for Macro
in MacroUsed
:
282 if Macro
not in MacroDefinitions
:
284 raise SymbolNotFound("%s not defined" % Macro
)
286 String
= String
.replace("$(%s)" % Macro
, '')
288 if "$(%s)" % Macro
not in MacroDefinitions
[Macro
]:
289 String
= String
.replace("$(%s)" % Macro
, MacroDefinitions
[Macro
])
290 # in case there's macro not defined
291 if String
== LastString
:
299 # Create a normal path
300 # And replace DFEINE in the path
302 # @param Path: The input value for Path to be converted
303 # @param Defines: A set for DEFINE statement
305 # @retval Path Formatted path
307 def NormPath(Path
, Defines
={}):
308 IsRelativePath
= False
311 IsRelativePath
= True
313 # Replace with Define
316 Path
= ReplaceMacro(Path
, Defines
)
318 # To local path format
320 Path
= os
.path
.normpath(Path
)
321 if Path
.startswith(GlobalData
.gWorkspace
) and not Path
.startswith(GlobalData
.gBuildDirectory
) and not os
.path
.exists(Path
):
322 Path
= Path
[len (GlobalData
.gWorkspace
):]
323 if Path
[0] == os
.path
.sep
:
325 Path
= mws
.join(GlobalData
.gWorkspace
, Path
)
327 if IsRelativePath
and Path
[0] != '.':
328 Path
= os
.path
.join('.', Path
)
334 # Remove comments in a string
337 # @param Line: The string to be cleaned
338 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
340 # @retval Path Formatted path
342 def CleanString(Line
, CommentCharacter
=DataType
.TAB_COMMENT_SPLIT
, AllowCppStyleComment
=False, BuildOption
=False):
348 # Replace Edk's comment character
350 if AllowCppStyleComment
:
351 Line
= Line
.replace(DataType
.TAB_COMMENT_EDK_SPLIT
, CommentCharacter
)
353 # remove comments, but we should escape comment character in string
355 InDoubleQuoteString
= False
356 InSingleQuoteString
= False
357 CommentInString
= False
358 for Index
in range(0, len(Line
)):
359 if Line
[Index
] == '"' and not InSingleQuoteString
:
360 InDoubleQuoteString
= not InDoubleQuoteString
361 elif Line
[Index
] == "'" and not InDoubleQuoteString
:
362 InSingleQuoteString
= not InSingleQuoteString
363 elif Line
[Index
] == CommentCharacter
and (InSingleQuoteString
or InDoubleQuoteString
):
364 CommentInString
= True
365 elif Line
[Index
] == CommentCharacter
and not (InSingleQuoteString
or InDoubleQuoteString
):
366 Line
= Line
[0: Index
]
369 if CommentInString
and BuildOption
:
370 Line
= Line
.replace('"', '')
371 ChIndex
= Line
.find('#')
373 if GlobalData
.gIsWindows
:
374 if ChIndex
== 0 or Line
[ChIndex
- 1] != '^':
375 Line
= Line
[0:ChIndex
] + '^' + Line
[ChIndex
:]
376 ChIndex
= Line
.find('#', ChIndex
+ 2)
378 ChIndex
= Line
.find('#', ChIndex
+ 1)
380 if ChIndex
== 0 or Line
[ChIndex
- 1] != '\\':
381 Line
= Line
[0:ChIndex
] + '\\' + Line
[ChIndex
:]
382 ChIndex
= Line
.find('#', ChIndex
+ 2)
384 ChIndex
= Line
.find('#', ChIndex
+ 1)
386 # remove whitespace again
394 # Split statement with comments in a string
397 # @param Line: The string to be cleaned
398 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
400 # @retval Path Formatted path
402 def CleanString2(Line
, CommentCharacter
=DataType
.TAB_COMMENT_SPLIT
, AllowCppStyleComment
=False):
408 # Replace Edk's comment character
410 if AllowCppStyleComment
:
411 Line
= Line
.replace(DataType
.TAB_COMMENT_EDK_SPLIT
, CommentCharacter
)
413 # separate comments and statements, but we should escape comment character in string
415 InDoubleQuoteString
= False
416 InSingleQuoteString
= False
417 CommentInString
= False
419 for Index
in range(0, len(Line
)):
420 if Line
[Index
] == '"' and not InSingleQuoteString
:
421 InDoubleQuoteString
= not InDoubleQuoteString
422 elif Line
[Index
] == "'" and not InDoubleQuoteString
:
423 InSingleQuoteString
= not InSingleQuoteString
424 elif Line
[Index
] == CommentCharacter
and (InDoubleQuoteString
or InSingleQuoteString
):
425 CommentInString
= True
426 elif Line
[Index
] == CommentCharacter
and not (InDoubleQuoteString
or InSingleQuoteString
):
427 Comment
= Line
[Index
:].strip()
428 Line
= Line
[0:Index
].strip()
433 ## GetMultipleValuesOfKeyFromLines
435 # Parse multiple strings to clean comment and spaces
436 # The result is saved to KeyValues
438 # @param Lines: The content to be parsed
439 # @param Key: Reserved
440 # @param KeyValues: To store data after parsing
441 # @param CommentCharacter: Comment char, used to ignore comment content
443 # @retval True Successfully executed
445 def GetMultipleValuesOfKeyFromLines(Lines
, Key
, KeyValues
, CommentCharacter
):
446 Lines
= Lines
.split(DataType
.TAB_SECTION_END
, 1)[1]
447 LineList
= Lines
.split('\n')
448 for Line
in LineList
:
449 Line
= CleanString(Line
, CommentCharacter
)
450 if Line
!= '' and Line
[0] != CommentCharacter
:
451 KeyValues
.append(Line
)
457 # Parse a DEFINE statement to get defined value
460 # @param String: The content to be parsed
461 # @param Key: The key of DEFINE statement
462 # @param CommentCharacter: Comment char, used to ignore comment content
464 # @retval string The defined value
466 def GetDefineValue(String
, Key
, CommentCharacter
):
467 String
= CleanString(String
)
468 return String
[String
.find(Key
+ ' ') + len(Key
+ ' ') : ]
472 # Get a Hex Version Value
474 # @param VerString: The version string to be parsed
477 # @retval: If VerString is incorrectly formatted, return "None" which will break the build.
478 # If VerString is correctly formatted, return a Hex value of the Version Number (0xmmmmnnnn)
479 # where mmmm is the major number and nnnn is the adjusted minor number.
481 def GetHexVerValue(VerString
):
482 VerString
= CleanString(VerString
)
484 if gHumanReadableVerPatt
.match(VerString
):
485 ValueList
= VerString
.split('.')
490 DeciValue
= (int(Major
) << 16) + int(Minor
);
491 return "0x%08x" % DeciValue
492 elif gHexVerPatt
.match(VerString
):
498 ## GetSingleValueOfKeyFromLines
500 # Parse multiple strings as below to get value of each definition line
503 # The result is saved to Dictionary
505 # @param Lines: The content to be parsed
506 # @param Dictionary: To store data after parsing
507 # @param CommentCharacter: Comment char, be used to ignore comment content
508 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
509 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
510 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
512 # @retval True Successfully executed
514 def GetSingleValueOfKeyFromLines(Lines
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
515 Lines
= Lines
.split('\n')
523 # Handle DEFINE and SPEC
525 if Line
.find(DataType
.TAB_INF_DEFINES_DEFINE
+ ' ') > -1:
526 if '' in DefineValues
:
527 DefineValues
.remove('')
528 DefineValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_DEFINE
, CommentCharacter
))
530 if Line
.find(DataType
.TAB_INF_DEFINES_SPEC
+ ' ') > -1:
532 SpecValues
.remove('')
533 SpecValues
.append(GetDefineValue(Line
, DataType
.TAB_INF_DEFINES_SPEC
, CommentCharacter
))
539 LineList
= Line
.split(KeySplitCharacter
, 1)
540 if len(LineList
) >= 2:
541 Key
= LineList
[0].split()
542 if len(Key
) == 1 and Key
[0][0] != CommentCharacter
:
544 # Remove comments and white spaces
546 LineList
[1] = CleanString(LineList
[1], CommentCharacter
)
548 Value
= map(string
.strip
, LineList
[1].split(ValueSplitCharacter
))
550 Value
= CleanString(LineList
[1], CommentCharacter
).splitlines()
552 if Key
[0] in Dictionary
:
553 if Key
[0] not in Keys
:
554 Dictionary
[Key
[0]] = Value
557 Dictionary
[Key
[0]].extend(Value
)
559 Dictionary
[DataType
.TAB_INF_DEFINES_MACRO
][Key
[0]] = Value
[0]
561 if DefineValues
== []:
565 Dictionary
[DataType
.TAB_INF_DEFINES_DEFINE
] = DefineValues
566 Dictionary
[DataType
.TAB_INF_DEFINES_SPEC
] = SpecValues
570 ## The content to be parsed
572 # Do pre-check for a file before it is parsed
576 # @param FileName: Used for error report
577 # @param FileContent: File content to be parsed
578 # @param SupSectionTag: Used for error report
580 def PreCheck(FileName
, FileContent
, SupSectionTag
):
584 for Line
in FileContent
.splitlines():
589 Line
= CleanString(Line
)
592 # Remove commented line
594 if Line
.find(DataType
.TAB_COMMA_SPLIT
) == 0:
599 if Line
.find('$') > -1:
600 if Line
.find('$(') < 0 or Line
.find(')') < 0:
601 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
=EdkLogger
.IsRaiseError
)
606 if Line
.find('[') > -1 or Line
.find(']') > -1:
608 # Only get one '[' or one ']'
610 if not (Line
.find('[') > -1 and Line
.find(']') > -1):
611 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
=EdkLogger
.IsRaiseError
)
614 # Regenerate FileContent
616 NewFileContent
= NewFileContent
+ Line
+ '\r\n'
619 EdkLogger
.error("Parser", FORMAT_INVALID
, Line
=LineNo
, File
=FileName
, RaiseError
=EdkLogger
.IsRaiseError
)
621 return NewFileContent
625 # Check if the Filename is including ExtName
626 # Return True if it exists
627 # Raise a error message if it not exists
629 # @param CheckFilename: Name of the file to be checked
630 # @param ExtName: Ext name of the file to be checked
631 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
632 # @param SectionName: Used for error report
633 # @param Line: The line in container file which defines the file to be checked
635 # @retval True The file type is correct
637 def CheckFileType(CheckFilename
, ExtName
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
638 if CheckFilename
!= '' and CheckFilename
is not None:
639 (Root
, Ext
) = os
.path
.splitext(CheckFilename
)
640 if Ext
.upper() != ExtName
.upper():
641 ContainerFile
= open(ContainerFilename
, 'r').read()
643 LineNo
= GetLineNo(ContainerFile
, Line
)
644 ErrorMsg
= "Invalid %s. '%s' is found, but '%s' file is needed" % (SectionName
, CheckFilename
, ExtName
)
645 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, Line
=LineNo
,
646 File
=ContainerFilename
, RaiseError
=EdkLogger
.IsRaiseError
)
652 # Check if the file exists
653 # Return True if it exists
654 # Raise a error message if it not exists
656 # @param CheckFilename: Name of the file to be checked
657 # @param WorkspaceDir: Current workspace dir
658 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
659 # @param SectionName: Used for error report
660 # @param Line: The line in container file which defines the file to be checked
662 # @retval The file full path if the file exists
664 def CheckFileExist(WorkspaceDir
, CheckFilename
, ContainerFilename
, SectionName
, Line
, LineNo
= -1):
666 if CheckFilename
!= '' and CheckFilename
is not None:
667 CheckFile
= WorkspaceFile(WorkspaceDir
, CheckFilename
)
668 if not os
.path
.isfile(CheckFile
):
669 ContainerFile
= open(ContainerFilename
, 'r').read()
671 LineNo
= GetLineNo(ContainerFile
, Line
)
672 ErrorMsg
= "Can't find file '%s' defined in section '%s'" % (CheckFile
, SectionName
)
673 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
,
674 File
=ContainerFilename
, Line
=LineNo
, RaiseError
=EdkLogger
.IsRaiseError
)
680 # Find the index of a line in a file
682 # @param FileContent: Search scope
683 # @param Line: Search key
685 # @retval int Index of the line
686 # @retval -1 The line is not found
688 def GetLineNo(FileContent
, Line
, IsIgnoreComment
=True):
689 LineList
= FileContent
.splitlines()
690 for Index
in range(len(LineList
)):
691 if LineList
[Index
].find(Line
) > -1:
693 # Ignore statement in comment
696 if LineList
[Index
].strip()[0] == DataType
.TAB_COMMENT_SPLIT
:
704 # Raise a parser error
706 # @param Line: String which has error
707 # @param Section: Used for error report
708 # @param File: File which has the string
709 # @param Format: Correct format
711 def RaiseParserError(Line
, Section
, File
, Format
='', LineNo
= -1):
713 LineNo
= GetLineNo(open(os
.path
.normpath(File
), 'r').read(), Line
)
714 ErrorMsg
= "Invalid statement '%s' is found in section '%s'" % (Line
, Section
)
716 Format
= "Correct format is " + Format
717 EdkLogger
.error("Parser", PARSER_ERROR
, ErrorMsg
, File
=File
, Line
=LineNo
, ExtraData
=Format
, RaiseError
=EdkLogger
.IsRaiseError
)
721 # Return a full path with workspace dir
723 # @param WorkspaceDir: Workspace dir
724 # @param Filename: Relative file name
726 # @retval string A full path
728 def WorkspaceFile(WorkspaceDir
, Filename
):
729 return mws
.join(NormPath(WorkspaceDir
), NormPath(Filename
))
733 # Revmove '"' which startswith and endswith string
735 # @param String: The string need to be splited
737 # @retval String: The string after removed '""'
739 def SplitString(String
):
740 if String
.startswith('\"'):
742 if String
.endswith('\"'):
747 ## Convert To Sql String
749 # 1. Replace "'" with "''" in each item of StringList
751 # @param StringList: A list for strings to be converted
753 def ConvertToSqlString(StringList
):
754 return map(lambda s
: s
.replace("'", "''"), StringList
)
756 ## Convert To Sql String
758 # 1. Replace "'" with "''" in the String
760 # @param String: A String to be converted
762 def ConvertToSqlString2(String
):
763 return String
.replace("'", "''")
766 # Remove comment block
768 def RemoveBlockComment(Lines
):
769 IsFindBlockComment
= False
770 IsFindBlockCode
= False
777 # Remove comment block
779 if Line
.find(DataType
.TAB_COMMENT_EDK_START
) > -1:
780 ReservedLine
= GetSplitList(Line
, DataType
.TAB_COMMENT_EDK_START
, 1)[0]
781 IsFindBlockComment
= True
782 if Line
.find(DataType
.TAB_COMMENT_EDK_END
) > -1:
783 Line
= ReservedLine
+ GetSplitList(Line
, DataType
.TAB_COMMENT_EDK_END
, 1)[1]
785 IsFindBlockComment
= False
786 if IsFindBlockComment
:
790 NewLines
.append(Line
)
794 # Get String of a List
796 def GetStringOfList(List
, Split
=' '):
797 if not isinstance(List
, type([])):
801 Str
= Str
+ Item
+ Split
806 # Get HelpTextList from HelpTextClassList
808 def GetHelpTextList(HelpTextClassList
):
810 if HelpTextClassList
:
811 for HelpText
in HelpTextClassList
:
812 if HelpText
.String
.endswith('\n'):
813 HelpText
.String
= HelpText
.String
[0: len(HelpText
.String
) - len('\n')]
814 List
.extend(HelpText
.String
.split('\n'))
818 def StringToArray(String
):
819 if isinstance(String
, unicode):
820 if len(unicode) == 0:
822 return "{%s,0x00,0x00}" % ",".join("0x%02x,0x00" % ord(C
) for C
in String
)
823 elif String
.startswith('L"'):
824 if String
== "L\"\"":
827 return "{%s,0x00,0x00}" % ",".join("0x%02x,0x00" % ord(C
) for C
in String
[2:-1])
828 elif String
.startswith('"'):
832 StringLen
= len(String
[1:-1])
834 return "{%s,0x00}" % ",".join("0x%02x" % ord(C
) for C
in String
[1:-1])
836 return "{%s,0x00,0x00}" % ",".join("0x%02x" % ord(C
) for C
in String
[1:-1])
837 elif String
.startswith('{'):
838 StringLen
= len(String
.split(","))
840 return "{%s,0x00}" % ",".join(C
.strip() for C
in String
[1:-1].split(','))
842 return "{%s}" % ",".join(C
.strip() for C
in String
[1:-1].split(','))
845 if len(String
.split()) % 2:
846 return '{%s,0}' % ','.join(String
.split())
848 return '{%s,0,0}' % ','.join(String
.split())
850 def StringArrayLength(String
):
851 if isinstance(String
, unicode):
852 return (len(String
) + 1) * 2 + 1;
853 elif String
.startswith('L"'):
854 return (len(String
) - 3 + 1) * 2
855 elif String
.startswith('"'):
856 return (len(String
) - 2 + 1)
858 return len(String
.split()) + 1
860 def RemoveDupOption(OptionString
, Which
="/I", Against
=None):
861 OptionList
= OptionString
.split()
865 for Index
in range(len(OptionList
)):
866 Opt
= OptionList
[Index
]
867 if not Opt
.startswith(Which
):
869 if len(Opt
) > len(Which
):
870 Val
= Opt
[len(Which
):]
874 OptionList
[Index
] = ""
876 ValueList
.append(Val
)
877 return " ".join(OptionList
)
881 # This acts like the main() function for the script, unless it is 'import'ed into another
884 if __name__
== '__main__':