X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FAutoGen%2FStrGather.py;h=9c7dd1e40374adf45080909a6f0b34f807c8a884;hp=20af015bcf254c7e1d0e0ddf9766fc8c2365d21a;hb=4d83616f9d7731934e7f91058efb165a2bc5b9d7;hpb=1be2ed90a20618d71ddf34b8a07d038da0b36854 diff --git a/BaseTools/Source/Python/AutoGen/StrGather.py b/BaseTools/Source/Python/AutoGen/StrGather.py index 20af015bcf..9c7dd1e403 100644 --- a/BaseTools/Source/Python/AutoGen/StrGather.py +++ b/BaseTools/Source/Python/AutoGen/StrGather.py @@ -1,4 +1,8 @@ -# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.
+## @file +# This file is used to parse a strings file and create or add to a string database +# file. +# +# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at @@ -7,10 +11,6 @@ # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -#This file is used to parse a strings file and create or add to a string database file. -# - ## # Import Modules # @@ -19,7 +19,7 @@ import Common.EdkLogger as EdkLogger from Common.BuildToolError import * from UniClassObject import * from StringIO import StringIO -from struct import pack +from struct import pack, unpack from Common.LongFilePathSupport import OpenLongFilePath as open ## @@ -59,11 +59,7 @@ NOT_REFERENCED = 'not referenced' COMMENT_NOT_REFERENCED = ' ' + COMMENT + NOT_REFERENCED CHAR_ARRAY_DEFIN = 'unsigned char' COMMON_FILE_NAME = 'Strings' -OFFSET = 'offset' -STRING = 'string' -TO = 'to' STRING_TOKEN = re.compile('STRING_TOKEN *\(([A-Z0-9_]+) *\)', re.MULTILINE | re.UNICODE) -COMPATIBLE_STRING_TOKEN = re.compile('STRING_TOKEN *\(([A-Z0-9_]+) *\)', re.MULTILINE | re.UNICODE) EFI_HII_ARRAY_SIZE_LENGTH = 4 EFI_HII_PACKAGE_HEADER_LENGTH = 4 @@ -98,7 +94,7 @@ PRINTABLE_LANGUAGE_NAME_STRING_NAME = '$PRINTABLE_LANGUAGE_NAME' # @retval: The formatted hex string # def DecToHexStr(Dec, Digit = 8): - return eval("'0x%0" + str(Digit) + "X' % int(Dec)") + return '0x{0:0{1}X}'.format(Dec,Digit) ## Convert a dec number to a hex list # @@ -113,11 +109,8 @@ def DecToHexStr(Dec, Digit = 8): # @retval: A list for formatted hex string # def DecToHexList(Dec, Digit = 8): - Hex = eval("'%0" + str(Digit) + "X' % int(Dec)" ) - List = [] - for Bit in range(Digit - 2, -1, -2): - List.append(HexHeader + Hex[Bit:Bit + 2]) - return List + Hex = '{0:0{1}X}'.format(Dec,Digit) + return ["0x" + Hex[Bit:Bit + 2] for Bit in range(Digit - 2, -1, -2)] ## Convert a acsii string to a hex list # @@ -129,27 +122,7 @@ def DecToHexList(Dec, Digit = 8): # @retval: A list for formatted hex string # def AscToHexList(Ascii): - List = [] - for Item in Ascii: - List.append('0x%2X' % ord(Item)) - - return List - -## Create header of .h file -# -# Create a header of .h file -# -# @param BaseName: The basename of strings -# -# @retval Str: A string for .h file header -# -def CreateHFileHeader(BaseName): - Str = '' - for Item in H_C_FILE_HEADER: - Str = WriteLine(Str, Item) - Str = WriteLine(Str, '#ifndef _' + BaseName.upper() + '_STRINGS_DEFINE_H_') - Str = WriteLine(Str, '#define _' + BaseName.upper() + '_STRINGS_DEFINE_H_') - return Str + return ['0x{0:02X}'.format(ord(Item)) for Item in Ascii] ## Create content of .h file # @@ -192,7 +165,7 @@ def CreateHFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag): Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED UnusedStr = WriteLine(UnusedStr, Line) - Str = ''.join([Str,UnusedStr]) + Str = ''.join([Str, UnusedStr]) Str = WriteLine(Str, '') if IsCompatibleMode or UniGenCFlag: @@ -215,19 +188,6 @@ def CreateHFile(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag): return HFile -## Create header of .c file -# -# Create a header of .c file -# -# @retval Str: A string for .c file header -# -def CreateCFileHeader(): - Str = '' - for Item in H_C_FILE_HEADER: - Str = WriteLine(Str, Item) - - return Str - ## Create a buffer to store all items in an array # # @param BinBuffer Buffer to contain Binary data. @@ -235,7 +195,7 @@ def CreateCFileHeader(): # def CreateBinBuffer(BinBuffer, Array): for Item in Array: - BinBuffer.write(pack("B", int(Item,16))) + BinBuffer.write(pack("B", int(Item, 16))) ## Create a formatted string all items in an array # @@ -258,7 +218,7 @@ def CreateArrayItem(Array, Width = 16): Index = Index + 1 else: ArrayItem = WriteLine(ArrayItem, Line) - Line = ' ' + Item + ', ' + Line = ' ' + Item + ', ' Index = 1 ArrayItem = Write(ArrayItem, Line.rstrip()) @@ -320,7 +280,7 @@ def GetFilteredLanguage(UniLanguageList, LanguageFilterList): if PrimaryTag == UniLanguagePrimaryTag: if UniLanguage not in UniLanguageListFiltered: - UniLanguageListFiltered += [UniLanguage] + UniLanguageListFiltered += [UniLanguage] break else: # Here is rule 3 for "get best language" @@ -368,7 +328,7 @@ def CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniBinBuffer, UniLanguageList = [] for IndexI in range(len(UniObjectClass.LanguageDef)): - UniLanguageList += [UniObjectClass.LanguageDef[IndexI][0]] + UniLanguageList += [UniObjectClass.LanguageDef[IndexI][0]] UniLanguageListFiltered = GetFilteredLanguage(UniLanguageList, LanguageFilterList) @@ -450,14 +410,14 @@ def CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniBinBuffer, if UniBinBuffer: CreateBinBuffer (UniBinBuffer, List) UniBinBuffer.write (StringBuffer.getvalue()) - UniBinBuffer.write (pack("B", int(EFI_HII_SIBT_END,16))) + UniBinBuffer.write (pack("B", int(EFI_HII_SIBT_END, 16))) StringBuffer.close() # # Create line for string variable name # "unsigned char $(BaseName)Strings[] = {" # - AllStr = WriteLine('', CHAR_ARRAY_DEFIN + ' ' + BaseName + COMMON_FILE_NAME + '[] = {\n' ) + AllStr = WriteLine('', CHAR_ARRAY_DEFIN + ' ' + BaseName + COMMON_FILE_NAME + '[] = {\n') if IsCompatibleMode: # @@ -504,7 +464,6 @@ def CreateCFileEnd(): # def CreateCFile(BaseName, UniObjectClass, IsCompatibleMode, FilterInfo): CFile = '' - #CFile = WriteLine(CFile, CreateCFileHeader()) CFile = WriteLine(CFile, CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, None, FilterInfo)) CFile = WriteLine(CFile, CreateCFileEnd()) return CFile @@ -572,11 +531,7 @@ def SearchString(UniObjectClass, FileList, IsCompatibleMode): if os.path.isfile(File): Lines = open(File, 'r') for Line in Lines: - if not IsCompatibleMode: - StringTokenList = STRING_TOKEN.findall(Line) - else: - StringTokenList = COMPATIBLE_STRING_TOKEN.findall(Line) - for StrName in StringTokenList: + for StrName in STRING_TOKEN.findall(Line): EdkLogger.debug(EdkLogger.DEBUG_5, "Found string identifier: " + StrName) UniObjectClass.SetStringReferenced(StrName) @@ -618,13 +573,13 @@ def GetStringFiles(UniFilList, SourceFileList, IncludeList, IncludePathList, Ski # Write an item # def Write(Target, Item): - return ''.join([Target,Item]) + return ''.join([Target, Item]) # # Write an item with a break line # def WriteLine(Target, Item): - return ''.join([Target,Item,'\n']) + return ''.join([Target, Item, '\n']) # This acts like the main() function for the script, unless it is 'import'ed into another # script.