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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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