-## @file\r
-# This file is used to define common string related functions used in parsing\r
-# process\r
-#\r
-# Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
-#\r
-# This program and the accompanying materials are licensed and made available\r
-# under the terms and conditions of the BSD License which accompanies this\r
-# distribution. The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-#\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-'''\r
-String\r
-'''\r
-##\r
-# Import Modules\r
-#\r
-import re\r
-import os.path\r
-from string import strip\r
-import Logger.Log as Logger\r
-import Library.DataType as DataType\r
-from Logger.ToolError import FORMAT_INVALID\r
-from Logger.ToolError import PARSER_ERROR\r
-from Logger import StringTable as ST\r
-\r
-#\r
-# Regular expression for matching macro used in DSC/DEC/INF file inclusion\r
-#\r
-gMACRO_PATTERN = re.compile("\$\(([_A-Z][_A-Z0-9]*)\)", re.UNICODE)\r
-\r
-## GetSplitValueList\r
-#\r
-# Get a value list from a string with multiple values splited with SplitTag\r
-# The default SplitTag is DataType.TAB_VALUE_SPLIT\r
-# 'AAA|BBB|CCC' -> ['AAA', 'BBB', 'CCC']\r
-#\r
-# @param String: The input string to be splitted\r
-# @param SplitTag: The split key, default is DataType.TAB_VALUE_SPLIT\r
-# @param MaxSplit: The max number of split values, default is -1\r
-#\r
-#\r
-def GetSplitValueList(String, SplitTag=DataType.TAB_VALUE_SPLIT, MaxSplit= -1):\r
- return map(lambda l: l.strip(), String.split(SplitTag, MaxSplit))\r
-\r
-## MergeArches\r
-#\r
-# Find a key's all arches in dict, add the new arch to the list\r
-# If not exist any arch, set the arch directly\r
-#\r
-# @param Dict: The input value for Dict\r
-# @param Key: The input value for Key\r
-# @param Arch: The Arch to be added or merged\r
-#\r
-def MergeArches(Dict, Key, Arch):\r
- if Key in Dict.keys():\r
- Dict[Key].append(Arch)\r
- else:\r
- Dict[Key] = Arch.split()\r
-\r
-## GenDefines\r
-#\r
-# Parse a string with format "DEFINE <VarName> = <PATH>"\r
-# Generate a map Defines[VarName] = PATH\r
-# Return False if invalid format\r
-#\r
-# @param String: String with DEFINE statement\r
-# @param Arch: Supportted Arch\r
-# @param Defines: DEFINE statement to be parsed\r
-#\r
-def GenDefines(String, Arch, Defines):\r
- if String.find(DataType.TAB_DEFINE + ' ') > -1:\r
- List = String.replace(DataType.TAB_DEFINE + ' ', '').\\r
- split(DataType.TAB_EQUAL_SPLIT)\r
- if len(List) == 2:\r
- Defines[(CleanString(List[0]), Arch)] = CleanString(List[1])\r
- return 0\r
- else:\r
- return -1\r
- return 1\r
-\r
-## GetLibraryClassesWithModuleType\r
-#\r
-# Get Library Class definition when no module type defined\r
-#\r
-# @param Lines: The content to be parsed\r
-# @param Key: Reserved\r
-# @param KeyValues: To store data after parsing\r
-# @param CommentCharacter: Comment char, used to ignore comment content\r
-#\r
-def GetLibraryClassesWithModuleType(Lines, Key, KeyValues, CommentCharacter):\r
- NewKey = SplitModuleType(Key)\r
- Lines = Lines.split(DataType.TAB_SECTION_END, 1)[1]\r
- LineList = Lines.splitlines()\r
- for Line in LineList:\r
- Line = CleanString(Line, CommentCharacter)\r
- if Line != '' and Line[0] != CommentCharacter:\r
- KeyValues.append([CleanString(Line, CommentCharacter), NewKey[1]])\r
-\r
- return True\r
-\r
-## GetDynamics\r
-#\r
-# Get Dynamic Pcds\r
-#\r
-# @param Lines: The content to be parsed\r
-# @param Key: Reserved\r
-# @param KeyValues: To store data after parsing\r
-# @param CommentCharacter: Comment char, used to ignore comment content\r
-#\r
-def GetDynamics(Lines, Key, KeyValues, CommentCharacter):\r
- #\r
- # Get SkuId Name List\r
- #\r
- SkuIdNameList = SplitModuleType(Key)\r
-\r
- Lines = Lines.split(DataType.TAB_SECTION_END, 1)[1]\r
- LineList = Lines.splitlines()\r
- for Line in LineList:\r
- Line = CleanString(Line, CommentCharacter)\r
- if Line != '' and Line[0] != CommentCharacter:\r
- KeyValues.append([CleanString(Line, CommentCharacter), SkuIdNameList[1]])\r
-\r
- return True\r
-\r
-## SplitModuleType\r
-#\r
-# Split ModuleType out of section defien to get key\r
-# [LibraryClass.Arch.ModuleType|ModuleType|ModuleType] -> [\r
-# 'LibraryClass.Arch', ['ModuleType', 'ModuleType', 'ModuleType'] ]\r
-#\r
-# @param Key: String to be parsed\r
-#\r
-def SplitModuleType(Key):\r
- KeyList = Key.split(DataType.TAB_SPLIT)\r
- #\r
- # Fill in for arch\r
- #\r
- KeyList.append('')\r
- #\r
- # Fill in for moduletype\r
- #\r
- KeyList.append('')\r
- ReturnValue = []\r
- KeyValue = KeyList[0]\r
- if KeyList[1] != '':\r
- KeyValue = KeyValue + DataType.TAB_SPLIT + KeyList[1]\r
- ReturnValue.append(KeyValue)\r
- ReturnValue.append(GetSplitValueList(KeyList[2]))\r
-\r
- return ReturnValue\r
-\r
-## Replace macro in string\r
-#\r
-# This method replace macros used in given string. The macros are given in a\r
-# dictionary.\r
-#\r
-# @param String String to be processed\r
-# @param MacroDefinitions The macro definitions in the form of dictionary\r
-# @param SelfReplacement To decide whether replace un-defined macro to ''\r
-# @param Line: The content contain line string and line number\r
-# @param FileName: The meta-file file name\r
-#\r
-def ReplaceMacro(String, MacroDefinitions=None, SelfReplacement=False, Line=None, FileName=None, Flag=False):\r
- LastString = String\r
- if MacroDefinitions is None:\r
- MacroDefinitions = {}\r
- while MacroDefinitions:\r
- QuotedStringList = []\r
- HaveQuotedMacroFlag = False\r
- if not Flag:\r
- MacroUsed = gMACRO_PATTERN.findall(String)\r
- else:\r
- ReQuotedString = re.compile('\"')\r
- QuotedStringList = ReQuotedString.split(String)\r
- if len(QuotedStringList) >= 3:\r
- HaveQuotedMacroFlag = True\r
- Count = 0\r
- MacroString = ""\r
- for QuotedStringItem in QuotedStringList:\r
- Count += 1\r
- if Count % 2 != 0:\r
- MacroString += QuotedStringItem\r
-\r
- if Count == len(QuotedStringList) and Count % 2 == 0:\r
- MacroString += QuotedStringItem\r
-\r
- MacroUsed = gMACRO_PATTERN.findall(MacroString)\r
- #\r
- # no macro found in String, stop replacing\r
- #\r
- if len(MacroUsed) == 0:\r
- break\r
- for Macro in MacroUsed:\r
- if Macro not in MacroDefinitions:\r
- if SelfReplacement:\r
- String = String.replace("$(%s)" % Macro, '')\r
- Logger.Debug(5, "Delete undefined MACROs in file %s line %d: %s!" % (FileName, Line[1], Line[0]))\r
- continue\r
- if not HaveQuotedMacroFlag:\r
- String = String.replace("$(%s)" % Macro, MacroDefinitions[Macro])\r
- else:\r
- Count = 0\r
- for QuotedStringItem in QuotedStringList:\r
- Count += 1\r
- if Count % 2 != 0:\r
- QuotedStringList[Count - 1] = QuotedStringList[Count - 1].replace("$(%s)" % Macro,\r
- MacroDefinitions[Macro])\r
- elif Count == len(QuotedStringList) and Count % 2 == 0:\r
- QuotedStringList[Count - 1] = QuotedStringList[Count - 1].replace("$(%s)" % Macro,\r
- MacroDefinitions[Macro])\r
-\r
- RetString = ''\r
- if HaveQuotedMacroFlag:\r
- Count = 0\r
- for QuotedStringItem in QuotedStringList:\r
- Count += 1\r
- if Count != len(QuotedStringList):\r
- RetString += QuotedStringList[Count - 1] + "\""\r
- else:\r
- RetString += QuotedStringList[Count - 1]\r
-\r
- String = RetString\r
-\r
- #\r
- # in case there's macro not defined\r
- #\r
- if String == LastString:\r
- break\r
- LastString = String\r
-\r
- return String\r
-\r
-## NormPath\r
-#\r
-# Create a normal path\r
-# And replace DFEINE in the path\r
-#\r
-# @param Path: The input value for Path to be converted\r
-# @param Defines: A set for DEFINE statement\r
-#\r
-def NormPath(Path, Defines=None):\r
- IsRelativePath = False\r
- if Defines is None:\r
- Defines = {}\r
- if Path:\r
- if Path[0] == '.':\r
- IsRelativePath = True\r
- #\r
- # Replace with Define\r
- #\r
- if Defines:\r
- Path = ReplaceMacro(Path, Defines)\r
- #\r
- # To local path format\r
- #\r
- Path = os.path.normpath(Path)\r
-\r
- if IsRelativePath and Path[0] != '.':\r
- Path = os.path.join('.', Path)\r
- return Path\r
-\r
-## CleanString\r
-#\r
-# Remove comments in a string\r
-# Remove spaces\r
-#\r
-# @param Line: The string to be cleaned\r
-# @param CommentCharacter: Comment char, used to ignore comment content,\r
-# default is DataType.TAB_COMMENT_SPLIT\r
-#\r
-def CleanString(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False):\r
- #\r
- # remove whitespace\r
- #\r
- Line = Line.strip()\r
- #\r
- # Replace EDK1's comment character\r
- #\r
- if AllowCppStyleComment:\r
- Line = Line.replace(DataType.TAB_COMMENT_EDK1_SPLIT, CommentCharacter)\r
- #\r
- # remove comments, but we should escape comment character in string\r
- #\r
- InString = False\r
- for Index in range(0, len(Line)):\r
- if Line[Index] == '"':\r
- InString = not InString\r
- elif Line[Index] == CommentCharacter and not InString:\r
- Line = Line[0: Index]\r
- break\r
- #\r
- # remove whitespace again\r
- #\r
- Line = Line.strip()\r
-\r
- return Line\r
-\r
-## CleanString2\r
-#\r
-# Split comments in a string\r
-# Remove spaces\r
-#\r
-# @param Line: The string to be cleaned\r
-# @param CommentCharacter: Comment char, used to ignore comment content,\r
-# default is DataType.TAB_COMMENT_SPLIT\r
-#\r
-def CleanString2(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False):\r
- #\r
- # remove whitespace\r
- #\r
- Line = Line.strip()\r
- #\r
- # Replace EDK1's comment character\r
- #\r
- if AllowCppStyleComment:\r
- Line = Line.replace(DataType.TAB_COMMENT_EDK1_SPLIT, CommentCharacter)\r
- #\r
- # separate comments and statements\r
- #\r
- LineParts = Line.split(CommentCharacter, 1)\r
- #\r
- # remove whitespace again\r
- #\r
- Line = LineParts[0].strip()\r
- if len(LineParts) > 1:\r
- Comment = LineParts[1].strip()\r
- #\r
- # Remove prefixed and trailing comment characters\r
- #\r
- Start = 0\r
- End = len(Comment)\r
- while Start < End and Comment.startswith(CommentCharacter, Start, End):\r
- Start += 1\r
- while End >= 0 and Comment.endswith(CommentCharacter, Start, End):\r
- End -= 1\r
- Comment = Comment[Start:End]\r
- Comment = Comment.strip()\r
- else:\r
- Comment = ''\r
-\r
- return Line, Comment\r
-\r
-## GetMultipleValuesOfKeyFromLines\r
-#\r
-# Parse multiple strings to clean comment and spaces\r
-# The result is saved to KeyValues\r
-#\r
-# @param Lines: The content to be parsed\r
-# @param Key: Reserved\r
-# @param KeyValues: To store data after parsing\r
-# @param CommentCharacter: Comment char, used to ignore comment content\r
-#\r
-def GetMultipleValuesOfKeyFromLines(Lines, Key, KeyValues, CommentCharacter):\r
- if Key:\r
- pass\r
- if KeyValues:\r
- pass\r
- Lines = Lines.split(DataType.TAB_SECTION_END, 1)[1]\r
- LineList = Lines.split('\n')\r
- for Line in LineList:\r
- Line = CleanString(Line, CommentCharacter)\r
- if Line != '' and Line[0] != CommentCharacter:\r
- KeyValues += [Line]\r
- return True\r
-\r
-## GetDefineValue\r
-#\r
-# Parse a DEFINE statement to get defined value\r
-# DEFINE Key Value\r
-#\r
-# @param String: The content to be parsed\r
-# @param Key: The key of DEFINE statement\r
-# @param CommentCharacter: Comment char, used to ignore comment content\r
-#\r
-def GetDefineValue(String, Key, CommentCharacter):\r
- if CommentCharacter:\r
- pass\r
- String = CleanString(String)\r
- return String[String.find(Key + ' ') + len(Key + ' ') : ]\r
-\r
-## GetSingleValueOfKeyFromLines\r
-#\r
-# Parse multiple strings as below to get value of each definition line\r
-# Key1 = Value1\r
-# Key2 = Value2\r
-# The result is saved to Dictionary\r
-#\r
-# @param Lines: The content to be parsed\r
-# @param Dictionary: To store data after parsing\r
-# @param CommentCharacter: Comment char, be used to ignore comment content\r
-# @param KeySplitCharacter: Key split char, between key name and key value.\r
-# Key1 = Value1, '=' is the key split char\r
-# @param ValueSplitFlag: Value split flag, be used to decide if has\r
-# multiple values\r
-# @param ValueSplitCharacter: Value split char, be used to split multiple\r
-# values. Key1 = Value1|Value2, '|' is the value\r
-# split char\r
-#\r
-def GetSingleValueOfKeyFromLines(Lines, Dictionary, CommentCharacter, KeySplitCharacter, \\r
- ValueSplitFlag, ValueSplitCharacter):\r
- Lines = Lines.split('\n')\r
- Keys = []\r
- Value = ''\r
- DefineValues = ['']\r
- SpecValues = ['']\r
-\r
- for Line in Lines:\r
- #\r
- # Handle DEFINE and SPEC\r
- #\r
- if Line.find(DataType.TAB_INF_DEFINES_DEFINE + ' ') > -1:\r
- if '' in DefineValues:\r
- DefineValues.remove('')\r
- DefineValues.append(GetDefineValue(Line, DataType.TAB_INF_DEFINES_DEFINE, CommentCharacter))\r
- continue\r
- if Line.find(DataType.TAB_INF_DEFINES_SPEC + ' ') > -1:\r
- if '' in SpecValues:\r
- SpecValues.remove('')\r
- SpecValues.append(GetDefineValue(Line, DataType.TAB_INF_DEFINES_SPEC, CommentCharacter))\r
- continue\r
-\r
- #\r
- # Handle Others\r
- #\r
- LineList = Line.split(KeySplitCharacter, 1)\r
- if len(LineList) >= 2:\r
- Key = LineList[0].split()\r
- if len(Key) == 1 and Key[0][0] != CommentCharacter:\r
- #\r
- # Remove comments and white spaces\r
- #\r
- LineList[1] = CleanString(LineList[1], CommentCharacter)\r
- if ValueSplitFlag:\r
- Value = map(strip, LineList[1].split(ValueSplitCharacter))\r
- else:\r
- Value = CleanString(LineList[1], CommentCharacter).splitlines()\r
-\r
- if Key[0] in Dictionary:\r
- if Key[0] not in Keys:\r
- Dictionary[Key[0]] = Value\r
- Keys.append(Key[0])\r
- else:\r
- Dictionary[Key[0]].extend(Value)\r
- else:\r
- Dictionary[DataType.TAB_INF_DEFINES_MACRO][Key[0]] = Value[0]\r
-\r
- if DefineValues == []:\r
- DefineValues = ['']\r
- if SpecValues == []:\r
- SpecValues = ['']\r
- Dictionary[DataType.TAB_INF_DEFINES_DEFINE] = DefineValues\r
- Dictionary[DataType.TAB_INF_DEFINES_SPEC] = SpecValues\r
-\r
- return True\r
-\r
-## The content to be parsed\r
-#\r
-# Do pre-check for a file before it is parsed\r
-# Check $()\r
-# Check []\r
-#\r
-# @param FileName: Used for error report\r
-# @param FileContent: File content to be parsed\r
-# @param SupSectionTag: Used for error report\r
-#\r
-def PreCheck(FileName, FileContent, SupSectionTag):\r
- if SupSectionTag:\r
- pass\r
- LineNo = 0\r
- IsFailed = False\r
- NewFileContent = ''\r
- for Line in FileContent.splitlines():\r
- LineNo = LineNo + 1\r
- #\r
- # Clean current line\r
- #\r
- Line = CleanString(Line)\r
- #\r
- # Remove commented line\r
- #\r
- if Line.find(DataType.TAB_COMMA_SPLIT) == 0:\r
- Line = ''\r
- #\r
- # Check $()\r
- #\r
- if Line.find('$') > -1:\r
- if Line.find('$(') < 0 or Line.find(')') < 0:\r
- Logger.Error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError=Logger.IS_RAISE_ERROR)\r
- #\r
- # Check []\r
- #\r
- if Line.find('[') > -1 or Line.find(']') > -1:\r
- #\r
- # Only get one '[' or one ']'\r
- #\r
- if not (Line.find('[') > -1 and Line.find(']') > -1):\r
- Logger.Error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError=Logger.IS_RAISE_ERROR)\r
- #\r
- # Regenerate FileContent\r
- #\r
- NewFileContent = NewFileContent + Line + '\r\n'\r
-\r
- if IsFailed:\r
- Logger.Error("Parser", FORMAT_INVALID, Line=LineNo, File=FileName, RaiseError=Logger.IS_RAISE_ERROR)\r
-\r
- return NewFileContent\r
-\r
-## CheckFileType\r
-#\r
-# Check if the Filename is including ExtName\r
-# Return True if it exists\r
-# Raise a error message if it not exists\r
-#\r
-# @param CheckFilename: Name of the file to be checked\r
-# @param ExtName: Ext name of the file to be checked\r
-# @param ContainerFilename: The container file which describes the file to be\r
-# checked, used for error report\r
-# @param SectionName: Used for error report\r
-# @param Line: The line in container file which defines the file\r
-# to be checked\r
-#\r
-def CheckFileType(CheckFilename, ExtName, ContainerFilename, SectionName, Line, LineNo= -1):\r
- if CheckFilename != '' and CheckFilename is not None:\r
- (Root, Ext) = os.path.splitext(CheckFilename)\r
- if Ext.upper() != ExtName.upper() and Root:\r
- ContainerFile = open(ContainerFilename, 'r').read()\r
- if LineNo == -1:\r
- LineNo = GetLineNo(ContainerFile, Line)\r
- ErrorMsg = ST.ERR_SECTIONNAME_INVALID % (SectionName, CheckFilename, ExtName)\r
- Logger.Error("Parser", PARSER_ERROR, ErrorMsg, Line=LineNo, \\r
- File=ContainerFilename, RaiseError=Logger.IS_RAISE_ERROR)\r
-\r
- return True\r
-\r
-## CheckFileExist\r
-#\r
-# Check if the file exists\r
-# Return True if it exists\r
-# Raise a error message if it not exists\r
-#\r
-# @param CheckFilename: Name of the file to be checked\r
-# @param WorkspaceDir: Current workspace dir\r
-# @param ContainerFilename: The container file which describes the file to\r
-# be checked, used for error report\r
-# @param SectionName: Used for error report\r
-# @param Line: The line in container file which defines the\r
-# file to be checked\r
-#\r
-def CheckFileExist(WorkspaceDir, CheckFilename, ContainerFilename, SectionName, Line, LineNo= -1):\r
- CheckFile = ''\r
- if CheckFilename != '' and CheckFilename is not None:\r
- CheckFile = WorkspaceFile(WorkspaceDir, CheckFilename)\r
- if not os.path.isfile(CheckFile):\r
- ContainerFile = open(ContainerFilename, 'r').read()\r
- if LineNo == -1:\r
- LineNo = GetLineNo(ContainerFile, Line)\r
- ErrorMsg = ST.ERR_CHECKFILE_NOTFOUND % (CheckFile, SectionName)\r
- Logger.Error("Parser", PARSER_ERROR, ErrorMsg,\r
- File=ContainerFilename, Line=LineNo, RaiseError=Logger.IS_RAISE_ERROR)\r
- return CheckFile\r
-\r
-## GetLineNo\r
-#\r
-# Find the index of a line in a file\r
-#\r
-# @param FileContent: Search scope\r
-# @param Line: Search key\r
-#\r
-def GetLineNo(FileContent, Line, IsIgnoreComment=True):\r
- LineList = FileContent.splitlines()\r
- for Index in range(len(LineList)):\r
- if LineList[Index].find(Line) > -1:\r
- #\r
- # Ignore statement in comment\r
- #\r
- if IsIgnoreComment:\r
- if LineList[Index].strip()[0] == DataType.TAB_COMMENT_SPLIT:\r
- continue\r
- return Index + 1\r
-\r
- return -1\r
-\r
-## RaiseParserError\r
-#\r
-# Raise a parser error\r
-#\r
-# @param Line: String which has error\r
-# @param Section: Used for error report\r
-# @param File: File which has the string\r
-# @param Format: Correct format\r
-#\r
-def RaiseParserError(Line, Section, File, Format='', LineNo= -1):\r
- if LineNo == -1:\r
- LineNo = GetLineNo(open(os.path.normpath(File), 'r').read(), Line)\r
- ErrorMsg = ST.ERR_INVALID_NOTFOUND % (Line, Section)\r
- if Format != '':\r
- Format = "Correct format is " + Format\r
- Logger.Error("Parser", PARSER_ERROR, ErrorMsg, File=File, Line=LineNo, \\r
- ExtraData=Format, RaiseError=Logger.IS_RAISE_ERROR)\r
-\r
-## WorkspaceFile\r
-#\r
-# Return a full path with workspace dir\r
-#\r
-# @param WorkspaceDir: Workspace dir\r
-# @param Filename: Relative file name\r
-#\r
-def WorkspaceFile(WorkspaceDir, Filename):\r
- return os.path.join(NormPath(WorkspaceDir), NormPath(Filename))\r
-\r
-## Split string\r
-#\r
-# Revmove '"' which startswith and endswith string\r
-#\r
-# @param String: The string need to be splited\r
-#\r
-def SplitString(String):\r
- if String.startswith('\"'):\r
- String = String[1:]\r
- if String.endswith('\"'):\r
- String = String[:-1]\r
- return String\r
-\r
-## Convert To Sql String\r
-#\r
-# Replace "'" with "''" in each item of StringList\r
-#\r
-# @param StringList: A list for strings to be converted\r
-#\r
-def ConvertToSqlString(StringList):\r
- return map(lambda s: s.replace("'", "''") , StringList)\r
-\r
-## Convert To Sql String\r
-#\r
-# Replace "'" with "''" in the String\r
-#\r
-# @param String: A String to be converted\r
-#\r
-def ConvertToSqlString2(String):\r
- return String.replace("'", "''")\r
-\r
-## GetStringOfList\r
-#\r
-# Get String of a List\r
-#\r
-# @param Lines: string list\r
-# @param Split: split character\r
-#\r
-def GetStringOfList(List, Split=' '):\r
- if type(List) != type([]):\r
- return List\r
- Str = ''\r
- for Item in List:\r
- Str = Str + Item + Split\r
- return Str.strip()\r
-\r
-## Get HelpTextList\r
-#\r
-# Get HelpTextList from HelpTextClassList\r
-#\r
-# @param HelpTextClassList: Help Text Class List\r
-#\r
-def GetHelpTextList(HelpTextClassList):\r
- List = []\r
- if HelpTextClassList:\r
- for HelpText in HelpTextClassList:\r
- if HelpText.String.endswith('\n'):\r
- HelpText.String = HelpText.String[0: len(HelpText.String) - len('\n')]\r
- List.extend(HelpText.String.split('\n'))\r
- return List\r
-\r
-## Get String Array Length\r
-#\r
-# Get String Array Length\r
-#\r
-# @param String: the source string\r
-#\r
-def StringArrayLength(String):\r
- if isinstance(String, unicode):\r
- return (len(String) + 1) * 2 + 1\r
- elif String.startswith('L"'):\r
- return (len(String) - 3 + 1) * 2\r
- elif String.startswith('"'):\r
- return (len(String) - 2 + 1)\r
- else:\r
- return len(String.split()) + 1\r
-\r
-## RemoveDupOption\r
-#\r
-# Remove Dup Option\r
-#\r
-# @param OptionString: the option string\r
-# @param Which: Which flag\r
-# @param Against: Against flag\r
-#\r
-def RemoveDupOption(OptionString, Which="/I", Against=None):\r
- OptionList = OptionString.split()\r
- ValueList = []\r
- if Against:\r
- ValueList += Against\r
- for Index in range(len(OptionList)):\r
- Opt = OptionList[Index]\r
- if not Opt.startswith(Which):\r
- continue\r
- if len(Opt) > len(Which):\r
- Val = Opt[len(Which):]\r
- else:\r
- Val = ""\r
- if Val in ValueList:\r
- OptionList[Index] = ""\r
- else:\r
- ValueList.append(Val)\r
- return " ".join(OptionList)\r
-\r
-## Check if the string is HexDgit\r
-#\r
-# Return true if all characters in the string are digits and there is at\r
-# least one character\r
-# or valid Hexs (started with 0x, following by hexdigit letters)\r
-# , false otherwise.\r
-# @param string: input string\r
-#\r
-def IsHexDigit(Str):\r
- try:\r
- int(Str, 10)\r
- return True\r
- except ValueError:\r
- if len(Str) > 2 and Str.upper().startswith('0X'):\r
- try:\r
- int(Str, 16)\r
- return True\r
- except ValueError:\r
- return False\r
- return False\r
-\r
-## Check if the string is HexDgit and its interger value within limit of UINT32\r
-#\r
-# Return true if all characters in the string are digits and there is at\r
-# least one character\r
-# or valid Hexs (started with 0x, following by hexdigit letters)\r
-# , false otherwise.\r
-# @param string: input string\r
-#\r
-def IsHexDigitUINT32(Str):\r
- try:\r
- Value = int(Str, 10)\r
- if (Value <= 0xFFFFFFFF) and (Value >= 0):\r
- return True\r
- except ValueError:\r
- if len(Str) > 2 and Str.upper().startswith('0X'):\r
- try:\r
- Value = int(Str, 16)\r
- if (Value <= 0xFFFFFFFF) and (Value >= 0):\r
- return True\r
- except ValueError:\r
- return False\r
- return False\r
-\r
-## CleanSpecialChar\r
-#\r
-# The ASCII text files of type INF, DEC, INI are edited by developers,\r
-# and may contain characters that cannot be directly translated to strings that\r
-# are conformant with the UDP XML Schema. Any characters in this category\r
-# (0x00-0x08, TAB [0x09], 0x0B, 0x0C, 0x0E-0x1F, 0x80-0xFF)\r
-# must be converted to a space character[0x20] as part of the parsing process.\r
-#\r
-def ConvertSpecialChar(Lines):\r
- RetLines = []\r
- for line in Lines:\r
- ReMatchSpecialChar = re.compile(r"[\x00-\x08]|\x09|\x0b|\x0c|[\x0e-\x1f]|[\x7f-\xff]")\r
- RetLines.append(ReMatchSpecialChar.sub(' ', line))\r
-\r
- return RetLines\r
-\r
-## __GetTokenList\r
-#\r
-# Assume Str is a valid feature flag expression.\r
-# Return a list which contains tokens: alpha numeric token and other token\r
-# Whitespace are not stripped\r
-#\r
-def __GetTokenList(Str):\r
- InQuote = False\r
- Token = ''\r
- TokenOP = ''\r
- PreChar = ''\r
- List = []\r
- for Char in Str:\r
- if InQuote:\r
- Token += Char\r
- if Char == '"' and PreChar != '\\':\r
- InQuote = not InQuote\r
- List.append(Token)\r
- Token = ''\r
- continue\r
- if Char == '"':\r
- if Token and Token != 'L':\r
- List.append(Token)\r
- Token = ''\r
- if TokenOP:\r
- List.append(TokenOP)\r
- TokenOP = ''\r
- InQuote = not InQuote\r
- Token += Char\r
- continue\r
-\r
- if not (Char.isalnum() or Char in '_'):\r
- TokenOP += Char\r
- if Token:\r
- List.append(Token)\r
- Token = ''\r
- else:\r
- Token += Char\r
- if TokenOP:\r
- List.append(TokenOP)\r
- TokenOP = ''\r
-\r
- if PreChar == '\\' and Char == '\\':\r
- PreChar = ''\r
- else:\r
- PreChar = Char\r
- if Token:\r
- List.append(Token)\r
- if TokenOP:\r
- List.append(TokenOP)\r
- return List\r
-\r
-## ConvertNEToNOTEQ\r
-#\r
-# Convert NE operator to NOT EQ\r
-# For example: 1 NE 2 -> 1 NOT EQ 2\r
-#\r
-# @param Expr: Feature flag expression to be converted\r
-#\r
-def ConvertNEToNOTEQ(Expr):\r
- List = __GetTokenList(Expr)\r
- for Index in range(len(List)):\r
- if List[Index] == 'NE':\r
- List[Index] = 'NOT EQ'\r
- return ''.join(List)\r
-\r
-## ConvertNOTEQToNE\r
-#\r
-# Convert NOT EQ operator to NE\r
-# For example: 1 NOT NE 2 -> 1 NE 2\r
-#\r
-# @param Expr: Feature flag expression to be converted\r
-#\r
-def ConvertNOTEQToNE(Expr):\r
- List = __GetTokenList(Expr)\r
- HasNOT = False\r
- RetList = []\r
- for Token in List:\r
- if HasNOT and Token == 'EQ':\r
- # At least, 'NOT' is in the list\r
- while not RetList[-1].strip():\r
- RetList.pop()\r
- RetList[-1] = 'NE'\r
- HasNOT = False\r
- continue\r
- if Token == 'NOT':\r
- HasNOT = True\r
- elif Token.strip():\r
- HasNOT = False\r
- RetList.append(Token)\r
-\r
- return ''.join(RetList)\r
-\r
-## SplitPcdEntry\r
-#\r
-# Split an PCD entry string to Token.CName and PCD value and FFE.\r
-# NOTE: PCD Value and FFE can contain "|" in it's expression. And in INF specification, have below rule.\r
-# When using the characters "|" or "||" in an expression, the expression must be encapsulated in\r
-# open "(" and close ")" parenthesis.\r
-#\r
-# @param String An PCD entry string need to be split.\r
-#\r
-# @return List [PcdTokenCName, Value, FFE]\r
-#\r
-def SplitPcdEntry(String):\r
- if not String:\r
- return ['', '', ''], False\r
-\r
- PcdTokenCName = ''\r
- PcdValue = ''\r
- PcdFeatureFlagExp = ''\r
-\r
- ValueList = GetSplitValueList(String, "|", 1)\r
-\r
- #\r
- # Only contain TokenCName\r
- #\r
- if len(ValueList) == 1:\r
- return [ValueList[0]], True\r
-\r
- NewValueList = []\r
-\r
- if len(ValueList) == 2:\r
- PcdTokenCName = ValueList[0]\r
-\r
- InQuote = False\r
- InParenthesis = False\r
- StrItem = ''\r
- for StrCh in ValueList[1]:\r
- if StrCh == '"':\r
- InQuote = not InQuote\r
- elif StrCh == '(' or StrCh == ')':\r
- InParenthesis = not InParenthesis\r
-\r
- if StrCh == '|':\r
- if not InQuote or not InParenthesis:\r
- NewValueList.append(StrItem.strip())\r
- StrItem = ' '\r
- continue\r
-\r
- StrItem += StrCh\r
-\r
- NewValueList.append(StrItem.strip())\r
-\r
- if len(NewValueList) == 1:\r
- PcdValue = NewValueList[0]\r
- return [PcdTokenCName, PcdValue], True\r
- elif len(NewValueList) == 2:\r
- PcdValue = NewValueList[0]\r
- PcdFeatureFlagExp = NewValueList[1]\r
- return [PcdTokenCName, PcdValue, PcdFeatureFlagExp], True\r
- else:\r
- return ['', '', ''], False\r
-\r
- return ['', '', ''], False\r
-\r
-## Check if two arches matched?\r
-#\r
-# @param Arch1\r
-# @param Arch2\r
-#\r
-def IsMatchArch(Arch1, Arch2):\r
- if 'COMMON' in Arch1 or 'COMMON' in Arch2:\r
- return True\r
- if isinstance(Arch1, basestring) and isinstance(Arch2, basestring):\r
- if Arch1 == Arch2:\r
- return True\r
-\r
- if isinstance(Arch1, basestring) and isinstance(Arch2, list):\r
- return Arch1 in Arch2\r
-\r
- if isinstance(Arch2, basestring) and isinstance(Arch1, list):\r
- return Arch2 in Arch1\r
-\r
- if isinstance(Arch1, list) and isinstance(Arch2, list):\r
- for Item1 in Arch1:\r
- for Item2 in Arch2:\r
- if Item1 == Item2:\r
- return True\r
-\r
- return False\r
-\r
-# Search all files in FilePath to find the FileName with the largest index\r
-# Return the FileName with index +1 under the FilePath\r
-#\r
-def GetUniFileName(FilePath, FileName):\r
- Files = []\r
- try:\r
- Files = os.listdir(FilePath)\r
- except:\r
- pass\r
-\r
- LargestIndex = -1\r
- IndexNotFound = True\r
- for File in Files:\r
- if File.upper().startswith(FileName.upper()) and File.upper().endswith('.UNI'):\r
- Index = File.upper().replace(FileName.upper(), '').replace('.UNI', '')\r
- if Index:\r
- try:\r
- Index = int(Index)\r
- except Exception:\r
- Index = -1\r
- else:\r
- IndexNotFound = False\r
- Index = 0\r
- if Index > LargestIndex:\r
- LargestIndex = Index + 1\r
-\r
- if LargestIndex > -1 and not IndexNotFound:\r
- return os.path.normpath(os.path.join(FilePath, FileName + str(LargestIndex) + '.uni'))\r
- else:\r
- return os.path.normpath(os.path.join(FilePath, FileName + '.uni'))\r