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