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