]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/String.py
BaseTools: Clear build versions to sync with buildtools/BaseTools
[mirror_edk2.git] / BaseTools / Source / Python / Common / String.py
1 ## @file
2 # This file is used to define common string related functions used in parsing process
3 #
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
9 #
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.
12 #
13
14 ##
15 # Import Modules
16 #
17 import re
18 import DataType
19 import os.path
20 import string
21 import EdkLogger as EdkLogger
22
23 import GlobalData
24 from BuildToolError import *
25 from CommonDataClass.Exceptions import *
26
27 gHexVerPatt = re.compile('0x[a-f0-9]{4}[a-f0-9]{4}$', re.IGNORECASE)
28 gHumanReadableVerPatt = re.compile(r'([1-9][0-9]*|0)\.[0-9]{1,2}$')
29
30 ## GetSplitValueList
31 #
32 # Get a value list from a string with multiple values splited with SplitTag
33 # The default SplitTag is DataType.TAB_VALUE_SPLIT
34 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
35 #
36 # @param String: The input string to be splitted
37 # @param SplitTag: The split key, default is DataType.TAB_VALUE_SPLIT
38 # @param MaxSplit: The max number of split values, default is -1
39 #
40 # @retval list() A list for splitted string
41 #
42 def GetSplitValueList(String, SplitTag=DataType.TAB_VALUE_SPLIT, MaxSplit= -1):
43 ValueList = []
44 Last = 0
45 Escaped = False
46 InString = False
47 for Index in range(0, len(String)):
48 Char = String[Index]
49
50 if not Escaped:
51 # Found a splitter not in a string, split it
52 if not InString and Char == SplitTag:
53 ValueList.append(String[Last:Index].strip())
54 Last = Index + 1
55 if MaxSplit > 0 and len(ValueList) >= MaxSplit:
56 break
57
58 if Char == '\\' and InString:
59 Escaped = True
60 elif Char == '"':
61 if not InString:
62 InString = True
63 else:
64 InString = False
65 else:
66 Escaped = False
67
68 if Last < len(String):
69 ValueList.append(String[Last:].strip())
70 elif Last == len(String):
71 ValueList.append('')
72
73 return ValueList
74
75 ## GetSplitList
76 #
77 # Get a value list from a string with multiple values splited with SplitString
78 # The default SplitTag is DataType.TAB_VALUE_SPLIT
79 # 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']
80 #
81 # @param String: The input string to be splitted
82 # @param SplitStr: The split key, default is DataType.TAB_VALUE_SPLIT
83 # @param MaxSplit: The max number of split values, default is -1
84 #
85 # @retval list() A list for splitted string
86 #
87 def GetSplitList(String, SplitStr=DataType.TAB_VALUE_SPLIT, MaxSplit= -1):
88 return map(lambda l: l.strip(), String.split(SplitStr, MaxSplit))
89
90 ## MergeArches
91 #
92 # Find a key's all arches in dict, add the new arch to the list
93 # If not exist any arch, set the arch directly
94 #
95 # @param Dict: The input value for Dict
96 # @param Key: The input value for Key
97 # @param Arch: The Arch to be added or merged
98 #
99 def MergeArches(Dict, Key, Arch):
100 if Key in Dict.keys():
101 Dict[Key].append(Arch)
102 else:
103 Dict[Key] = Arch.split()
104
105 ## GenDefines
106 #
107 # Parse a string with format "DEFINE <VarName> = <PATH>"
108 # Generate a map Defines[VarName] = PATH
109 # Return False if invalid format
110 #
111 # @param String: String with DEFINE statement
112 # @param Arch: Supportted Arch
113 # @param Defines: DEFINE statement to be parsed
114 #
115 # @retval 0 DEFINE statement found, and valid
116 # @retval 1 DEFINE statement found, but not valid
117 # @retval -1 DEFINE statement not found
118 #
119 def GenDefines(String, Arch, Defines):
120 if String.find(DataType.TAB_DEFINE + ' ') > -1:
121 List = String.replace(DataType.TAB_DEFINE + ' ', '').split(DataType.TAB_EQUAL_SPLIT)
122 if len(List) == 2:
123 Defines[(CleanString(List[0]), Arch)] = CleanString(List[1])
124 return 0
125 else:
126 return -1
127
128 return 1
129
130 ## GenInclude
131 #
132 # Parse a string with format "!include <Filename>"
133 # Return the file path
134 # Return False if invalid format or NOT FOUND
135 #
136 # @param String: String with INCLUDE statement
137 # @param IncludeFiles: INCLUDE statement to be parsed
138 # @param Arch: Supportted Arch
139 #
140 # @retval True
141 # @retval False
142 #
143 def GenInclude(String, IncludeFiles, Arch):
144 if String.upper().find(DataType.TAB_INCLUDE.upper() + ' ') > -1:
145 IncludeFile = CleanString(String[String.upper().find(DataType.TAB_INCLUDE.upper() + ' ') + len(DataType.TAB_INCLUDE + ' ') : ])
146 MergeArches(IncludeFiles, IncludeFile, Arch)
147 return True
148 else:
149 return False
150
151 ## GetLibraryClassesWithModuleType
152 #
153 # Get Library Class definition when no module type defined
154 #
155 # @param Lines: The content to be parsed
156 # @param Key: Reserved
157 # @param KeyValues: To store data after parsing
158 # @param CommentCharacter: Comment char, used to ignore comment content
159 #
160 # @retval True Get library classes successfully
161 #
162 def GetLibraryClassesWithModuleType(Lines, Key, KeyValues, CommentCharacter):
163 newKey = SplitModuleType(Key)
164 Lines = Lines.split(DataType.TAB_SECTION_END, 1)[1]
165 LineList = Lines.splitlines()
166 for Line in LineList:
167 Line = CleanString(Line, CommentCharacter)
168 if Line != '' and Line[0] != CommentCharacter:
169 KeyValues.append([CleanString(Line, CommentCharacter), newKey[1]])
170
171 return True
172
173 ## GetDynamics
174 #
175 # Get Dynamic Pcds
176 #
177 # @param Lines: The content to be parsed
178 # @param Key: Reserved
179 # @param KeyValues: To store data after parsing
180 # @param CommentCharacter: Comment char, used to ignore comment content
181 #
182 # @retval True Get Dynamic Pcds successfully
183 #
184 def GetDynamics(Lines, Key, KeyValues, CommentCharacter):
185 #
186 # Get SkuId Name List
187 #
188 SkuIdNameList = SplitModuleType(Key)
189
190 Lines = Lines.split(DataType.TAB_SECTION_END, 1)[1]
191 LineList = Lines.splitlines()
192 for Line in LineList:
193 Line = CleanString(Line, CommentCharacter)
194 if Line != '' and Line[0] != CommentCharacter:
195 KeyValues.append([CleanString(Line, CommentCharacter), SkuIdNameList[1]])
196
197 return True
198
199 ## SplitModuleType
200 #
201 # Split ModuleType out of section defien to get key
202 # [LibraryClass.Arch.ModuleType|ModuleType|ModuleType] -> [ 'LibraryClass.Arch', ['ModuleType', 'ModuleType', 'ModuleType'] ]
203 #
204 # @param Key: String to be parsed
205 #
206 # @retval ReturnValue A list for module types
207 #
208 def SplitModuleType(Key):
209 KeyList = Key.split(DataType.TAB_SPLIT)
210 #
211 # Fill in for arch
212 #
213 KeyList.append('')
214 #
215 # Fill in for moduletype
216 #
217 KeyList.append('')
218 ReturnValue = []
219 KeyValue = KeyList[0]
220 if KeyList[1] != '':
221 KeyValue = KeyValue + DataType.TAB_SPLIT + KeyList[1]
222 ReturnValue.append(KeyValue)
223 ReturnValue.append(GetSplitValueList(KeyList[2]))
224
225 return ReturnValue
226
227 ## Replace macro in strings list
228 #
229 # This method replace macros used in a given string list. The macros are
230 # given in a dictionary.
231 #
232 # @param StringList StringList to be processed
233 # @param MacroDefinitions The macro definitions in the form of dictionary
234 # @param SelfReplacement To decide whether replace un-defined macro to ''
235 #
236 # @retval NewList A new string list whose macros are replaced
237 #
238 def ReplaceMacros(StringList, MacroDefinitions={}, SelfReplacement=False):
239 NewList = []
240 for String in StringList:
241 if type(String) == type(''):
242 NewList.append(ReplaceMacro(String, MacroDefinitions, SelfReplacement))
243 else:
244 NewList.append(String)
245
246 return NewList
247
248 ## Replace macro in string
249 #
250 # This method replace macros used in given string. The macros are given in a
251 # dictionary.
252 #
253 # @param String String to be processed
254 # @param MacroDefinitions The macro definitions in the form of dictionary
255 # @param SelfReplacement To decide whether replace un-defined macro to ''
256 #
257 # @retval string The string whose macros are replaced
258 #
259 def ReplaceMacro(String, MacroDefinitions={}, SelfReplacement=False, RaiseError=False):
260 LastString = String
261 while String and MacroDefinitions:
262 MacroUsed = GlobalData.gMacroRefPattern.findall(String)
263 # no macro found in String, stop replacing
264 if len(MacroUsed) == 0:
265 break
266
267 for Macro in MacroUsed:
268 if Macro not in MacroDefinitions:
269 if RaiseError:
270 raise SymbolNotFound("%s not defined" % Macro)
271 if SelfReplacement:
272 String = String.replace("$(%s)" % Macro, '')
273 continue
274 String = String.replace("$(%s)" % Macro, MacroDefinitions[Macro])
275 # in case there's macro not defined
276 if String == LastString:
277 break
278 LastString = String
279
280 return String
281
282 ## NormPath
283 #
284 # Create a normal path
285 # And replace DFEINE in the path
286 #
287 # @param Path: The input value for Path to be converted
288 # @param Defines: A set for DEFINE statement
289 #
290 # @retval Path Formatted path
291 #
292 def NormPath(Path, Defines={}):
293 IsRelativePath = False
294 if Path:
295 if Path[0] == '.':
296 IsRelativePath = True
297 #
298 # Replace with Define
299 #
300 if Defines:
301 Path = ReplaceMacro(Path, Defines)
302 #
303 # To local path format
304 #
305 Path = os.path.normpath(Path)
306
307 if IsRelativePath and Path[0] != '.':
308 Path = os.path.join('.', Path)
309
310 return Path
311
312 ## CleanString
313 #
314 # Remove comments in a string
315 # Remove spaces
316 #
317 # @param Line: The string to be cleaned
318 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
319 #
320 # @retval Path Formatted path
321 #
322 def CleanString(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False, BuildOption=False):
323 #
324 # remove whitespace
325 #
326 Line = Line.strip();
327 #
328 # Replace Edk's comment character
329 #
330 if AllowCppStyleComment:
331 Line = Line.replace(DataType.TAB_COMMENT_EDK_SPLIT, CommentCharacter)
332 #
333 # remove comments, but we should escape comment character in string
334 #
335 InString = False
336 CommentInString = False
337 for Index in range(0, len(Line)):
338 if Line[Index] == '"':
339 InString = not InString
340 elif Line[Index] == CommentCharacter and InString :
341 CommentInString = True
342 elif Line[Index] == CommentCharacter and not InString :
343 Line = Line[0: Index]
344 break
345
346 if CommentInString and BuildOption:
347 Line = Line.replace('"', '')
348 ChIndex = Line.find('#')
349 while ChIndex >= 0:
350 if GlobalData.gIsWindows:
351 if ChIndex == 0 or Line[ChIndex - 1] != '^':
352 Line = Line[0:ChIndex] + '^' + Line[ChIndex:]
353 ChIndex = Line.find('#', ChIndex + 2)
354 else:
355 ChIndex = Line.find('#', ChIndex + 1)
356 else:
357 if ChIndex == 0 or Line[ChIndex - 1] != '\\':
358 Line = Line[0:ChIndex] + '\\' + Line[ChIndex:]
359 ChIndex = Line.find('#', ChIndex + 2)
360 else:
361 ChIndex = Line.find('#', ChIndex + 1)
362 #
363 # remove whitespace again
364 #
365 Line = Line.strip();
366
367 return Line
368
369 ## CleanString2
370 #
371 # Split statement with comments in a string
372 # Remove spaces
373 #
374 # @param Line: The string to be cleaned
375 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
376 #
377 # @retval Path Formatted path
378 #
379 def CleanString2(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False):
380 #
381 # remove whitespace
382 #
383 Line = Line.strip();
384 #
385 # Replace Edk's comment character
386 #
387 if AllowCppStyleComment:
388 Line = Line.replace(DataType.TAB_COMMENT_EDK_SPLIT, CommentCharacter)
389 #
390 # separate comments and statements, but we should escape comment character in string
391 #
392 InString = False
393 CommentInString = False
394 Comment = ''
395 for Index in range(0, len(Line)):
396 if Line[Index] == '"':
397 InString = not InString
398 elif Line[Index] == CommentCharacter and InString:
399 CommentInString = True
400 elif Line[Index] == CommentCharacter and not InString:
401 Comment = Line[Index:].strip()
402 Line = Line[0:Index].strip()
403 break
404
405 return Line, Comment
406
407 ## GetMultipleValuesOfKeyFromLines
408 #
409 # Parse multiple strings to clean comment and spaces
410 # The result is saved to KeyValues
411 #
412 # @param Lines: The content to be parsed
413 # @param Key: Reserved
414 # @param KeyValues: To store data after parsing
415 # @param CommentCharacter: Comment char, used to ignore comment content
416 #
417 # @retval True Successfully executed
418 #
419 def GetMultipleValuesOfKeyFromLines(Lines, Key, KeyValues, CommentCharacter):
420 Lines = Lines.split(DataType.TAB_SECTION_END, 1)[1]
421 LineList = Lines.split('\n')
422 for Line in LineList:
423 Line = CleanString(Line, CommentCharacter)
424 if Line != '' and Line[0] != CommentCharacter:
425 KeyValues += [Line]
426
427 return True
428
429 ## GetDefineValue
430 #
431 # Parse a DEFINE statement to get defined value
432 # DEFINE Key Value
433 #
434 # @param String: The content to be parsed
435 # @param Key: The key of DEFINE statement
436 # @param CommentCharacter: Comment char, used to ignore comment content
437 #
438 # @retval string The defined value
439 #
440 def GetDefineValue(String, Key, CommentCharacter):
441 String = CleanString(String)
442 return String[String.find(Key + ' ') + len(Key + ' ') : ]
443
444 ## GetHexVerValue
445 #
446 # Get a Hex Version Value
447 #
448 # @param VerString: The version string to be parsed
449 #
450 #
451 # @retval: If VerString is incorrectly formatted, return "None" which will break the build.
452 # If VerString is correctly formatted, return a Hex value of the Version Number (0xmmmmnnnn)
453 # where mmmm is the major number and nnnn is the adjusted minor number.
454 #
455 def GetHexVerValue(VerString):
456 VerString = CleanString(VerString)
457
458 if gHumanReadableVerPatt.match(VerString):
459 ValueList = VerString.split('.')
460 Major = ValueList[0]
461 Minor = ValueList[1]
462 if len(Minor) == 1:
463 Minor += '0'
464 DeciValue = (int(Major) << 16) + int(Minor);
465 return "0x%08x" % DeciValue
466 elif gHexVerPatt.match(VerString):
467 return VerString
468 else:
469 return None
470
471
472 ## GetSingleValueOfKeyFromLines
473 #
474 # Parse multiple strings as below to get value of each definition line
475 # Key1 = Value1
476 # Key2 = Value2
477 # The result is saved to Dictionary
478 #
479 # @param Lines: The content to be parsed
480 # @param Dictionary: To store data after parsing
481 # @param CommentCharacter: Comment char, be used to ignore comment content
482 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
483 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
484 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
485 #
486 # @retval True Successfully executed
487 #
488 def GetSingleValueOfKeyFromLines(Lines, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
489 Lines = Lines.split('\n')
490 Keys = []
491 Value = ''
492 DefineValues = ['']
493 SpecValues = ['']
494
495 for Line in Lines:
496 #
497 # Handle DEFINE and SPEC
498 #
499 if Line.find(DataType.TAB_INF_DEFINES_DEFINE + ' ') > -1:
500 if '' in DefineValues:
501 DefineValues.remove('')
502 DefineValues.append(GetDefineValue(Line, DataType.TAB_INF_DEFINES_DEFINE, CommentCharacter))
503 continue
504 if Line.find(DataType.TAB_INF_DEFINES_SPEC + ' ') > -1:
505 if '' in SpecValues:
506 SpecValues.remove('')
507 SpecValues.append(GetDefineValue(Line, DataType.TAB_INF_DEFINES_SPEC, CommentCharacter))
508 continue
509
510 #
511 # Handle Others
512 #
513 LineList = Line.split(KeySplitCharacter, 1)
514 if len(LineList) >= 2:
515 Key = LineList[0].split()
516 if len(Key) == 1 and Key[0][0] != CommentCharacter:
517 #
518 # Remove comments and white spaces
519 #
520 LineList[1] = CleanString(LineList[1], CommentCharacter)
521 if ValueSplitFlag:
522 Value = map(string.strip, LineList[1].split(ValueSplitCharacter))
523 else:
524 Value = CleanString(LineList[1], CommentCharacter).splitlines()
525
526 if Key[0] in Dictionary:
527 if Key[0] not in Keys:
528 Dictionary[Key[0]] = Value
529 Keys.append(Key[0])
530 else:
531 Dictionary[Key[0]].extend(Value)
532 else:
533 Dictionary[DataType.TAB_INF_DEFINES_MACRO][Key[0]] = Value[0]
534
535 if DefineValues == []:
536 DefineValues = ['']
537 if SpecValues == []:
538 SpecValues = ['']
539 Dictionary[DataType.TAB_INF_DEFINES_DEFINE] = DefineValues
540 Dictionary[DataType.TAB_INF_DEFINES_SPEC] = SpecValues
541
542 return True
543
544 ## The content to be parsed
545 #
546 # Do pre-check for a file before it is parsed
547 # Check $()
548 # Check []
549 #
550 # @param FileName: Used for error report
551 # @param FileContent: File content to be parsed
552 # @param SupSectionTag: Used for error report
553 #
554 def PreCheck(FileName, FileContent, SupSectionTag):
555 LineNo = 0
556 IsFailed = False
557 NewFileContent = ''
558 for Line in FileContent.splitlines():
559 LineNo = LineNo + 1
560 #
561 # Clean current line
562 #
563 Line = CleanString(Line)
564
565 #
566 # Remove commented line
567 #
568 if Line.find(DataType.TAB_COMMA_SPLIT) == 0:
569 Line = ''
570 #
571 # Check $()
572 #
573 if Line.find('$') > -1:
574 if Line.find('$(') < 0 or Line.find(')') < 0:
575 EdkLogger.error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError=EdkLogger.IsRaiseError)
576
577 #
578 # Check []
579 #
580 if Line.find('[') > -1 or Line.find(']') > -1:
581 #
582 # Only get one '[' or one ']'
583 #
584 if not (Line.find('[') > -1 and Line.find(']') > -1):
585 EdkLogger.error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError=EdkLogger.IsRaiseError)
586
587 #
588 # Regenerate FileContent
589 #
590 NewFileContent = NewFileContent + Line + '\r\n'
591
592 if IsFailed:
593 EdkLogger.error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError=EdkLogger.IsRaiseError)
594
595 return NewFileContent
596
597 ## CheckFileType
598 #
599 # Check if the Filename is including ExtName
600 # Return True if it exists
601 # Raise a error message if it not exists
602 #
603 # @param CheckFilename: Name of the file to be checked
604 # @param ExtName: Ext name of the file to be checked
605 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
606 # @param SectionName: Used for error report
607 # @param Line: The line in container file which defines the file to be checked
608 #
609 # @retval True The file type is correct
610 #
611 def CheckFileType(CheckFilename, ExtName, ContainerFilename, SectionName, Line, LineNo= -1):
612 if CheckFilename != '' and CheckFilename != None:
613 (Root, Ext) = os.path.splitext(CheckFilename)
614 if Ext.upper() != ExtName.upper():
615 ContainerFile = open(ContainerFilename, 'r').read()
616 if LineNo == -1:
617 LineNo = GetLineNo(ContainerFile, Line)
618 ErrorMsg = "Invalid %s. '%s' is found, but '%s' file is needed" % (SectionName, CheckFilename, ExtName)
619 EdkLogger.error("Parser", PARSER_ERROR, ErrorMsg, Line=LineNo,
620 File=ContainerFilename, RaiseError=EdkLogger.IsRaiseError)
621
622 return True
623
624 ## CheckFileExist
625 #
626 # Check if the file exists
627 # Return True if it exists
628 # Raise a error message if it not exists
629 #
630 # @param CheckFilename: Name of the file to be checked
631 # @param WorkspaceDir: Current workspace dir
632 # @param ContainerFilename: The container file which describes the file to be checked, used for error report
633 # @param SectionName: Used for error report
634 # @param Line: The line in container file which defines the file to be checked
635 #
636 # @retval The file full path if the file exists
637 #
638 def CheckFileExist(WorkspaceDir, CheckFilename, ContainerFilename, SectionName, Line, LineNo= -1):
639 CheckFile = ''
640 if CheckFilename != '' and CheckFilename != None:
641 CheckFile = WorkspaceFile(WorkspaceDir, CheckFilename)
642 if not os.path.isfile(CheckFile):
643 ContainerFile = open(ContainerFilename, 'r').read()
644 if LineNo == -1:
645 LineNo = GetLineNo(ContainerFile, Line)
646 ErrorMsg = "Can't find file '%s' defined in section '%s'" % (CheckFile, SectionName)
647 EdkLogger.error("Parser", PARSER_ERROR, ErrorMsg,
648 File=ContainerFilename, Line=LineNo, RaiseError=EdkLogger.IsRaiseError)
649
650 return CheckFile
651
652 ## GetLineNo
653 #
654 # Find the index of a line in a file
655 #
656 # @param FileContent: Search scope
657 # @param Line: Search key
658 #
659 # @retval int Index of the line
660 # @retval -1 The line is not found
661 #
662 def GetLineNo(FileContent, Line, IsIgnoreComment=True):
663 LineList = FileContent.splitlines()
664 for Index in range(len(LineList)):
665 if LineList[Index].find(Line) > -1:
666 #
667 # Ignore statement in comment
668 #
669 if IsIgnoreComment:
670 if LineList[Index].strip()[0] == DataType.TAB_COMMENT_SPLIT:
671 continue
672 return Index + 1
673
674 return -1
675
676 ## RaiseParserError
677 #
678 # Raise a parser error
679 #
680 # @param Line: String which has error
681 # @param Section: Used for error report
682 # @param File: File which has the string
683 # @param Format: Correct format
684 #
685 def RaiseParserError(Line, Section, File, Format='', LineNo= -1):
686 if LineNo == -1:
687 LineNo = GetLineNo(open(os.path.normpath(File), 'r').read(), Line)
688 ErrorMsg = "Invalid statement '%s' is found in section '%s'" % (Line, Section)
689 if Format != '':
690 Format = "Correct format is " + Format
691 EdkLogger.error("Parser", PARSER_ERROR, ErrorMsg, File=File, Line=LineNo, ExtraData=Format, RaiseError=EdkLogger.IsRaiseError)
692
693 ## WorkspaceFile
694 #
695 # Return a full path with workspace dir
696 #
697 # @param WorkspaceDir: Workspace dir
698 # @param Filename: Relative file name
699 #
700 # @retval string A full path
701 #
702 def WorkspaceFile(WorkspaceDir, Filename):
703 return os.path.join(NormPath(WorkspaceDir), NormPath(Filename))
704
705 ## Split string
706 #
707 # Revmove '"' which startswith and endswith string
708 #
709 # @param String: The string need to be splited
710 #
711 # @retval String: The string after removed '""'
712 #
713 def SplitString(String):
714 if String.startswith('\"'):
715 String = String[1:]
716 if String.endswith('\"'):
717 String = String[:-1]
718
719 return String
720
721 ## Convert To Sql String
722 #
723 # 1. Replace "'" with "''" in each item of StringList
724 #
725 # @param StringList: A list for strings to be converted
726 #
727 def ConvertToSqlString(StringList):
728 return map(lambda s: s.replace("'", "''") , StringList)
729
730 ## Convert To Sql String
731 #
732 # 1. Replace "'" with "''" in the String
733 #
734 # @param String: A String to be converted
735 #
736 def ConvertToSqlString2(String):
737 return String.replace("'", "''")
738
739 #
740 # Remove comment block
741 #
742 def RemoveBlockComment(Lines):
743 IsFindBlockComment = False
744 IsFindBlockCode = False
745 ReservedLine = ''
746 NewLines = []
747
748 for Line in Lines:
749 Line = Line.strip()
750 #
751 # Remove comment block
752 #
753 if Line.find(DataType.TAB_COMMENT_EDK_START) > -1:
754 ReservedLine = GetSplitList(Line, DataType.TAB_COMMENT_EDK_START, 1)[0]
755 IsFindBlockComment = True
756 if Line.find(DataType.TAB_COMMENT_EDK_END) > -1:
757 Line = ReservedLine + GetSplitList(Line, DataType.TAB_COMMENT_EDK_END, 1)[1]
758 ReservedLine = ''
759 IsFindBlockComment = False
760 if IsFindBlockComment:
761 NewLines.append('')
762 continue
763
764 NewLines.append(Line)
765 return NewLines
766
767 #
768 # Get String of a List
769 #
770 def GetStringOfList(List, Split=' '):
771 if type(List) != type([]):
772 return List
773 Str = ''
774 for Item in List:
775 Str = Str + Item + Split
776
777 return Str.strip()
778
779 #
780 # Get HelpTextList from HelpTextClassList
781 #
782 def GetHelpTextList(HelpTextClassList):
783 List = []
784 if HelpTextClassList:
785 for HelpText in HelpTextClassList:
786 if HelpText.String.endswith('\n'):
787 HelpText.String = HelpText.String[0: len(HelpText.String) - len('\n')]
788 List.extend(HelpText.String.split('\n'))
789
790 return List
791
792 def StringToArray(String):
793 if isinstance(String, unicode):
794 if len(unicode) == 0:
795 return "{0x00, 0x00}"
796 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String])
797 elif String.startswith('L"'):
798 if String == "L\"\"":
799 return "{0x00, 0x00}"
800 else:
801 return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String[2:-1]])
802 elif String.startswith('"'):
803 if String == "\"\"":
804 return "{0x00,0x00}"
805 else:
806 StringLen = len(String[1:-1])
807 if StringLen % 2:
808 return "{%s, 0x00}" % ", ".join(["0x%02x" % ord(C) for C in String[1:-1]])
809 else:
810 return "{%s, 0x00,0x00}" % ", ".join(["0x%02x" % ord(C) for C in String[1:-1]])
811 elif String.startswith('{'):
812 StringLen = len(String.split(","))
813 if StringLen % 2:
814 return "{%s, 0x00}" % ", ".join([ C for C in String[1:-1].split(',')])
815 else:
816 return "{%s}" % ", ".join([ C for C in String[1:-1].split(',')])
817
818 else:
819 if len(String.split()) % 2:
820 return '{%s, 0}' % ', '.join(String.split())
821 else:
822 return '{%s, 0,0}' % ', '.join(String.split())
823
824 def StringArrayLength(String):
825 if isinstance(String, unicode):
826 return (len(String) + 1) * 2 + 1;
827 elif String.startswith('L"'):
828 return (len(String) - 3 + 1) * 2
829 elif String.startswith('"'):
830 return (len(String) - 2 + 1)
831 else:
832 return len(String.split()) + 1
833
834 def RemoveDupOption(OptionString, Which="/I", Against=None):
835 OptionList = OptionString.split()
836 ValueList = []
837 if Against:
838 ValueList += Against
839 for Index in range(len(OptionList)):
840 Opt = OptionList[Index]
841 if not Opt.startswith(Which):
842 continue
843 if len(Opt) > len(Which):
844 Val = Opt[len(Which):]
845 else:
846 Val = ""
847 if Val in ValueList:
848 OptionList[Index] = ""
849 else:
850 ValueList.append(Val)
851 return " ".join(OptionList)
852
853 ##
854 #
855 # This acts like the main() function for the script, unless it is 'import'ed into another
856 # script.
857 #
858 if __name__ == '__main__':
859 pass
860