BaseTools: fix the open file's read and write bugs

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
This commit is contained in:
Yunhua Feng 2018-07-27 16:02:05 +08:00 committed by Yonghong Zhu
parent fe3991d635
commit a09f4c91f7
7 changed files with 38 additions and 20 deletions

View File

@ -1031,7 +1031,7 @@ cleanlib:
CurrentFileDependencyList = DepDb[F] CurrentFileDependencyList = DepDb[F]
else: else:
try: try:
Fd = open(F.Path, 'r') Fd = open(F.Path, 'rb')
except BaseException as X: except BaseException as X:
EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=F.Path + "\n\t" + str(X)) EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=F.Path + "\n\t" + str(X))
@ -1041,8 +1041,14 @@ cleanlib:
continue continue
if FileContent[0] == 0xff or FileContent[0] == 0xfe: if FileContent[0] == 0xff or FileContent[0] == 0xfe:
FileContent = unicode(FileContent, "utf-16") FileContent = str(FileContent, encoding="utf-16")
IncludedFileList = gIncludePattern.findall(FileContent) IncludedFileList = gIncludePattern.findall(FileContent)
else:
try:
FileContent = str(FileContent, encoding="utf-8")
IncludedFileList = gIncludePattern.findall(FileContent)
except:
continue
for Inc in IncludedFileList: for Inc in IncludedFileList:
Inc = Inc.strip() Inc = Inc.strip()

View File

@ -34,7 +34,7 @@ class InfSectionParser():
SectionData = [] SectionData = []
try: try:
FileLinesList = open(self._FilePath, "r", 0).readlines() FileLinesList = open(self._FilePath, "r").readlines()
except BaseException: except BaseException:
EdkLogger.error("build", AUTOGEN_ERROR, 'File %s is opened failed.' % self._FilePath) EdkLogger.error("build", AUTOGEN_ERROR, 'File %s is opened failed.' % self._FilePath)

View File

@ -305,7 +305,7 @@ class GenVPD :
self.PcdFixedOffsetSizeList = [] self.PcdFixedOffsetSizeList = []
self.PcdUnknownOffsetList = [] self.PcdUnknownOffsetList = []
try: try:
fInputfile = open(InputFileName, "r", 0) fInputfile = open(InputFileName, "r")
try: try:
self.FileLinesList = fInputfile.readlines() self.FileLinesList = fInputfile.readlines()
except: except:
@ -650,7 +650,7 @@ class GenVPD :
EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.VpdFileName, None) EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.VpdFileName, None)
try : try :
fMapFile = open(MapFileName, "w", 0) fMapFile = open(MapFileName, "w")
except: except:
# Open failed # Open failed
EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.MapFileName, None) EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.MapFileName, None)

View File

@ -459,7 +459,13 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
if os.path.exists(File): if os.path.exists(File):
try: try:
if Content == open(File, "rb").read(): if isinstance(Content, bytes):
with open(File, "rb") as f:
if Content == f.read():
return False
else:
with open(File, "r") as f:
if Content == f.read():
return False return False
except: except:
EdkLogger.error(None, FILE_OPEN_FAILURE, ExtraData=File) EdkLogger.error(None, FILE_OPEN_FAILURE, ExtraData=File)
@ -480,13 +486,19 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
if not SaveFileToDisk(File, Content): if not SaveFileToDisk(File, Content):
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData=File) EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData=File)
except: except:
Fd = open(File, "wb") if isinstance(Content, bytes):
with open(File, "wb") as Fd:
Fd.write(Content) Fd.write(Content)
Fd.close()
else: else:
Fd = open(File, "wb") with open(File, "w") as Fd:
Fd.write(Content)
else:
if isinstance(Content, bytes):
with open(File, "wb") as Fd:
Fd.write(Content)
else:
with open(File, "w") as Fd:
Fd.write(Content) Fd.write(Content)
Fd.close()
except IOError as X: except IOError as X:
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X) EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)

View File

@ -155,7 +155,7 @@ class IncludeFileProfile :
self.FileName = FileName self.FileName = FileName
self.FileLinesList = [] self.FileLinesList = []
try: try:
fsock = open(FileName, "rb", 0) fsock = open(FileName, "r")
try: try:
self.FileLinesList = fsock.readlines() self.FileLinesList = fsock.readlines()
for index, line in enumerate(self.FileLinesList): for index, line in enumerate(self.FileLinesList):
@ -216,7 +216,7 @@ class FileProfile :
def __init__(self, FileName): def __init__(self, FileName):
self.FileLinesList = [] self.FileLinesList = []
try: try:
fsock = open(FileName, "rb", 0) fsock = open(FileName, "r")
try: try:
self.FileLinesList = fsock.readlines() self.FileLinesList = fsock.readlines()
finally: finally:

View File

@ -245,7 +245,7 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):
# save to file # save to file
try: try:
f = open (Target, 'wb') f = open (Target, 'w')
except: except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target) EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
f.writelines(NewLines) f.writelines(NewLines)
@ -562,7 +562,7 @@ def TrimEdkSourceCode(Source, Target):
CreateDirectory(os.path.dirname(Target)) CreateDirectory(os.path.dirname(Target))
try: try:
f = open (Source, 'rb') f = open (Source, 'r')
except: except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source) EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)
# read whole file # read whole file
@ -581,7 +581,7 @@ def TrimEdkSourceCode(Source, Target):
return return
try: try:
f = open (Target, 'wb') f = open (Target, 'w')
except: except:
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target) EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
f.write(NewLines) f.write(NewLines)

View File

@ -634,7 +634,7 @@ class ModuleReport(object):
FwReportFileName = os.path.join(self._BuildDir, "DEBUG", self.ModuleName + ".txt") FwReportFileName = os.path.join(self._BuildDir, "DEBUG", self.ModuleName + ".txt")
if os.path.isfile(FwReportFileName): if os.path.isfile(FwReportFileName):
try: try:
FileContents = open(FwReportFileName).read() FileContents = open(FwReportFileName, 'r').read()
Match = gModuleSizePattern.search(FileContents) Match = gModuleSizePattern.search(FileContents)
if Match: if Match:
self.Size = int(Match.group(1)) self.Size = int(Match.group(1))