]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/StringUtils.py
BaseTools: Use absolute import in Common
[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 from __future__ import absolute_import
18 import re
19 from . import DataType
20 import Common.LongFilePathOs as os
21 import string
22 from . import EdkLogger as EdkLogger
23
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
29
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}$')
32
33 ## GetSplitValueList
34 #
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']
38 #
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
42 #
43 # @retval list() A list for splitted string
44 #
45 def GetSplitValueList(String, SplitTag=DataType.TAB_VALUE_SPLIT, MaxSplit= -1):
46 ValueList = []
47 Last = 0
48 Escaped = False
49 InSingleQuoteString = False
50 InDoubleQuoteString = False
51 InParenthesis = 0
52 for Index in range(0, len(String)):
53 Char = String[Index]
54
55 if not Escaped:
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())
59 Last = Index + 1
60 if MaxSplit > 0 and len(ValueList) >= MaxSplit:
61 break
62
63 if Char == '\\' and (InSingleQuoteString or InDoubleQuoteString):
64 Escaped = True
65 elif Char == '"' and not InSingleQuoteString:
66 if not InDoubleQuoteString:
67 InDoubleQuoteString = True
68 else:
69 InDoubleQuoteString = False
70 elif Char == "'" and not InDoubleQuoteString:
71 if not InSingleQuoteString:
72 InSingleQuoteString = True
73 else:
74 InSingleQuoteString = False
75 elif Char == '(':
76 InParenthesis = InParenthesis + 1
77 elif Char == ')':
78 InParenthesis = InParenthesis - 1
79 else:
80 Escaped = False
81
82 if Last < len(String):
83 ValueList.append(String[Last:].strip())
84 elif Last == len(String):
85 ValueList.append('')
86
87 return ValueList
88
89 ## GetSplitList
90 #
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']
94 #
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
98 #
99 # @retval list() A list for splitted string
100 #
101 def GetSplitList(String, SplitStr=DataType.TAB_VALUE_SPLIT, MaxSplit= -1):
102 return map(lambda l: l.strip(), String.split(SplitStr, MaxSplit))
103
104 ## MergeArches
105 #
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
108 #
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
112 #
113 def MergeArches(Dict, Key, Arch):
114 if Key in Dict:
115 Dict[Key].append(Arch)
116 else:
117 Dict[Key] = Arch.split()
118
119 ## GenDefines
120 #
121 # Parse a string with format "DEFINE <VarName> = <PATH>"
122 # Generate a map Defines[VarName] = PATH
123 # Return False if invalid format
124 #
125 # @param String: String with DEFINE statement
126 # @param Arch: Supportted Arch
127 # @param Defines: DEFINE statement to be parsed
128 #
129 # @retval 0 DEFINE statement found, and valid
130 # @retval 1 DEFINE statement found, but not valid
131 # @retval -1 DEFINE statement not found
132 #
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)
136 if len(List) == 2:
137 Defines[(CleanString(List[0]), Arch)] = CleanString(List[1])
138 return 0
139 else:
140 return -1
141
142 return 1
143
144 ## GenInclude
145 #
146 # Parse a string with format "!include <Filename>"
147 # Return the file path
148 # Return False if invalid format or NOT FOUND
149 #
150 # @param String: String with INCLUDE statement
151 # @param IncludeFiles: INCLUDE statement to be parsed
152 # @param Arch: Supportted Arch
153 #
154 # @retval True
155 # @retval False
156 #
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)
161 return True
162 else:
163 return False
164
165 ## GetLibraryClassesWithModuleType
166 #
167 # Get Library Class definition when no module type defined
168 #
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
173 #
174 # @retval True Get library classes successfully
175 #
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]])
184
185 return True
186
187 ## GetDynamics
188 #
189 # Get Dynamic Pcds
190 #
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
195 #
196 # @retval True Get Dynamic Pcds successfully
197 #
198 def GetDynamics(Lines, Key, KeyValues, CommentCharacter):
199 #
200 # Get SkuId Name List
201 #
202 SkuIdNameList = SplitModuleType(Key)
203
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]])
210
211 return True
212
213 ## SplitModuleType
214 #
215 # Split ModuleType out of section defien to get key
216 # [LibraryClass.Arch.ModuleType|ModuleType|ModuleType] -> [ 'LibraryClass.Arch', ['ModuleType', 'ModuleType', 'ModuleType'] ]
217 #
218 # @param Key: String to be parsed
219 #
220 # @retval ReturnValue A list for module types
221 #
222 def SplitModuleType(Key):
223 KeyList = Key.split(DataType.TAB_SPLIT)
224 #
225 # Fill in for arch
226 #
227 KeyList.append('')
228 #
229 # Fill in for moduletype
230 #
231 KeyList.append('')
232 ReturnValue = []
233 KeyValue = KeyList[0]
234 if KeyList[1] != '':
235 KeyValue = KeyValue + DataType.TAB_SPLIT + KeyList[1]
236 ReturnValue.append(KeyValue)
237 ReturnValue.append(GetSplitValueList(KeyList[2]))
238
239 return ReturnValue
240
241 ## Replace macro in strings list
242 #
243 # This method replace macros used in a given string list. The macros are
244 # given in a dictionary.
245 #
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 ''
249 #
250 # @retval NewList A new string list whose macros are replaced
251 #
252 def ReplaceMacros(StringList, MacroDefinitions={}, SelfReplacement=False):
253 NewList = []
254 for String in StringList:
255 if isinstance(String, type('')):
256 NewList.append(ReplaceMacro(String, MacroDefinitions, SelfReplacement))
257 else:
258 NewList.append(String)
259
260 return NewList
261
262 ## Replace macro in string
263 #
264 # This method replace macros used in given string. The macros are given in a
265 # dictionary.
266 #
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 ''
270 #
271 # @retval string The string whose macros are replaced
272 #
273 def ReplaceMacro(String, MacroDefinitions={}, SelfReplacement=False, RaiseError=False):
274 LastString = String
275 while String and MacroDefinitions:
276 MacroUsed = GlobalData.gMacroRefPattern.findall(String)
277 # no macro found in String, stop replacing
278 if len(MacroUsed) == 0:
279 break
280
281 for Macro in MacroUsed:
282 if Macro not in MacroDefinitions:
283 if RaiseError:
284 raise SymbolNotFound("%s not defined" % Macro)
285 if SelfReplacement:
286 String = String.replace("$(%s)" % Macro, '')
287 continue
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:
292 break
293 LastString = String
294
295 return String
296
297 ## NormPath
298 #
299 # Create a normal path
300 # And replace DFEINE in the path
301 #
302 # @param Path: The input value for Path to be converted
303 # @param Defines: A set for DEFINE statement
304 #
305 # @retval Path Formatted path
306 #
307 def NormPath(Path, Defines={}):
308 IsRelativePath = False
309 if Path:
310 if Path[0] == '.':
311 IsRelativePath = True
312 #
313 # Replace with Define
314 #
315 if Defines:
316 Path = ReplaceMacro(Path, Defines)
317 #
318 # To local path format
319 #
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:
324 Path = Path[1:]
325 Path = mws.join(GlobalData.gWorkspace, Path)
326
327 if IsRelativePath and Path[0] != '.':
328 Path = os.path.join('.', Path)
329
330 return Path
331
332 ## CleanString
333 #
334 # Remove comments in a string
335 # Remove spaces
336 #
337 # @param Line: The string to be cleaned
338 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
339 #
340 # @retval Path Formatted path
341 #
342 def CleanString(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False, BuildOption=False):
343 #
344 # remove whitespace
345 #
346 Line = Line.strip();
347 #
348 # Replace Edk's comment character
349 #
350 if AllowCppStyleComment:
351 Line = Line.replace(DataType.TAB_COMMENT_EDK_SPLIT, CommentCharacter)
352 #
353 # remove comments, but we should escape comment character in string
354 #
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]
367 break
368
369 if CommentInString and BuildOption:
370 Line = Line.replace('"', '')
371 ChIndex = Line.find('#')
372 while ChIndex >= 0:
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)
377 else:
378 ChIndex = Line.find('#', ChIndex + 1)
379 else:
380 if ChIndex == 0 or Line[ChIndex - 1] != '\\':
381 Line = Line[0:ChIndex] + '\\' + Line[ChIndex:]
382 ChIndex = Line.find('#', ChIndex + 2)
383 else:
384 ChIndex = Line.find('#', ChIndex + 1)
385 #
386 # remove whitespace again
387 #
388 Line = Line.strip();
389
390 return Line
391
392 ## CleanString2
393 #
394 # Split statement with comments in a string
395 # Remove spaces
396 #
397 # @param Line: The string to be cleaned
398 # @param CommentCharacter: Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT
399 #
400 # @retval Path Formatted path
401 #
402 def CleanString2(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False):
403 #
404 # remove whitespace
405 #
406 Line = Line.strip();
407 #
408 # Replace Edk's comment character
409 #
410 if AllowCppStyleComment:
411 Line = Line.replace(DataType.TAB_COMMENT_EDK_SPLIT, CommentCharacter)
412 #
413 # separate comments and statements, but we should escape comment character in string
414 #
415 InDoubleQuoteString = False
416 InSingleQuoteString = False
417 CommentInString = False
418 Comment = ''
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()
429 break
430
431 return Line, Comment
432
433 ## GetMultipleValuesOfKeyFromLines
434 #
435 # Parse multiple strings to clean comment and spaces
436 # The result is saved to KeyValues
437 #
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
442 #
443 # @retval True Successfully executed
444 #
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)
452
453 return True
454
455 ## GetDefineValue
456 #
457 # Parse a DEFINE statement to get defined value
458 # DEFINE Key Value
459 #
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
463 #
464 # @retval string The defined value
465 #
466 def GetDefineValue(String, Key, CommentCharacter):
467 String = CleanString(String)
468 return String[String.find(Key + ' ') + len(Key + ' ') : ]
469
470 ## GetHexVerValue
471 #
472 # Get a Hex Version Value
473 #
474 # @param VerString: The version string to be parsed
475 #
476 #
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.
480 #
481 def GetHexVerValue(VerString):
482 VerString = CleanString(VerString)
483
484 if gHumanReadableVerPatt.match(VerString):
485 ValueList = VerString.split('.')
486 Major = ValueList[0]
487 Minor = ValueList[1]
488 if len(Minor) == 1:
489 Minor += '0'
490 DeciValue = (int(Major) << 16) + int(Minor);
491 return "0x%08x" % DeciValue
492 elif gHexVerPatt.match(VerString):
493 return VerString
494 else:
495 return None
496
497
498 ## GetSingleValueOfKeyFromLines
499 #
500 # Parse multiple strings as below to get value of each definition line
501 # Key1 = Value1
502 # Key2 = Value2
503 # The result is saved to Dictionary
504 #
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
511 #
512 # @retval True Successfully executed
513 #
514 def GetSingleValueOfKeyFromLines(Lines, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
515 Lines = Lines.split('\n')
516 Keys = []
517 Value = ''
518 DefineValues = ['']
519 SpecValues = ['']
520
521 for Line in Lines:
522 #
523 # Handle DEFINE and SPEC
524 #
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))
529 continue
530 if Line.find(DataType.TAB_INF_DEFINES_SPEC + ' ') > -1:
531 if '' in SpecValues:
532 SpecValues.remove('')
533 SpecValues.append(GetDefineValue(Line, DataType.TAB_INF_DEFINES_SPEC, CommentCharacter))
534 continue
535
536 #
537 # Handle Others
538 #
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:
543 #
544 # Remove comments and white spaces
545 #
546 LineList[1] = CleanString(LineList[1], CommentCharacter)
547 if ValueSplitFlag:
548 Value = map(string.strip, LineList[1].split(ValueSplitCharacter))
549 else:
550 Value = CleanString(LineList[1], CommentCharacter).splitlines()
551
552 if Key[0] in Dictionary:
553 if Key[0] not in Keys:
554 Dictionary[Key[0]] = Value
555 Keys.append(Key[0])
556 else:
557 Dictionary[Key[0]].extend(Value)
558 else:
559 Dictionary[DataType.TAB_INF_DEFINES_MACRO][Key[0]] = Value[0]
560
561 if DefineValues == []:
562 DefineValues = ['']
563 if SpecValues == []:
564 SpecValues = ['']
565 Dictionary[DataType.TAB_INF_DEFINES_DEFINE] = DefineValues
566 Dictionary[DataType.TAB_INF_DEFINES_SPEC] = SpecValues
567
568 return True
569
570 ## The content to be parsed
571 #
572 # Do pre-check for a file before it is parsed
573 # Check $()
574 # Check []
575 #
576 # @param FileName: Used for error report
577 # @param FileContent: File content to be parsed
578 # @param SupSectionTag: Used for error report
579 #
580 def PreCheck(FileName, FileContent, SupSectionTag):
581 LineNo = 0
582 IsFailed = False
583 NewFileContent = ''
584 for Line in FileContent.splitlines():
585 LineNo = LineNo + 1
586 #
587 # Clean current line
588 #
589 Line = CleanString(Line)
590
591 #
592 # Remove commented line
593 #
594 if Line.find(DataType.TAB_COMMA_SPLIT) == 0:
595 Line = ''
596 #
597 # Check $()
598 #
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)
602
603 #
604 # Check []
605 #
606 if Line.find('[') > -1 or Line.find(']') > -1:
607 #
608 # Only get one '[' or one ']'
609 #
610 if not (Line.find('[') > -1 and Line.find(']') > -1):
611 EdkLogger.error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError=EdkLogger.IsRaiseError)
612
613 #
614 # Regenerate FileContent
615 #
616 NewFileContent = NewFileContent + Line + '\r\n'
617
618 if IsFailed:
619 EdkLogger.error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError=EdkLogger.IsRaiseError)
620
621 return NewFileContent
622
623 ## CheckFileType
624 #
625 # Check if the Filename is including ExtName
626 # Return True if it exists
627 # Raise a error message if it not exists
628 #
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
634 #
635 # @retval True The file type is correct
636 #
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()
642 if LineNo == -1:
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)
647
648 return True
649
650 ## CheckFileExist
651 #
652 # Check if the file exists
653 # Return True if it exists
654 # Raise a error message if it not exists
655 #
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
661 #
662 # @retval The file full path if the file exists
663 #
664 def CheckFileExist(WorkspaceDir, CheckFilename, ContainerFilename, SectionName, Line, LineNo= -1):
665 CheckFile = ''
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()
670 if LineNo == -1:
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)
675
676 return CheckFile
677
678 ## GetLineNo
679 #
680 # Find the index of a line in a file
681 #
682 # @param FileContent: Search scope
683 # @param Line: Search key
684 #
685 # @retval int Index of the line
686 # @retval -1 The line is not found
687 #
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:
692 #
693 # Ignore statement in comment
694 #
695 if IsIgnoreComment:
696 if LineList[Index].strip()[0] == DataType.TAB_COMMENT_SPLIT:
697 continue
698 return Index + 1
699
700 return -1
701
702 ## RaiseParserError
703 #
704 # Raise a parser error
705 #
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
710 #
711 def RaiseParserError(Line, Section, File, Format='', LineNo= -1):
712 if 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)
715 if Format != '':
716 Format = "Correct format is " + Format
717 EdkLogger.error("Parser", PARSER_ERROR, ErrorMsg, File=File, Line=LineNo, ExtraData=Format, RaiseError=EdkLogger.IsRaiseError)
718
719 ## WorkspaceFile
720 #
721 # Return a full path with workspace dir
722 #
723 # @param WorkspaceDir: Workspace dir
724 # @param Filename: Relative file name
725 #
726 # @retval string A full path
727 #
728 def WorkspaceFile(WorkspaceDir, Filename):
729 return mws.join(NormPath(WorkspaceDir), NormPath(Filename))
730
731 ## Split string
732 #
733 # Revmove '"' which startswith and endswith string
734 #
735 # @param String: The string need to be splited
736 #
737 # @retval String: The string after removed '""'
738 #
739 def SplitString(String):
740 if String.startswith('\"'):
741 String = String[1:]
742 if String.endswith('\"'):
743 String = String[:-1]
744
745 return String
746
747 ## Convert To Sql String
748 #
749 # 1. Replace "'" with "''" in each item of StringList
750 #
751 # @param StringList: A list for strings to be converted
752 #
753 def ConvertToSqlString(StringList):
754 return map(lambda s: s.replace("'", "''"), StringList)
755
756 ## Convert To Sql String
757 #
758 # 1. Replace "'" with "''" in the String
759 #
760 # @param String: A String to be converted
761 #
762 def ConvertToSqlString2(String):
763 return String.replace("'", "''")
764
765 #
766 # Remove comment block
767 #
768 def RemoveBlockComment(Lines):
769 IsFindBlockComment = False
770 IsFindBlockCode = False
771 ReservedLine = ''
772 NewLines = []
773
774 for Line in Lines:
775 Line = Line.strip()
776 #
777 # Remove comment block
778 #
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]
784 ReservedLine = ''
785 IsFindBlockComment = False
786 if IsFindBlockComment:
787 NewLines.append('')
788 continue
789
790 NewLines.append(Line)
791 return NewLines
792
793 #
794 # Get String of a List
795 #
796 def GetStringOfList(List, Split=' '):
797 if not isinstance(List, type([])):
798 return List
799 Str = ''
800 for Item in List:
801 Str = Str + Item + Split
802
803 return Str.strip()
804
805 #
806 # Get HelpTextList from HelpTextClassList
807 #
808 def GetHelpTextList(HelpTextClassList):
809 List = []
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'))
815
816 return List
817
818 def StringToArray(String):
819 if isinstance(String, unicode):
820 if len(unicode) == 0:
821 return "{0x00,0x00}"
822 return "{%s,0x00,0x00}" % ",".join("0x%02x,0x00" % ord(C) for C in String)
823 elif String.startswith('L"'):
824 if String == "L\"\"":
825 return "{0x00,0x00}"
826 else:
827 return "{%s,0x00,0x00}" % ",".join("0x%02x,0x00" % ord(C) for C in String[2:-1])
828 elif String.startswith('"'):
829 if String == "\"\"":
830 return "{0x00,0x00}"
831 else:
832 StringLen = len(String[1:-1])
833 if StringLen % 2:
834 return "{%s,0x00}" % ",".join("0x%02x" % ord(C) for C in String[1:-1])
835 else:
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(","))
839 if StringLen % 2:
840 return "{%s,0x00}" % ",".join(C.strip() for C in String[1:-1].split(','))
841 else:
842 return "{%s}" % ",".join(C.strip() for C in String[1:-1].split(','))
843
844 else:
845 if len(String.split()) % 2:
846 return '{%s,0}' % ','.join(String.split())
847 else:
848 return '{%s,0,0}' % ','.join(String.split())
849
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)
857 else:
858 return len(String.split()) + 1
859
860 def RemoveDupOption(OptionString, Which="/I", Against=None):
861 OptionList = OptionString.split()
862 ValueList = []
863 if Against:
864 ValueList += Against
865 for Index in range(len(OptionList)):
866 Opt = OptionList[Index]
867 if not Opt.startswith(Which):
868 continue
869 if len(Opt) > len(Which):
870 Val = Opt[len(Which):]
871 else:
872 Val = ""
873 if Val in ValueList:
874 OptionList[Index] = ""
875 else:
876 ValueList.append(Val)
877 return " ".join(OptionList)
878
879 ##
880 #
881 # This acts like the main() function for the script, unless it is 'import'ed into another
882 # script.
883 #
884 if __name__ == '__main__':
885 pass
886