BaseTools:Trim will trig exception when input asl UTF8 format file

BZ:https://bugzilla.tianocore.org/show_bug.cgi?id=1641

The command trim --asl-file -o test.i UTF8.asl will trig the exception.
Trim tool should report error message for unsupported UTF8 file instead
of the exception.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Zhiju.Fan <zhijux.fan@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
This commit is contained in:
Fan, ZhijuX 2019-03-28 10:12:31 +08:00 committed by Feng, Bob C
parent 8028f03032
commit 58742d7945
1 changed files with 21 additions and 23 deletions

View File

@ -18,7 +18,7 @@ import Common.LongFilePathOs as os
import sys import sys
import re import re
from io import BytesIO from io import BytesIO
import codecs
from optparse import OptionParser from optparse import OptionParser
from optparse import make_option from optparse import make_option
from Common.BuildToolError import * from Common.BuildToolError import *
@ -77,14 +77,11 @@ gIncludedAslFile = []
def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong): def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):
CreateDirectory(os.path.dirname(Target)) CreateDirectory(os.path.dirname(Target))
try: try:
f = open (Source, 'r') with open(Source, "r") as File:
Lines = File.readlines()
except: except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source) EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)
# read whole file
Lines = f.readlines()
f.close()
PreprocessedFile = "" PreprocessedFile = ""
InjectedFile = "" InjectedFile = ""
LineIndexOfOriginalFile = None LineIndexOfOriginalFile = None
@ -181,11 +178,10 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):
# save to file # save to file
try: try:
f = open (Target, 'w') with open(Target, 'w') as File:
File.writelines(NewLines)
except: except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target) EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
f.writelines(NewLines)
f.close()
## Trim preprocessed VFR file ## Trim preprocessed VFR file
# #
@ -199,12 +195,11 @@ def TrimPreprocessedVfr(Source, Target):
CreateDirectory(os.path.dirname(Target)) CreateDirectory(os.path.dirname(Target))
try: try:
f = open (Source, 'r') with open(Source, "r") as File:
Lines = File.readlines()
except: except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source) EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)
# read whole file # read whole file
Lines = f.readlines()
f.close()
FoundTypedef = False FoundTypedef = False
Brace = 0 Brace = 0
@ -248,11 +243,10 @@ def TrimPreprocessedVfr(Source, Target):
# save all lines trimmed # save all lines trimmed
try: try:
f = open (Target, 'w') with open(Target, 'w') as File:
File.writelines(Lines)
except: except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target) EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
f.writelines(Lines)
f.close()
## Read the content ASL file, including ASL included, recursively ## Read the content ASL file, including ASL included, recursively
# #
@ -278,7 +272,12 @@ def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):
for IncludePath in SearchPathList: for IncludePath in SearchPathList:
IncludeFile = os.path.join(IncludePath, Source) IncludeFile = os.path.join(IncludePath, Source)
if os.path.isfile(IncludeFile): if os.path.isfile(IncludeFile):
F = open(IncludeFile, "r") try:
with open(IncludeFile, "r") as File:
F = File.readlines()
except:
with codecs.open(IncludeFile, "r", encoding='utf-8') as File:
F = File.readlines()
break break
else: else:
EdkLogger.error("Trim", "Failed to find include file %s" % Source) EdkLogger.error("Trim", "Failed to find include file %s" % Source)
@ -313,7 +312,6 @@ def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):
NewFileContent.append("\n") NewFileContent.append("\n")
gIncludedAslFile.pop() gIncludedAslFile.pop()
F.close()
return NewFileContent return NewFileContent
@ -345,7 +343,9 @@ def TrimAslFile(Source, Target, IncludePathFile):
if IncludePathFile: if IncludePathFile:
try: try:
LineNum = 0 LineNum = 0
for Line in open(IncludePathFile, 'r'): with open(IncludePathFile, 'r') as File:
FileLines = File.readlines()
for Line in FileLines:
LineNum += 1 LineNum += 1
if Line.startswith("/I") or Line.startswith ("-I"): if Line.startswith("/I") or Line.startswith ("-I"):
IncludePathList.append(Line[2:].strip()) IncludePathList.append(Line[2:].strip())
@ -363,13 +363,11 @@ def TrimAslFile(Source, Target, IncludePathFile):
# save all lines trimmed # save all lines trimmed
try: try:
f = open (Target, 'w') with open(Target, 'w') as File:
File.writelines(Lines)
except: except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target) EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
f.writelines(Lines)
f.close()
def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile): def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):
VfrNameList = [] VfrNameList = []
if os.path.isdir(DebugDir): if os.path.isdir(DebugDir):
@ -389,7 +387,7 @@ def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):
return return
try: try:
fInputfile = open(OutputFile, "wb+", 0) fInputfile = open(OutputFile, "wb+")
except: except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %s" %OutputFile, None) EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %s" %OutputFile, None)