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