From a09f4c91f785e36f0987aa3a6d7656ba51e6aeda Mon Sep 17 00:00:00 2001 From: Yunhua Feng Date: Fri, 27 Jul 2018 16:02:05 +0800 Subject: [PATCH] BaseTools: fix the open file's read and write bugs Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Yunhua Feng Reviewed-by: Liming Gao --- BaseTools/Source/Python/AutoGen/GenMake.py | 12 ++++++-- .../Source/Python/AutoGen/InfSectionParser.py | 2 +- BaseTools/Source/Python/BPDG/GenVpd.py | 4 +-- BaseTools/Source/Python/Common/Misc.py | 28 +++++++++++++------ BaseTools/Source/Python/GenFds/FdfParser.py | 4 +-- BaseTools/Source/Python/Trim/Trim.py | 6 ++-- BaseTools/Source/Python/build/BuildReport.py | 2 +- 7 files changed, 38 insertions(+), 20 deletions(-) diff --git a/BaseTools/Source/Python/AutoGen/GenMake.py b/BaseTools/Source/Python/AutoGen/GenMake.py index 75b4e2247a..b168a6663d 100644 --- a/BaseTools/Source/Python/AutoGen/GenMake.py +++ b/BaseTools/Source/Python/AutoGen/GenMake.py @@ -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() diff --git a/BaseTools/Source/Python/AutoGen/InfSectionParser.py b/BaseTools/Source/Python/AutoGen/InfSectionParser.py index d985089738..09e9af3fb4 100644 --- a/BaseTools/Source/Python/AutoGen/InfSectionParser.py +++ b/BaseTools/Source/Python/AutoGen/InfSectionParser.py @@ -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) diff --git a/BaseTools/Source/Python/BPDG/GenVpd.py b/BaseTools/Source/Python/BPDG/GenVpd.py index 366fac095b..bd2c05d782 100644 --- a/BaseTools/Source/Python/BPDG/GenVpd.py +++ b/BaseTools/Source/Python/BPDG/GenVpd.py @@ -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) diff --git a/BaseTools/Source/Python/Common/Misc.py b/BaseTools/Source/Python/Common/Misc.py index b91f06b19e..1d62a8b56b 100644 --- a/BaseTools/Source/Python/Common/Misc.py +++ b/BaseTools/Source/Python/Common/Misc.py @@ -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) diff --git a/BaseTools/Source/Python/GenFds/FdfParser.py b/BaseTools/Source/Python/GenFds/FdfParser.py index cbdc893cdf..ff8ccf21bd 100644 --- a/BaseTools/Source/Python/GenFds/FdfParser.py +++ b/BaseTools/Source/Python/GenFds/FdfParser.py @@ -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: diff --git a/BaseTools/Source/Python/Trim/Trim.py b/BaseTools/Source/Python/Trim/Trim.py index 4b3091bec3..b9ca83d6d8 100644 --- a/BaseTools/Source/Python/Trim/Trim.py +++ b/BaseTools/Source/Python/Trim/Trim.py @@ -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) diff --git a/BaseTools/Source/Python/build/BuildReport.py b/BaseTools/Source/Python/build/BuildReport.py index 4fe29f124d..33cc9db735 100644 --- a/BaseTools/Source/Python/build/BuildReport.py +++ b/BaseTools/Source/Python/build/BuildReport.py @@ -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)) -- 2.39.2