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