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